Switch fields data type from []*lp.Field to map[string]interface{}

This commit is contained in:
Holger Obermaier 2022-02-07 22:41:31 +01:00
parent 627163d4df
commit fe42e8bb95

View File

@ -19,7 +19,7 @@ type ccMetric struct {
name string // Measurement name name string // Measurement name
meta map[string]string // map of meta data tags meta map[string]string // map of meta data tags
tags map[string]string // map of of tags tags map[string]string // map of of tags
fields []*lp.Field // unordered list of of fields fields map[string]interface{} // map of of fields
tm time.Time // timestamp tm time.Time // timestamp
} }
@ -48,7 +48,6 @@ type CCMetric interface {
HasField(key string) bool // Check if a field key is present HasField(key string) bool // Check if a field key is present
RemoveField(key string) // Remove a field addressed by its key RemoveField(key string) // Remove a field addressed by its key
Fields() map[string]interface{} // Map of fields Fields() map[string]interface{} // Map of fields
FieldList() []*lp.Field // Ordered list of fields
} }
// Meta returns the meta data tags as key-value mapping // Meta returns the meta data tags as key-value mapping
@ -69,7 +68,7 @@ func (m *ccMetric) MetaList() []*lp.Tag {
// 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("%s %v %v %v %d", m.name, m.tags, m.meta, m.Fields(), m.tm.UnixNano()) return fmt.Sprintf("%s %v %v %v %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
@ -83,7 +82,7 @@ func (m *ccMetric) ToLineProtocol(metaAsTags bool) string {
tags[key] = value tags[key] = value
} }
} }
p := influxdb2.NewPoint(m.name, tags, m.Fields(), m.tm) p := influxdb2.NewPoint(m.name, tags, m.fields, m.tm)
return write.PointToLineProtocol(p, time.Nanosecond) return write.PointToLineProtocol(p, time.Nanosecond)
} }
@ -113,17 +112,16 @@ func (m *ccMetric) TagList() []*lp.Tag {
// Fields returns the list of fields as key-value-mapping // Fields returns the list of fields as key-value-mapping
func (m *ccMetric) Fields() map[string]interface{} { func (m *ccMetric) Fields() map[string]interface{} {
fields := make(map[string]interface{}, len(m.fields)) return m.fields
for _, field := range m.fields {
fields[field.Key] = field.Value
}
return fields
} }
// FieldList returns the list of fields // FieldList returns the list of fields
func (m *ccMetric) FieldList() []*lp.Field { func (m *ccMetric) FieldList() []*lp.Field {
return m.fields fieldList := make([]*lp.Field, 0, len(m.fields))
for key, value := range m.fields {
fieldList = append(fieldList, &lp.Field{Key: key, Value: value})
}
return fieldList
} }
// Time returns timestamp // Time returns timestamp
@ -186,46 +184,25 @@ func (m *ccMetric) AddMeta(key, value string) {
// 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 unordered list of fields
func (m *ccMetric) AddField(key string, value interface{}) { func (m *ccMetric) AddField(key string, value interface{}) {
for i, field := range m.fields { m.fields[key] = value
if key == field.Key {
m.fields[i] = &lp.Field{Key: key, Value: convertField(value)}
return
}
}
m.fields = append(m.fields, &lp.Field{Key: key, Value: convertField(value)})
} }
// GetField returns the field with field's key equal to <key> // GetField returns the field with field's key equal to <key>
func (m *ccMetric) GetField(key string) (interface{}, bool) { func (m *ccMetric) GetField(key string) (interface{}, bool) {
for _, field := range m.fields { v, ok := m.fields[key]
if field.Key == key { return v, ok
return field.Value, true
}
}
return "", false
} }
// 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 list of fields
func (m *ccMetric) HasField(key string) bool { func (m *ccMetric) HasField(key string) bool {
for _, field := range m.fields { _, ok := m.fields[key]
if field.Key == key { return ok
return true
}
}
return false
} }
// 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 unordered list of fields
func (m *ccMetric) RemoveField(key string) { func (m *ccMetric) RemoveField(key string) {
for i, field := range m.fields { delete(m.fields, key)
if field.Key == key {
copy(m.fields[i:], m.fields[i+1:])
m.fields[len(m.fields)-1] = nil
m.fields = m.fields[:len(m.fields)-1]
return
}
}
} }
// New creates a new measurement point // New creates a new measurement point
@ -240,7 +217,7 @@ func New(
name: name, name: name,
tags: make(map[string]string, len(tags)), tags: make(map[string]string, len(tags)),
meta: make(map[string]string, len(meta)), meta: make(map[string]string, len(meta)),
fields: make([]*lp.Field, 0, len(fields)), fields: make(map[string]interface{}, len(fields)),
tm: tm, tm: tm,
} }
@ -260,7 +237,7 @@ func New(
if v == nil { if v == nil {
continue continue
} }
m.AddField(k, v) m.fields[k] = v
} }
return m, nil return m, nil
@ -271,20 +248,19 @@ 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),
fields: make([]*lp.Field, len(other.FieldList())),
meta: make(map[string]string), meta: make(map[string]string),
fields: make(map[string]interface{}),
tm: other.Time(), tm: other.Time(),
} }
for key, value := range other.Tags() { for key, value := range other.tags {
m.tags[key] = value m.tags[key] = value
} }
for key, value := range other.Meta() { for key, value := range other.meta {
m.meta[key] = value m.meta[key] = value
} }
for key, value := range other.fields {
for i, field := range other.FieldList() { m.fields[key] = value
m.fields[i] = &lp.Field{Key: field.Key, Value: field.Value}
} }
return m return m
} }
@ -294,20 +270,16 @@ func FromInfluxMetric(other lp.Metric) CCMetric {
m := &ccMetric{ m := &ccMetric{
name: other.Name(), name: other.Name(),
tags: make(map[string]string), tags: make(map[string]string),
fields: make([]*lp.Field, len(other.FieldList())),
meta: make(map[string]string), meta: make(map[string]string),
fields: make(map[string]interface{}),
tm: other.Time(), tm: other.Time(),
} }
for _, otherTag := range other.TagList() { for _, otherTag := range other.TagList() {
m.tags[otherTag.Key] = otherTag.Value m.tags[otherTag.Key] = otherTag.Value
} }
for _, otherField := range other.FieldList() {
for i, otherField := range other.FieldList() { m.fields[otherField.Key] = otherField.Value
m.fields[i] = &lp.Field{
Key: otherField.Key,
Value: otherField.Value,
}
} }
return m return m
} }