-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitor.go
69 lines (60 loc) · 1.51 KB
/
monitor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
import (
"encoding/json"
"time"
)
type Monitor interface {
GetVariables() []string
GetValues([]string) map[string]interface{}
}
var monitorDrivers = make(map[string]func(*json.RawMessage) Monitor)
func AddMonitorDriver(monitor string, constructor func(*json.RawMessage) Monitor) {
monitorDrivers[monitor] = constructor
}
type MonitorTrack struct {
Variables map[string]*MonitorTrackVariable
Interval int
timer *time.Timer
}
func newMonitorTrack() *MonitorTrack {
return &MonitorTrack{
Variables: make(map[string]*MonitorTrackVariable),
}
}
type MonitorTrackVariable struct {
History int
Data []interface{}
}
func (mt *MonitorTrack) SetTrack(variable string, history int) {
track, ok := mt.Variables[variable]
if !ok && history > 0 {
track = &MonitorTrackVariable{}
mt.Variables[variable] = track
}
if history == 0 && ok {
delete(mt.Variables, variable)
return
}
track.History = history
}
func (mt *MonitorTrack) Start(monitor Monitor) {
go func() {
mt.timer = time.NewTimer(time.Duration(1) * time.Second)
for _ = range mt.timer.C {
if mt.Interval > 0 {
mt.timer.Reset(time.Second * time.Duration(mt.Interval))
}
variables := []string{}
for variable := range mt.Variables {
variables = append(variables, variable)
}
values := monitor.GetValues(variables)
for variable, vt := range mt.Variables {
vt.Data = append(vt.Data, values[variable])
if len(vt.Data) > vt.History {
vt.Data = vt.Data[len(vt.Data)-vt.History:]
}
}
}
}()
}