-
Notifications
You must be signed in to change notification settings - Fork 0
/
live.go
135 lines (114 loc) · 2.97 KB
/
live.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package view
import (
"github.com/peter-mount/piweather.center/store/api"
"github.com/peter-mount/piweather.center/tools/weathercenter/dashboard/model"
"github.com/peter-mount/piweather.center/tools/weathercenter/ws"
"sort"
"sync"
)
type Live struct {
name string // name of dashboard
server *Service // Parent service
dashboard *model.Dashboard // Attached dashboard
websocket *ws.Server // websocket server for a dashboard
js map[string]interface{} // Javascript templates for this dashboard
mutex sync.Mutex
}
func (s *Service) newLiveServer(n string, d *model.Dashboard) *Live {
l := &Live{
name: n,
server: s,
}
l.newDashboard(d)
// Listen for live metric updates sharing the single queue
s.Server.Listener().Add(l.notify)
return l
}
func (s *Live) getDashboard() *model.Dashboard {
s.mutex.Lock()
defer s.mutex.Unlock()
return s.dashboard
}
// return the data required by the templates
func (s *Live) getData() any {
s.mutex.Lock()
defer s.mutex.Unlock()
var js []string
for k, _ := range s.js {
js = append(js, k)
}
sort.SliceStable(js, func(i, j int) bool {
return js[i] < js[j]
})
return map[string]interface{}{
"dash": s.name,
"board": s.dashboard,
"js": js,
}
}
// Set the dashboard for this instance and update the state
func (s *Live) newDashboard(d *model.Dashboard) {
s.mutex.Lock()
defer s.mutex.Unlock()
s.dashboard = d
s.js = make(map[string]interface{})
s.updateJs(d)
s.initDashboard()
// Enable websocket if the dashboard expects live updates
// Note: Changing Dashboard.Live to false will not remove the websocket,
// but this allows us to only create them for those that do need them
if s.websocket == nil && d.Live {
s.websocket = ws.NewServer()
s.server.Rest.HandleFunc("/live/dash/"+s.name, s.websocket.Handle)
go s.websocket.Run()
}
}
// Populate the dashboard with current data
func (s *Live) initDashboard() {
latest := s.server.Server.Latest
r := &model.Response{}
for _, n := range latest.Metrics() {
m, exists := latest.Latest(n)
if exists {
metric := api.Metric{
Metric: n,
Time: m.Time,
Unit: m.Value.Unit().ID(),
Value: m.Value.Float(),
Formatted: m.Value.String(),
}
s.dashboard.Process(metric, r)
}
}
}
// Update the list of javascript templates
func (s *Live) updateJs(c model.Instance) {
t := c.GetType()
n := "dash/" + t + ".js"
if s.server.Template.HasTemplate(n) {
s.js[t] = true
}
if d, ok := c.(*model.Dashboard); ok {
s.updateJsl(d.Components)
} else if d, ok := c.(*model.Container); ok {
s.updateJsl(d.Components)
}
}
func (s *Live) updateJsl(l model.ComponentList) {
for _, c := range l {
s.updateJs(c)
}
}
func (s *Live) notify(m api.Metric) {
if m.IsValid() {
d := s.getDashboard()
if d != nil {
r := &model.Response{}
d.Process(m, r)
// Only send if we have something to update
if b, valid := r.Json(); valid {
s.websocket.Send(b)
}
}
}
}