This commit is contained in:
2023-12-01 09:32:45 +01:00
parent afbc3d3454
commit aa062a79ab
3 changed files with 110 additions and 0 deletions

View File

@ -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"`
}

View File

@ -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)
}

View File

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