From 5b46ecb0b1b4c74a85d41c5fa355a1c15652630c Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Wed, 4 Feb 2026 21:32:32 +0100 Subject: [PATCH] fields and tags --- src/udi/database/database.go | 30 ++++++++++++-------------- src/udi/database/schema.sql | 41 ------------------------------------ 2 files changed, 14 insertions(+), 57 deletions(-) delete mode 100644 src/udi/database/schema.sql diff --git a/src/udi/database/database.go b/src/udi/database/database.go index 26e7897..5ff3786 100644 --- a/src/udi/database/database.go +++ b/src/udi/database/database.go @@ -81,22 +81,11 @@ func (self *DatabaseHandle) StoreMeasurement(measurement *Measurement) { } // Build tags - tags := map[string]string{ - "application": measurement.Application, - } + tags := make(map[string]string) if measurement.Device != "" { tags["device"] = measurement.Device } - // Add attributes as tags - for key, value := range measurement.Attributes { - if strValue, ok := value.(string); ok { - tags[key] = strValue - } else { - tags[key] = fmt.Sprintf("%v", value) - } - } - // Build fields from Values fields := make(map[string]interface{}) for key, varType := range measurement.Values { @@ -107,13 +96,22 @@ func (self *DatabaseHandle) StoreMeasurement(measurement *Measurement) { if varType.Unit != "" { fields[key+"_unit"] = varType.Unit } - if varType.Variable != "" { - fields[key+"_variable"] = varType.Variable - } + // This is already the column name, so we can skip it + //if varType.Variable != "" { + // fields[key+"_variable"] = varType.Variable + //} if varType.Status != "" { fields[key+"_status"] = varType.Status } } + // Add attributes as fields + for key, value := range measurement.Attributes { + if strValue, ok := value.(string); ok { + fields[key] = strValue + } else { + fields[key] = fmt.Sprintf("%v", value) + } + } // Ensure we have at least one field if len(fields) == 0 { @@ -124,7 +122,7 @@ func (self *DatabaseHandle) StoreMeasurement(measurement *Measurement) { // Create point pt, err := influxdb.NewPoint( - "observation", + measurement.Application, tags, fields, measurement.Time, diff --git a/src/udi/database/schema.sql b/src/udi/database/schema.sql deleted file mode 100644 index e94fad3..0000000 --- a/src/udi/database/schema.sql +++ /dev/null @@ -1,41 +0,0 @@ -create extension if not exists timescaledb; - -create table application_t ( - id serial not null primary key, - label text not null unique, - attributes jsonb -); - -create table sensor_type_t ( - id serial not null primary key, - label text not null unique, - variable text not null, - unit text not null, - converter text not null, - attributes jsonb -); - -create table sensor_t ( - id serial not null primary key, - label text not null, - application int references application_t(id), - sensor_type int references sensor_type_t(id), - attributes jsonb, - unique (label, application) -); - -create table measurement_t ( - time timestamp without time zone not null, - application text not null, - sensor_type text not null, - sensor text not null, - variable text not null, - unit text not null, - value double precision not null, - attributes jsonb -); - -select create_hypertable('measurement_t', 'time'); - - -