database
This commit is contained in:
41
src/udi/database/abstract_database.go
Normal file
41
src/udi/database/abstract_database.go
Normal 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"`
|
||||||
|
}
|
||||||
|
|
||||||
|
|
28
src/udi/database/database.go
Normal file
28
src/udi/database/database.go
Normal 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)
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
41
src/udi/database/schema.sql
Normal file
41
src/udi/database/schema.sql
Normal 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');
|
||||||
|
|
||||||
|
|
||||||
|
|
Reference in New Issue
Block a user