add dbm new model and fixed json_data

This commit is contained in:
Adrian Zürcher
2025-05-12 17:12:25 +02:00
parent 5ee97416dd
commit 836a69f914
14 changed files with 260 additions and 167 deletions

190
models/dbm.go Normal file
View File

@@ -0,0 +1,190 @@
package models
import (
"fmt"
"runtime"
"time"
"maps"
"github.com/google/uuid"
serverModels "github.com/tecamino/tecamino-dbm/server/models"
"github.com/tecamino/tecamino-dbm/utils"
json_dataModels "github.com/tecamino/tecamino-json_data/models"
"github.com/tecamino/tecamino-logger/logging"
)
type DBM struct {
Datapoints Datapoint
Uuids Uuids
Conns *serverModels.Connections
Log *logging.Logger
}
var SystemDatapoints uuid.UUID
func NewDBM(conns *serverModels.Connections, log *logging.Logger) *DBM {
return &DBM{
Datapoints: Datapoint{},
Uuids: make(Uuids),
Conns: conns,
Log: log,
}
}
func (d *DBM) CreateDatapoints(sets ...json_dataModels.Set) ([]json_dataModels.Set, error) {
if len(sets) == 0 {
return nil, nil
}
dps, uuids, err := d.Datapoints.CreateDatapoints(d.Conns, sets...)
//save uuid in seperate map for fast look up
maps.Copy(d.Uuids, uuids)
if err != nil {
return nil, err
}
var ndp uint64
for _, dp := range dps {
if !dp.Updated {
ndp++
}
}
d.ModifyCountedDatapoints(ndp, false)
return dps, nil
}
func (d *DBM) ImportDatapoints(dps ...Datapoint) error {
for _, dp := range dps {
uuids, err := d.Datapoints.ImportDatapoint(d.Conns, dp, dp.Path)
if err != nil {
return err
}
maps.Copy(d.Uuids, uuids)
d.ModifyCountedDatapoints(1, false)
}
return nil
}
func (d *DBM) UpdateDatapointValue(value any, uid uuid.UUID, path ...string) error {
if uid != uuid.Nil {
if _, ok := d.Uuids[uid]; !ok {
return fmt.Errorf("uuid %s not found", uid.String())
}
dp := d.Uuids[uid]
dp.Value = dp.Type.ConvertValue(value)
dp.UpdateDateTime = time.Now().UnixMilli()
dp.Publish(d.Conns, OnChange)
}
if len(path) > 1 {
return fmt.Errorf("only one path allowed")
}
return d.Datapoints.UpdateDatapointValue(d.Conns, value, path[0])
}
func (d *DBM) RemoveDatapoint(sets ...json_dataModels.Set) ([]json_dataModels.Set, error) {
var lsRemoved []json_dataModels.Set
for _, set := range sets {
removed, err := d.Datapoints.RemoveDatapoint(d.Conns, set)
if err != nil {
return lsRemoved, err
}
lsRemoved = append(lsRemoved, removed)
d.ModifyCountedDatapoints(1, true)
}
return lsRemoved, nil
}
func (d *DBM) QueryDatapoints(depth uint, uid uuid.UUID, key ...string) []*Datapoint {
if uid != uuid.Nil {
if _, ok := d.Uuids[uid]; !ok {
return nil
}
dp := d.Uuids[uid]
dps := []*Datapoint{dp}
return append(dps, dp.QueryDatapoints(depth, key[0])...)
}
return d.Datapoints.QueryDatapoints(depth, key[0])
}
func (d *DBM) GetAllDatapoints(depth uint) (dps Datapoints) {
return d.Datapoints.GetAllDatapoints(0)
}
func (d *DBM) GetNumbersOfDatapoints() uint64 {
return utils.Uint64From(d.Datapoints.Datapoints["System"].Datapoints["Datapoints"].Value)
}
func (d *DBM) ModifyCountedDatapoints(count uint64, countDown bool) {
dp := d.QueryDatapoints(1, SystemDatapoints, "System:Datapoints")
amount := dp[0].GetValueUint64()
if countDown {
amount -= count
} else {
amount += count
}
d.UpdateDatapointValue(amount, SystemDatapoints, "System:Datapoints")
}
func (d *DBM) GoSystemTime() error {
path := "System:Time"
var tOld int64
typ := json_dataModels.STR
rights := json_dataModels.Read
_, err := d.CreateDatapoints(json_dataModels.Set{Path: path, Type: typ, Rights: rights})
if err != nil {
d.Log.Error("system.GoSystemTime", err.Error())
return err
}
go func() {
for {
t := time.Now().UnixMilli()
if tOld != t {
if er := d.UpdateDatapointValue(time.UnixMilli(t).Format("2006-01-02 15:04:05"), uuid.Nil, path); er != nil {
d.Log.Error("dmb.Handler.AddSystemDps.UpdateDatapointValue", er.Error())
}
tOld = t
}
time.Sleep(time.Second)
}
}()
return nil
}
func (d *DBM) GoSystemMemory() error {
path := "System:UsedMemory"
var m runtime.MemStats
var mOld uint64
typ := json_dataModels.STR
rights := json_dataModels.Read
_, err := d.CreateDatapoints(json_dataModels.Set{Path: path, Type: typ, Rights: rights})
if err != nil {
d.Log.Error("system.GoSystemMemory", err.Error())
return err
}
go func() {
for {
runtime.ReadMemStats(&m)
if m.Sys != mOld {
mem := fmt.Sprintf("%.2f MB", float64(m.Sys)/1024/1024)
if er := d.UpdateDatapointValue(mem, uuid.Nil, path); er != nil {
d.Log.Error("dmb.Handler.AddSystemDps.UpdateDatapointValue", er.Error())
}
mOld = m.Sys
}
time.Sleep(time.Second)
}
}()
return nil
}