From aa062a79ab982eecadb2af1f77e514769469afd8 Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Fri, 1 Dec 2023 09:32:45 +0100 Subject: [PATCH] database --- src/udi/database/abstract_database.go | 41 +++++++++++++++++++++++++++ src/udi/database/database.go | 28 ++++++++++++++++++ src/udi/database/schema.sql | 41 +++++++++++++++++++++++++++ 3 files changed, 110 insertions(+) create mode 100644 src/udi/database/abstract_database.go create mode 100644 src/udi/database/database.go create mode 100644 src/udi/database/schema.sql diff --git a/src/udi/database/abstract_database.go b/src/udi/database/abstract_database.go new file mode 100644 index 0000000..0bf347a --- /dev/null +++ b/src/udi/database/abstract_database.go @@ -0,0 +1,41 @@ +package database + +import "time" +import "gorm.io/gorm" + +type Measurement struct { + Time time.Time `gorm:"not null;primary_key"` + Application string `gorm:"not null"` + SensorType string `gorm:"not null"` + Sensor string `gorm:"not null"` + Variable string `gorm:"not null"` + Unit string + Value float32 `gorm:"not null"` + Attributes map[string]string `gorm:"serializer:json;type:jsonb"` +} + +type Application struct { + gorm.Model + Label string `gorm:"not null"` + Attributes map[string]string `gorm:"serializer:json;type:jsonb"` +} + +type SensorType struct { + gorm.Model + Label string `gorm:"not null"` + Variable string `gorm:"not null"` + Unit string + Attributes map[string]string `gorm:"serializer:json;type:jsonb"` +} + +type Sensor struct { + gorm.Model + Label string `gorm:"not null"` + ApplicationID int `gorm:"not null"` + Application Application + SensorTypeID int `gorm:"not null"` + SensorType SensorType + Attributes map[string]string `gorm:"serializer:json;type:jsonb"` +} + + diff --git a/src/udi/database/database.go b/src/udi/database/database.go new file mode 100644 index 0000000..e8cf7c5 --- /dev/null +++ b/src/udi/database/database.go @@ -0,0 +1,28 @@ +package database + +import ( + "log" + "time" + "gorm.io/driver/postgres" + "gorm.io/gorm" +) + +func Test() { + dsn := "" + db, err := gorm.Open(postgres.Open(dsn)) + log.Printf("Database: %s, %s", db, err) + + db.AutoMigrate(&Application{}) + db.AutoMigrate(&SensorType{}) + db.AutoMigrate(&Sensor{}) + db.AutoMigrate(&Measurement{}) + + n := time.Now() + m := Measurement { Time: n, Application: "bla", SensorType: "bla", Sensor: "bla", Variable: "bla", Unit: "bla", Value: 1.0 } + db.Create(&m) + m = Measurement { Time: n, Application: "bla", SensorType: "bla", Sensor: "bla", Variable: "bla", Unit: "bla", Value: 1.0 } + db.Create(&m) + + +} + diff --git a/src/udi/database/schema.sql b/src/udi/database/schema.sql new file mode 100644 index 0000000..e94fad3 --- /dev/null +++ b/src/udi/database/schema.sql @@ -0,0 +1,41 @@ +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'); + + +