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

@@ -1,11 +1,8 @@
package dbm
import (
"fmt"
"runtime"
"time"
"github.com/tecamino/tecamino-dbm/utils"
"github.com/google/uuid"
"github.com/tecamino/tecamino-dbm/models"
json_dataModels "github.com/tecamino/tecamino-json_data/models"
)
@@ -14,84 +11,19 @@ func (d *DBMHandler) AddSystemDps() (err error) {
typ := json_dataModels.LOU
rights := json_dataModels.Read
_, err = d.DB.CreateDatapoints(d.Conns, json_dataModels.Set{Path: path, Value: 0, Type: typ, Rights: rights})
_, err = d.DBM.CreateDatapoints(json_dataModels.Set{Path: path, Value: 0, Type: typ, Rights: rights})
if err != nil {
d.Log.Error("dmb.Handler.AddSystemDps", err.Error())
return err
}
dp := d.QueryDatapoints(1, path)
d.UpdateDatapointValue(path, dp[0].GetValueUint64()+1)
models.SystemDatapoints = d.DBM.QueryDatapoints(1, uuid.Nil, path)[0].Uuid
if err = d.GoSystemTime(); err != nil {
if err = d.DBM.GoSystemTime(); err != nil {
return err
}
if err = d.GoSystemMemory(); err != nil {
if err = d.DBM.GoSystemMemory(); err != nil {
return err
}
return
}
func (d *DBMHandler) GetNumbersOfDatapoints() uint64 {
return utils.Uint64From(d.DB.Datapoints["System"].Datapoints["Datapoints"].Value)
}
func (d *DBMHandler) GoSystemTime() error {
path := "System:Time"
var tOld int64
typ := json_dataModels.STR
rights := json_dataModels.Read
_, err := d.DB.CreateDatapoints(d.Conns, json_dataModels.Set{Path: path, Type: typ, Rights: rights})
if err != nil {
d.Log.Error("system.GoSystemTime", err.Error())
return err
}
dp := d.QueryDatapoints(1, "System:Datapoints")
d.UpdateDatapointValue("System:Datapoints", dp[0].GetValueUint64()+1)
go func() {
for {
t := time.Now().UnixMilli()
if tOld != t {
if er := d.DB.UpdateDatapointValue(d.Conns, time.UnixMilli(t).Format("2006-01-02 15:04:05"), path); er != nil {
d.Log.Error("dmb.Handler.AddSystemDps.UpdateDatapointValue", er.Error())
}
tOld = t
}
time.Sleep(time.Second)
}
}()
return nil
}
func (d *DBMHandler) GoSystemMemory() error {
path := "System:UsedMemory"
var m runtime.MemStats
var mOld uint64
typ := json_dataModels.STR
rights := json_dataModels.Read
_, err := d.DB.CreateDatapoints(d.Conns, json_dataModels.Set{Path: path, Type: typ, Rights: rights})
if err != nil {
d.Log.Error("system.GoSystemMemory", err.Error())
return err
}
dp := d.QueryDatapoints(1, "System:Datapoints")
d.UpdateDatapointValue("System:Datapoints", dp[0].GetValueUint64()+1)
go func() {
for {
runtime.ReadMemStats(&m)
if m.Sys != mOld {
mem := fmt.Sprintf("%.2f MB", float64(m.Sys)/1024/1024)
if er := d.DB.UpdateDatapointValue(d.Conns, mem, path); er != nil {
d.Log.Error("dmb.Handler.AddSystemDps.UpdateDatapointValue", er.Error())
}
mOld = m.Sys
}
time.Sleep(time.Second)
}
}()
return nil
}