package dbm import ( "fmt" "runtime" "time" "github.com/tecamino/tecamino-dbm/utils" json_dataModels "github.com/tecamino/tecamino-json_data/models" ) func (d *DBMHandler) AddSystemDps() (err error) { path := "System:Datapoints" 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}) 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) if err = d.GoSystemTime(); err != nil { return err } if err = d.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 }