diff --git a/adaptors/entity.go b/adaptors/entity.go index 7438013d1..5ef1d23cd 100644 --- a/adaptors/entity.go +++ b/adaptors/entity.go @@ -397,7 +397,7 @@ func (n *Entity) preloadMetric(ver *m.Entity) { optionItems[i] = item.Name } - if ver.Metrics[i].Data, err = bucketMetricBucketAdaptor.Simple24HPreview(metric.Id, optionItems); err != nil { + if ver.Metrics[i].Data, err = bucketMetricBucketAdaptor.SimpleListWithSoftRange(nil, nil, metric.Id, common.String(common.MetricRange24H.String()), optionItems); err != nil { log.Error(err.Error()) return } diff --git a/models/attribute.go b/models/attribute.go index d044aa9f3..7146cfa20 100644 --- a/models/attribute.go +++ b/models/attribute.go @@ -57,6 +57,10 @@ func (a Attribute) Int64() int64 { } t := reflect.TypeOf(a.Value) switch t.Kind() { + case reflect.Uint64: + return int64(a.Value.(uint64)) + case reflect.Float32: + return int64(a.Value.(float32)) case reflect.Int: return int64(a.Value.(int)) case reflect.Float64: diff --git a/plugins/cpuspeed/actor.go b/plugins/cpuspeed/actor.go index da501e751..1cbacc6fd 100644 --- a/plugins/cpuspeed/actor.go +++ b/plugins/cpuspeed/actor.go @@ -122,9 +122,9 @@ func (u *Actor) selfUpdate() { u.Attrs[AttrLoadMin].Value = u.loadMin.Value() u.AttrMu.Unlock() - u.SetMetric(u.Id, "cpuspeed", map[string]float32{ - "all": common.Rounding32(u.all.Value(), 2), - }) + //u.SetMetric(u.Id, "cpuspeed", map[string]float32{ + // "all": common.Rounding32(u.all.Value(), 2), + //}) u.eventBus.Publish(event_bus.TopicEntities, events.EventStateChanged{ StorageSave: false, diff --git a/plugins/hdd/actor.go b/plugins/hdd/actor.go index 199e1513b..e24aee159 100644 --- a/plugins/hdd/actor.go +++ b/plugins/hdd/actor.go @@ -67,6 +67,7 @@ func NewActor(entity *m.Entity, AttrMu: &sync.RWMutex{}, Attrs: NewAttr(), Manager: entityManager, + Metric: entity.Metrics, }, eventBus: eventBus, updateLock: &sync.Mutex{}, @@ -115,7 +116,6 @@ func (u *Actor) selfUpdate() { u.Attrs[AttrInodesUsedPercent].Value = r.InodesUsedPercent u.AttrMu.Unlock() } - u.eventBus.Publish(event_bus.TopicEntities, events.EventStateChanged{ StorageSave: false, PluginName: u.Id.PluginName(), diff --git a/plugins/logs/actor.go b/plugins/logs/actor.go index c377d6244..7abc0cd0a 100644 --- a/plugins/logs/actor.go +++ b/plugins/logs/actor.go @@ -20,6 +20,7 @@ package logs import ( "fmt" + m "github.com/e154/smart-home/models" "sync" "github.com/rcrowley/go-metrics" @@ -49,7 +50,7 @@ type Actor struct { // NewActor ... func NewActor(entityManager entity_manager.EntityManager, - eventBus event_bus.EventBus) *Actor { + eventBus event_bus.EventBus, entity *m.Entity) *Actor { actor := &Actor{ BaseActor: entity_manager.BaseActor{ @@ -71,6 +72,16 @@ func NewActor(entityManager entity_manager.EntityManager, updateLock: &sync.Mutex{}, } + if entity != nil { + actor.Metric = entity.Metrics + attrs := entity.Attributes + actor.ErrTotal.Inc(attrs[AttrErrTotal].Int64()) + actor.ErrToday.Inc(attrs[AttrErrToday].Int64()) + actor.ErrYesterday.Inc(attrs[AttrErrYesterday].Int64()) + actor.WarnTotal.Inc(attrs[AttrWarnTotal].Int64()) + actor.WarnToday.Inc(attrs[AttrWarnToday].Int64()) + actor.WarnYesterday.Inc(attrs[AttrWarnYesterday].Int64()) + } return actor } diff --git a/plugins/logs/plugin.go b/plugins/logs/plugin.go index 6e3b49ede..bd39acf36 100644 --- a/plugins/logs/plugin.go +++ b/plugins/logs/plugin.go @@ -19,6 +19,8 @@ package logs import ( + "fmt" + "github.com/e154/smart-home/common" m "github.com/e154/smart-home/models" "github.com/e154/smart-home/system/cron" "github.com/e154/smart-home/system/logging" @@ -54,8 +56,26 @@ func (p *plugin) Load(service plugins.Service) (err error) { if err = p.Plugin.Load(service); err != nil { return } + return p.load(service) +} + +// Unload ... +func (p *plugin) Unload() (err error) { + if err = p.Plugin.Unload(); err != nil { + return + } + return p.unload() +} + +// Load ... +func (p *plugin) load(service plugins.Service) (err error) { + + var entity *m.Entity + if entity, err = p.Adaptors.Entity.GetById(common.EntityId(fmt.Sprintf("%s.%s", EntityLogs, Name))); err == nil { - p.actor = NewActor(p.EntityManager, p.EventBus) + } + + p.actor = NewActor(p.EntityManager, p.EventBus, entity) p.EntityManager.Spawn(p.actor.Spawn) logging.LogsHook = p.actor.LogsHook @@ -69,12 +89,7 @@ func (p *plugin) Load(service plugins.Service) (err error) { return nil } -// Unload ... -func (p *plugin) Unload() (err error) { - if err = p.Plugin.Unload(); err != nil { - return - } - +func (p *plugin) unload() (err error) { if p.task != nil { p.cron.RemoveTask(p.task) } @@ -83,6 +98,16 @@ func (p *plugin) Unload() (err error) { return nil } +// AddOrUpdateActor ... +func (p *plugin) AddOrUpdateActor(entity *m.Entity) (err error) { + return p.load(nil) +} + +// RemoveActor ... +func (p *plugin) RemoveActor(entityId common.EntityId) (err error) { + return p.unload() +} + // Name ... func (p plugin) Name() string { return Name diff --git a/plugins/memory/actor.go b/plugins/memory/actor.go index 217867fc2..f283a9518 100644 --- a/plugins/memory/actor.go +++ b/plugins/memory/actor.go @@ -95,9 +95,9 @@ func (u *Actor) selfUpdate() { u.Attrs[AttrUsedPercent].Value = usedPercent u.AttrMu.Unlock() - u.SetMetric(u.Id, "memory", map[string]float32{ - "used_percent": usedPercent, - }) + //u.SetMetric(u.Id, "memory", map[string]float32{ + // "used_percent": usedPercent, + //}) u.eventBus.Publish(event_bus.TopicEntities, events.EventStateChanged{ StorageSave: false, diff --git a/plugins/memory_app/actor.go b/plugins/memory_app/actor.go index 8a523085a..c2cd63583 100644 --- a/plugins/memory_app/actor.go +++ b/plugins/memory_app/actor.go @@ -20,6 +20,7 @@ package memory_app import ( "fmt" + m "github.com/e154/smart-home/models" "runtime" "sync" "time" @@ -39,7 +40,8 @@ type Actor struct { // NewActor ... func NewActor(entityManager entity_manager.EntityManager, - eventBus event_bus.EventBus) *Actor { + eventBus event_bus.EventBus, + entity *m.Entity) *Actor { actor := &Actor{ BaseActor: entity_manager.BaseActor{ @@ -55,6 +57,10 @@ func NewActor(entityManager entity_manager.EntityManager, updateLock: &sync.Mutex{}, } + if entity != nil { + actor.Metric = entity.Metrics + } + return actor } @@ -83,9 +89,9 @@ func (u *Actor) selfUpdate() { u.Attrs[AttrLastGC].Value = time.Unix(0, int64(s.LastGC)) u.AttrMu.Unlock() - u.SetMetric(u.Id, "memory_app", map[string]float32{ - "total_alloc": float32(s.TotalAlloc), - }) + //u.SetMetric(u.Id, "memory_app", map[string]float32{ + // "total_alloc": float32(s.TotalAlloc), + //}) u.eventBus.Publish(event_bus.TopicEntities, events.EventStateChanged{ StorageSave: false, diff --git a/plugins/memory_app/plugin.go b/plugins/memory_app/plugin.go index 657678dc9..5b71e229e 100644 --- a/plugins/memory_app/plugin.go +++ b/plugins/memory_app/plugin.go @@ -19,12 +19,12 @@ package memory_app import ( + "fmt" + "github.com/e154/smart-home/common" "time" - "github.com/e154/smart-home/common" m "github.com/e154/smart-home/models" "github.com/e154/smart-home/system/plugins" - "github.com/prometheus/common/log" ) var _ plugins.Plugable = (*plugin)(nil) @@ -54,42 +54,27 @@ func (p *plugin) Load(service plugins.Service) (err error) { if err = p.Plugin.Load(service); err != nil { return } + return p.load() +} - p.actor = NewActor(service.EntityManager(), service.EventBus()) - p.EntityManager.Spawn(p.actor.Spawn) - - list, _, err := p.Adaptors.Metric.Search("memory_app", 1, 0) - if err != nil { - log.Error(err.Error()) +// Unload ... +func (p *plugin) Unload() (err error) { + if err = p.Plugin.Unload(); err != nil { + return } + return p.unload() +} - var metric *m.Metric - if len(list) == 0 { - metric = &m.Metric{ - Name: "memory_app", - Description: "App metric", - Options: m.MetricOptions{ - Items: []m.MetricOptionsItem{ - { - Name: "total_alloc", - Description: "", - Color: "#0000FF", - Translate: "total_alloc", - Label: "%", - }, - }, - }, - Type: common.MetricTypeLine, - } - if metric.Id, err = p.Adaptors.Metric.Add(metric); err == nil { - _ = p.Adaptors.Entity.AppendMetric(p.actor.Id, metric) - } +// load ... +func (p *plugin) load() (err error) { + + var entity *m.Entity + if entity, err = p.Adaptors.Entity.GetById(common.EntityId(fmt.Sprintf("%s.%s", EntityMemory, Name))); err == nil { - } else { - metric = list[0] } - p.actor.Metric = []*m.Metric{metric} + p.actor = NewActor(p.EntityManager, p.EventBus, entity) + p.EntityManager.Spawn(p.actor.Spawn) go func() { p.ticker = time.NewTicker(time.Second * time.Duration(p.pause)) @@ -102,11 +87,8 @@ func (p *plugin) Load(service plugins.Service) (err error) { return nil } -// Unload ... -func (p *plugin) Unload() (err error) { - if err = p.Plugin.Unload(); err != nil { - return - } +// unload ... +func (p *plugin) unload() (err error) { if p.ticker != nil { p.ticker.Stop() p.ticker = nil @@ -114,6 +96,16 @@ func (p *plugin) Unload() (err error) { return nil } +// AddOrUpdateActor ... +func (p *plugin) AddOrUpdateActor(entity *m.Entity) (err error) { + return p.load() +} + +// RemoveActor ... +func (p *plugin) RemoveActor(entityId common.EntityId) (err error) { + return p.unload() +} + // Name ... func (p plugin) Name() string { return Name diff --git a/system/entity_manager/entity_manager.go b/system/entity_manager/entity_manager.go index 8cbf6b9de..8d763a41e 100644 --- a/system/entity_manager/entity_manager.go +++ b/system/entity_manager/entity_manager.go @@ -130,6 +130,37 @@ func (e *entityManager) Shutdown() { log.Info("Shutdown") } +func (e *entityManager) updateMetric(actor *actorInfo, state event_bus.EventEntityState) { + metrics := actor.Actor.Metrics() + if metrics == nil { + return + } + + var data = make(map[string]float32) + var name string + + for _, metric := range metrics { + for _, prop := range metric.Options.Items { + if value, ok := state.Attributes[prop.Name]; ok { + name = metric.Name + switch value.Type { + case common.AttributeInt: + data[prop.Name] = float32(value.Int64()) + case common.AttributeFloat: + data[prop.Name] = common.Rounding32(value.Float64(), 2) + } + } + } + } + + if len(data) == 0 || name == "" { + return + } + + e.SetMetric(state.EntityId, name, data) + +} + // SetMetric ... func (e *entityManager) SetMetric(id common.EntityId, name string, value map[string]float32) { @@ -354,6 +385,8 @@ func (e *entityManager) eventStateChangedHandler(msg events.EventStateChanged) { } actor := item.(*actorInfo) + go e.updateMetric(actor, msg.NewState) + if msg.NewState.Compare(msg.OldState) { return } @@ -531,12 +564,12 @@ func (e *entityManager) Add(entity *m.Entity) (err error) { return } - var creudActor CrudActor - if creudActor, err = e.getCrudActor(entity.Id); err != nil { + var crudActor CrudActor + if crudActor, err = e.getCrudActor(entity.Id); err != nil { return } - err = creudActor.AddOrUpdateActor(entity) + err = crudActor.AddOrUpdateActor(entity) return }