This commit is contained in:
Holger Obermaier 2022-02-08 12:22:56 +01:00
parent 6d55c376bd
commit c47ac2ebc3

View File

@ -22,10 +22,10 @@ type ccMetric struct {
tm time.Time // timestamp tm time.Time // timestamp
} }
// ccmetric access functions // ccMetric access functions
type CCMetric interface { type CCMetric interface {
ToLineProtocol(metaAsTags bool) string // Generate influxDB line protocol for data type ccMetric
ToPoint(metaAsTags bool) *write.Point // Generate influxDB point for data type ccMetric ToPoint(metaAsTags bool) *write.Point // Generate influxDB point for data type ccMetric
ToLineProtocol(metaAsTags bool) string // Generate influxDB line protocol for data type ccMetric
Name() string // Get metric name Name() string // Get metric name
SetName(name string) // Set metric name SetName(name string) // Set metric name
@ -36,13 +36,13 @@ type CCMetric interface {
Tags() map[string]string // Map of tags Tags() map[string]string // Map of tags
AddTag(key, value string) // Add a tag AddTag(key, value string) // Add a tag
GetTag(key string) (value string, ok bool) // Get a tag by its key GetTag(key string) (value string, ok bool) // Get a tag by its key
HasTag(key string) (ok bool) // Check a tag HasTag(key string) (ok bool) // Check if a tag key is present
RemoveTag(key string) // Remove a tag by its key RemoveTag(key string) // Remove a tag by its key
Meta() map[string]string // Map of meta data tags Meta() map[string]string // Map of meta data tags
AddMeta(key, value string) // Add a meta data tag AddMeta(key, value string) // Add a meta data tag
GetMeta(key string) (value string, ok bool) // Get a meta data tab addressed by its key GetMeta(key string) (value string, ok bool) // Get a meta data tab addressed by its key
HasMeta(key string) (ok bool) // Check a meta data tag HasMeta(key string) (ok bool) // Check if a meta data key is present
RemoveMeta(key string) // Remove a meta data tag by its key RemoveMeta(key string) // Remove a meta data tag by its key
Fields() map[string]interface{} // Map of fields Fields() map[string]interface{} // Map of fields
@ -52,14 +52,12 @@ type CCMetric interface {
RemoveField(key string) // Remove a field addressed by its key RemoveField(key string) // Remove a field addressed by its key
} }
// Meta returns the meta data tags as key-value mapping
func (m *ccMetric) Meta() map[string]string {
return m.meta
}
// String implements the stringer interface for data type ccMetric // String implements the stringer interface for data type ccMetric
func (m *ccMetric) String() string { func (m *ccMetric) String() string {
return fmt.Sprintf("Name: %s, Tags: %+v, Meta: %+v, fields: %+v, Timestamp: %d", m.name, m.tags, m.meta, m.fields, m.tm.UnixNano()) return fmt.Sprintf(
"Name: %s, Tags: %+v, Meta: %+v, fields: %+v, Timestamp: %d",
m.name, m.tags, m.meta, m.fields, m.tm.UnixNano(),
)
} }
// ToLineProtocol generates influxDB line protocol for data type ccMetric // ToLineProtocol generates influxDB line protocol for data type ccMetric
@ -94,20 +92,11 @@ func (m *ccMetric) Name() string {
return m.name return m.name
} }
// SetName sets the measurement name
func (m *ccMetric) SetName(name string) { func (m *ccMetric) SetName(name string) {
m.name = name m.name = name
} }
// Tags returns the the list of tags as key-value-mapping
func (m *ccMetric) Tags() map[string]string {
return m.tags
}
// Fields returns the list of fields as key-value-mapping
func (m *ccMetric) Fields() map[string]interface{} {
return m.fields
}
// Time returns timestamp // Time returns timestamp
func (m *ccMetric) Time() time.Time { func (m *ccMetric) Time() time.Time {
return m.tm return m.tm
@ -118,10 +107,14 @@ func (m *ccMetric) SetTime(t time.Time) {
m.tm = t m.tm = t
} }
// HasTag checks if a tag with key equal to <key> is present in the list of tags // Tags returns the the list of tags as key-value-mapping
func (m *ccMetric) HasTag(key string) bool { func (m *ccMetric) Tags() map[string]string {
_, ok := m.tags[key] return m.tags
return ok }
// AddTag adds a tag (consisting of key and value) to the map of tags
func (m *ccMetric) AddTag(key, value string) {
m.tags[key] = value
} }
// GetTag returns the tag with tag's key equal to <key> // GetTag returns the tag with tag's key equal to <key>
@ -130,22 +123,25 @@ func (m *ccMetric) GetTag(key string) (string, bool) {
return value, ok return value, ok
} }
// HasTag checks if a tag with key equal to <key> is present in the list of tags
func (m *ccMetric) HasTag(key string) bool {
_, ok := m.tags[key]
return ok
}
// RemoveTag removes the tag with tag's key equal to <key> // RemoveTag removes the tag with tag's key equal to <key>
// and keeps the tag list ordered by the keys
func (m *ccMetric) RemoveTag(key string) { func (m *ccMetric) RemoveTag(key string) {
delete(m.tags, key) delete(m.tags, key)
} }
// AddTag adds a tag (consisting of key and value) // Meta returns the meta data tags as key-value mapping
// and keeps the tag list ordered by the keys func (m *ccMetric) Meta() map[string]string {
func (m *ccMetric) AddTag(key, value string) { return m.meta
m.tags[key] = value
} }
// HasTag checks if a meta data tag with meta data's key equal to <key> is present in the list of meta data tags // AddMeta adds a meta data tag (consisting of key and value) to the map of meta data tags
func (m *ccMetric) HasMeta(key string) bool { func (m *ccMetric) AddMeta(key, value string) {
_, ok := m.meta[key] m.meta[key] = value
return ok
} }
// GetMeta returns the meta data tag with meta data's key equal to <key> // GetMeta returns the meta data tag with meta data's key equal to <key>
@ -154,19 +150,23 @@ func (m *ccMetric) GetMeta(key string) (string, bool) {
return value, ok return value, ok
} }
// HasMeta checks if a meta data tag with meta data's key equal to <key> is present in the map of meta data tags
func (m *ccMetric) HasMeta(key string) bool {
_, ok := m.meta[key]
return ok
}
// RemoveMeta removes the meta data tag with tag's key equal to <key> // RemoveMeta removes the meta data tag with tag's key equal to <key>
// and keeps the meta data tag list ordered by the keys
func (m *ccMetric) RemoveMeta(key string) { func (m *ccMetric) RemoveMeta(key string) {
delete(m.meta, key) delete(m.meta, key)
} }
// AddMeta adds a meta data tag (consisting of key and value) // Fields returns the list of fields as key-value-mapping
// and keeps the meta data list ordered by the keys func (m *ccMetric) Fields() map[string]interface{} {
func (m *ccMetric) AddMeta(key, value string) { return m.fields
m.meta[key] = value
} }
// AddField adds a field (consisting of key and value) to the unordered list of fields // AddField adds a field (consisting of key and value) to the map of fields
func (m *ccMetric) AddField(key string, value interface{}) { func (m *ccMetric) AddField(key string, value interface{}) {
m.fields[key] = value m.fields[key] = value
} }
@ -177,14 +177,14 @@ func (m *ccMetric) GetField(key string) (interface{}, bool) {
return v, ok return v, ok
} }
// HasField checks if a field with field's key equal to <key> is present in the list of fields // HasField checks if a field with field's key equal to <key> is present in the map of fields
func (m *ccMetric) HasField(key string) bool { func (m *ccMetric) HasField(key string) bool {
_, ok := m.fields[key] _, ok := m.fields[key]
return ok return ok
} }
// RemoveField removes the field with field's key equal to <key> // RemoveField removes the field with field's key equal to <key>
// from the unordered list of fields // from the map of fields
func (m *ccMetric) RemoveField(key string) { func (m *ccMetric) RemoveField(key string) {
delete(m.fields, key) delete(m.fields, key)
} }
@ -205,17 +205,13 @@ func New(
tm: tm, tm: tm,
} }
// deep copy tags // deep copy tags, meta data tags and fields
for k, v := range tags { for k, v := range tags {
m.tags[k] = v m.tags[k] = v
} }
// deep copy meta data tags
for k, v := range meta { for k, v := range meta {
m.meta[k] = v m.meta[k] = v
} }
// Unsorted list of fields
for k, v := range fields { for k, v := range fields {
v := convertField(v) v := convertField(v)
if v == nil { if v == nil {
@ -231,12 +227,13 @@ func New(
func FromMetric(other ccMetric) CCMetric { func FromMetric(other ccMetric) CCMetric {
m := &ccMetric{ m := &ccMetric{
name: other.Name(), name: other.Name(),
tags: make(map[string]string), tags: make(map[string]string, len(other.tags)),
meta: make(map[string]string), meta: make(map[string]string, len(other.meta)),
fields: make(map[string]interface{}), fields: make(map[string]interface{}, len(other.fields)),
tm: other.Time(), tm: other.Time(),
} }
// deep copy tags, meta data tags and fields
for key, value := range other.tags { for key, value := range other.tags {
m.tags[key] = value m.tags[key] = value
} }
@ -259,6 +256,7 @@ func FromInfluxMetric(other lp.Metric) CCMetric {
tm: other.Time(), tm: other.Time(),
} }
// deep copy tags and fields
for _, otherTag := range other.TagList() { for _, otherTag := range other.TagList() {
m.tags[otherTag.Key] = otherTag.Value m.tags[otherTag.Key] = otherTag.Value
} }