Skip to content

Commit

Permalink
Merge pull request #199 from e154/develop
Browse files Browse the repository at this point in the history
issues #64 entity import (#198)
  • Loading branch information
e154 authored Aug 20, 2022
2 parents 1aefdc2 + b02a5a5 commit 21a68e4
Show file tree
Hide file tree
Showing 36 changed files with 195 additions and 635 deletions.
1 change: 1 addition & 0 deletions adaptors/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package adaptors

import (
"encoding/json"

"github.com/e154/smart-home/common"
"github.com/e154/smart-home/db"
m "github.com/e154/smart-home/models"
Expand Down
1 change: 1 addition & 0 deletions api/dto/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package dto

import (
"fmt"

"github.com/e154/smart-home/api/stub/api"
"github.com/e154/smart-home/common"
m "github.com/e154/smart-home/models"
Expand Down
5 changes: 3 additions & 2 deletions api/stub/api/backup.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion bin/docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ FROM golang:1.18 as build
RUN update-ca-certificates
MAINTAINER Filippov Alex <[email protected]>

FROM scratch
FROM postgres:11
COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=build /etc/passwd /etc/passwd
ENTRYPOINT ["/server"]
Expand Down
1 change: 1 addition & 0 deletions endpoint/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package endpoint

import (
"context"

"github.com/go-playground/validator/v10"
"github.com/jinzhu/copier"
"github.com/pkg/errors"
Expand Down
1 change: 0 additions & 1 deletion endpoint/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ func (n *EntityEndpoint) Add(ctx context.Context, entity *m.Entity) (result *m.E
func (n *EntityEndpoint) Import(ctx context.Context, entity *m.Entity) (err error) {
// import scripts


err = n.adaptors.Entity.Import(entity)
return
}
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ require (
)

require (
github.com/go-co-op/gocron v1.16.2
github.com/go-playground/locales v0.14.0
github.com/go-playground/universal-translator v0.18.0
github.com/google/uuid v1.3.0
github.com/iancoleman/strcase v0.2.0
github.com/jackc/pgerrcode v0.0.0-20220416144525-469b46aa5efa
github.com/robfig/cron/v3 v3.0.1
github.com/shirou/gopsutil/v3 v3.22.4
)

Expand Down Expand Up @@ -102,9 +102,9 @@ require (
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/procfs v0.0.8 // indirect
github.com/robfig/cron/v3 v3.0.1 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.8.0 // indirect
github.com/tklauser/go-sysconf v0.3.10 // indirect
github.com/tklauser/numcpus v0.4.0 // indirect
github.com/ugorji/go/codec v1.2.6 // indirect
Expand Down
5 changes: 3 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,6 @@ github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm
github.com/gin-gonic/gin v1.7.4 h1:QmUZXrvJ9qZ3GfWvQ+2wnW/1ePrTEJqPKMYEU3lD/DM=
github.com/gin-gonic/gin v1.7.4/go.mod h1:jD2toBW3GZUr5UMcdrwQA10I7RuaFOl/SGeDjXkfUtY=
github.com/gliderlabs/ssh v0.1.1/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0=
github.com/go-co-op/gocron v1.16.2 h1:p9ghzsN5PqqPyWXYDO2JlvD1DOUNT8pPSyGYC62XBcY=
github.com/go-co-op/gocron v1.16.2/go.mod h1:W/N9G7bntRo5fVQlmjncvqSt74jxCxHfjyHlgcB33T8=
github.com/go-errors/errors v1.0.1/go.mod h1:f4zRHt4oKfwPJE5k8C9vpYG+aDHdBFUsgrm6/TyX73Q=
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
Expand Down Expand Up @@ -585,6 +583,7 @@ github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/y
github.com/spf13/viper v1.8.1/go.mod h1:o0Pch8wJ9BVSWGQMbra6iw0oQ5oktSIBaujf1rJH9Ns=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
Expand All @@ -593,6 +592,7 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/surge/glog v0.0.0-20141108051140-2578deb2b95c h1:cVA8Fd14+bmcDyVutgf976DrV9RzNO4SMzUQmfJDMrw=
github.com/surge/glog v0.0.0-20141108051140-2578deb2b95c/go.mod h1:W6gI0HQAbNyEO/62hesTBIbabSGJaEdlUApLw8UtuB0=
Expand Down Expand Up @@ -1093,6 +1093,7 @@ gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
grpc.go4.org v0.0.0-20170609214715-11d0a25b4919/go.mod h1:77eQGdRu53HpSqPFJFmuJdjuHRquDANNeA4x7B8WQ9o=
honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
9 changes: 8 additions & 1 deletion plugins/cpuspeed/actor.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"fmt"
"sync"

m "github.com/e154/smart-home/models"

"github.com/e154/smart-home/common/events"

"github.com/rcrowley/go-metrics"
Expand Down Expand Up @@ -49,7 +51,8 @@ type Actor struct {

// NewActor ...
func NewActor(entityManager entity_manager.EntityManager,
eventBus bus.Bus) *Actor {
eventBus bus.Bus,
entity *m.Entity) *Actor {

actor := &Actor{
BaseActor: entity_manager.BaseActor{
Expand All @@ -75,6 +78,10 @@ func NewActor(entityManager entity_manager.EntityManager,
actor.model = cpuInfo[0].Model
}

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

return actor
}

Expand Down
83 changes: 38 additions & 45 deletions plugins/cpuspeed/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@
package cpuspeed

import (
"fmt"
"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 All @@ -35,9 +36,9 @@ func init() {

type plugin struct {
*plugins.Plugin
quit chan struct{}
pause uint
actor *Actor
ticker *time.Ticker
pause uint
actor *Actor
}

// New ...
Expand All @@ -54,55 +55,37 @@ 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)
// Unload ...
func (p *plugin) Unload() (err error) {
if err = p.Plugin.Unload(); err != nil {
return
}
return p.unload()
}

list, _, err := p.Adaptors.Metric.Search("cpuspeed", 1, 0)
if err != nil {
log.Error(err.Error())
// load ...
func (p *plugin) load() (err error) {

if p.actor != nil {
return
}

var metric *m.Metric
if len(list) == 0 {
metric = &m.Metric{
Name: "cpuspeed",
Description: "Cpu metric",
Options: m.MetricOptions{
Items: []m.MetricOptionsItem{
{
Name: "all",
Description: "",
Color: "#00ff00",
Translate: "all",
Label: "GHz",
},
},
},
Type: common.MetricTypeLine,
}
if metric.Id, err = p.Adaptors.Metric.Add(metric); err == nil {
_ = p.Adaptors.Entity.AppendMetric(p.actor.Id, metric)
}
var entity *m.Entity
if entity, err = p.Adaptors.Entity.GetById(common.EntityId(fmt.Sprintf("%s.%s", EntityCpuspeed, 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() {
ticker := time.NewTicker(time.Second * time.Duration(p.pause))
p.quit = make(chan struct{})
defer func() {
ticker.Stop()
close(p.quit)
}()

for {
select {
case <-p.quit:
return
case <-ticker.C:
p.actor.selfUpdate()
}
Expand All @@ -112,15 +95,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
}
p.quit <- struct{}{}
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
2 changes: 0 additions & 2 deletions plugins/logs/actor.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (

"github.com/e154/smart-home/common"
"github.com/e154/smart-home/system/bus"
"github.com/e154/smart-home/system/cron"
"github.com/e154/smart-home/system/entity_manager"
)

Expand All @@ -46,7 +45,6 @@ type Actor struct {
WarnYesterday metrics.Counter
eventBus bus.Bus
updateLock *sync.Mutex
cron *cron.Cron
}

// NewActor ...
Expand Down
23 changes: 6 additions & 17 deletions plugins/logs/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (

"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"
"github.com/e154/smart-home/system/plugins"
)
Expand All @@ -38,16 +37,13 @@ type plugin struct {
*plugins.Plugin
pause uint
actor *Actor
cron *cron.Cron
task *cron.Task
}

// New ...
func New() plugins.Plugable {
p := &plugin{
Plugin: plugins.NewPlugin(),
pause: 10,
cron: cron.NewCron(),
}
return p
}
Expand All @@ -65,6 +61,10 @@ func (p *plugin) Unload() (err error) {
if err = p.Plugin.Unload(); err != nil {
return
}
// every day at 00:00 am
p.Scheduler.AddFunc("0 0 * * *", func() {
p.actor.UpdateDay()
})
return p.unload()
}

Expand All @@ -81,22 +81,11 @@ func (p *plugin) load(service plugins.Service) (err error) {

logging.LogsHook = p.actor.LogsHook

// Spawn ...
p.task, _ = p.cron.NewTask("0 0 0 * * *", func() {
p.actor.UpdateDay()
})
p.cron.Run()

return nil
return
}

func (p *plugin) unload() (err error) {
if p.task != nil {
p.cron.RemoveTask(p.task)
}
p.cron.Stop()
p.task = nil
return nil
return
}

// AddOrUpdateActor ...
Expand Down
9 changes: 8 additions & 1 deletion plugins/memory/actor.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"fmt"
"sync"

m "github.com/e154/smart-home/models"

"github.com/e154/smart-home/common/events"

"github.com/rcrowley/go-metrics"
Expand All @@ -48,7 +50,8 @@ type Actor struct {

// NewActor ...
func NewActor(entityManager entity_manager.EntityManager,
eventBus bus.Bus) *Actor {
eventBus bus.Bus,
entity *m.Entity) *Actor {

actor := &Actor{
BaseActor: entity_manager.BaseActor{
Expand All @@ -67,6 +70,10 @@ func NewActor(entityManager entity_manager.EntityManager,
updateLock: &sync.Mutex{},
}

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

return actor
}

Expand Down
Loading

0 comments on commit 21a68e4

Please sign in to comment.