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

View File

@@ -87,10 +87,13 @@ func (d *Datapoint) GetValueUint64() uint64 {
return utils.Uint64From(d.Value)
}
func (d *Datapoint) CreateDatapoints(conns *serverModels.Connections, sets ...json_dataModels.Set) (created []json_dataModels.Set, err error) {
func (d *Datapoint) CreateDatapoints(conns *serverModels.Connections, sets ...json_dataModels.Set) (created []json_dataModels.Set, uuids Uuids, err error) {
if len(sets) == 0 {
return
}
uuids = make(Uuids, 1)
for _, dp := range sets {
parts := strings.Split(dp.Path, ":")
@@ -105,7 +108,7 @@ func (d *Datapoint) CreateDatapoints(conns *serverModels.Connections, sets ...js
if existing, ok := current.Datapoints[part]; ok {
publish, err := existing.Set("", dp)
if err != nil {
return nil, err
return nil, nil, err
}
created = append(created, json_dataModels.Set{
Uuid: existing.Uuid,
@@ -130,7 +133,7 @@ func (d *Datapoint) CreateDatapoints(conns *serverModels.Connections, sets ...js
current.Datapoints[part] = &ndp
publish, err := ndp.Set(strings.Join(parts, ":"), dp)
if err != nil {
return nil, err
return nil, nil, err
}
created = append(created, json_dataModels.Set{
Uuid: ndp.Uuid,
@@ -143,6 +146,8 @@ func (d *Datapoint) CreateDatapoints(conns *serverModels.Connections, sets ...js
if publish {
current.Publish(conns, OnChange)
}
//add uuid to flat map for faster lookuo
uuids[ndp.Uuid] = &ndp
}
}
@@ -173,15 +178,20 @@ func (d *Datapoint) CreateDatapoints(conns *serverModels.Connections, sets ...js
current.Datapoints[part] = newDp
current = newDp
//add uuid to flat map for faster lookuo
uuids[newDp.Uuid] = newDp
}
}
}
return
}
func (d *Datapoint) ImportDatapoint(conns *serverModels.Connections, dp Datapoint, path string) error {
func (d *Datapoint) ImportDatapoint(conns *serverModels.Connections, dp Datapoint, path string) (uuids Uuids, err error) {
parts := strings.Split(dp.Path, ":")
uuids = make(Uuids, 1)
current := d
for i, part := range parts {
if current.Datapoints == nil {
@@ -202,9 +212,12 @@ func (d *Datapoint) ImportDatapoint(conns *serverModels.Connections, dp Datapoin
dp.UpdateDateTime = time.Now().UnixMilli()
dp.Subscriptions = InitSubscribtion()
current.Datapoints[part] = &dp
//add uuid to flat map for faster lookuo
uuids[dp.Uuid] = &dp
dp.Publish(conns, OnChange)
}
return nil
return uuids, nil
}
// Traverse or create intermediate nodes
@@ -220,9 +233,11 @@ func (d *Datapoint) ImportDatapoint(conns *serverModels.Connections, dp Datapoin
newDp.ReadWrite = newDp.ReadWrite.GetRights()
current.Datapoints[part] = newDp
current = newDp
//add uuid to flat map for faster lookuo
uuids[newDp.Uuid] = newDp
}
}
return nil
return uuids, nil
}
func (d *Datapoint) UpdateDatapointValue(conns *serverModels.Connections, value any, path string) error {

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
}

5
models/uuids.go Normal file
View File

@@ -0,0 +1,5 @@
package models
import "github.com/google/uuid"
type Uuids map[uuid.UUID]*Datapoint