Skip to content

Commit

Permalink
issues #64 technical debt
Browse files Browse the repository at this point in the history
  • Loading branch information
e154 committed Aug 14, 2022
1 parent 2c4b9ef commit 3894b67
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 60 deletions.
2 changes: 1 addition & 1 deletion adaptors/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 4 additions & 0 deletions models/attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions plugins/cpuspeed/actor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion plugins/hdd/actor.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func NewActor(entity *m.Entity,
AttrMu: &sync.RWMutex{},
Attrs: NewAttr(),
Manager: entityManager,
Metric: entity.Metrics,
},
eventBus: eventBus,
updateLock: &sync.Mutex{},
Expand Down Expand Up @@ -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(),
Expand Down
13 changes: 12 additions & 1 deletion plugins/logs/actor.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package logs

import (
"fmt"
m "github.com/e154/smart-home/models"
"sync"

"github.com/rcrowley/go-metrics"
Expand Down Expand Up @@ -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{
Expand All @@ -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
}

Expand Down
39 changes: 32 additions & 7 deletions plugins/logs/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions plugins/memory/actor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
14 changes: 10 additions & 4 deletions plugins/memory_app/actor.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package memory_app

import (
"fmt"
m "github.com/e154/smart-home/models"
"runtime"
"sync"
"time"
Expand All @@ -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{
Expand All @@ -55,6 +57,10 @@ func NewActor(entityManager entity_manager.EntityManager,
updateLock: &sync.Mutex{},
}

if entity != nil {
actor.Metric = entity.Metrics
}

return actor
}

Expand Down Expand Up @@ -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,
Expand Down
66 changes: 29 additions & 37 deletions plugins/memory_app/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand All @@ -102,18 +87,25 @@ 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
}
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
Expand Down
39 changes: 36 additions & 3 deletions system/entity_manager/entity_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 3894b67

Please sign in to comment.