fields and tags

This commit is contained in:
2026-02-04 21:32:32 +01:00
parent a1ea1b230e
commit 5b46ecb0b1
2 changed files with 14 additions and 57 deletions

View File

@@ -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,

View File

@@ -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');