-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.go
281 lines (225 loc) · 5.68 KB
/
graph.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package wifire
import (
"errors"
"fmt"
"image/color"
"time"
"gonum.org/v1/plot"
"gonum.org/v1/plot/plotter"
"gonum.org/v1/plot/vg"
"gonum.org/v1/plot/vg/draw"
)
// PlotterOptions is used to configure the Plotter.
type PlotterOptions struct {
Title string
Period Period
AmbientColor color.Color
AmbientFillColor color.Color
ProbeColor color.Color
GrillColor color.Color
MarkerColor color.Color
Data []Status
Markers []time.Duration
}
// Plotter creates a graph of the wifire Status data.
type Plotter struct {
options PlotterOptions
plot *plot.Plot
}
// Period is used to set the x-axis time period.
type Period int
// The Period can be hours, minutes, or days. The default is hours.
const (
ByHour Period = iota
ByMinute
ByDay
)
// NewPlotter returns a Plotter configured with the options o. If o is empty the
// default settings are used.
func NewPlotter(o PlotterOptions) *Plotter {
p := Plotter{
options: PlotterOptions{
AmbientColor: color.Gray{200},
AmbientFillColor: color.Gray{200},
ProbeColor: color.RGBA{B: 255, A: 255},
GrillColor: color.RGBA{R: 255, A: 255},
MarkerColor: color.RGBA{G: 100, A: 255},
},
}
p.options.Title = o.Title
p.options.Period = o.Period
p.options.Data = o.Data
p.options.Markers = o.Markers
if o.AmbientColor != nil {
p.options.AmbientColor = o.AmbientColor
}
if o.AmbientFillColor != nil {
p.options.AmbientFillColor = o.AmbientFillColor
}
if o.ProbeColor != nil {
p.options.ProbeColor = o.ProbeColor
}
if o.GrillColor != nil {
p.options.GrillColor = o.GrillColor
}
if o.AmbientColor != nil {
p.options.MarkerColor = o.MarkerColor
}
return &p
}
// Plot returns the plot.Plot for the Status data given to the Plotter. The
// caller should call plot.Save to create the graph files. This allows the
// caller to define the Plot size and graphics format.
func (p Plotter) Plot() (*plot.Plot, error) {
if p.options.Data == nil {
return nil, errors.New("no data")
}
ambient := make(plotter.XYs, len(p.options.Data))
for i, d := range normalizeStatus(p.options.Data) {
switch p.options.Period {
case ByMinute:
ambient[i].X = d.Minutes()
case ByHour:
ambient[i].X = d.Hours()
case ByDay:
ambient[i].X = d.Hours() / 24
}
ambient[i].Y = float64(p.options.Data[i].Ambient)
}
grill := make(plotter.XYs, len(ambient))
probe := make(plotter.XYs, len(ambient))
grillSet := make(plotter.XYs, len(ambient))
probeSet := make(plotter.XYs, len(ambient))
copy(grill, ambient)
copy(probe, ambient)
copy(grillSet, ambient)
copy(probeSet, ambient)
var maxTemp int
for i := range p.options.Data {
if p.options.Data[i].Grill > maxTemp {
maxTemp = p.options.Data[i].Grill
}
grill[i].Y = float64(p.options.Data[i].Grill)
probe[i].Y = float64(p.options.Data[i].Probe)
grillSet[i].Y = float64(p.options.Data[i].Grill)
probeSet[i].Y = float64(p.options.Data[i].Probe)
}
markers := make(plotter.XYs, len(p.options.Markers))
for i, m := range p.options.Markers {
switch p.options.Period {
case ByMinute:
markers[i].X = m.Minutes()
case ByHour:
markers[i].X = m.Hours()
case ByDay:
markers[i].X = m.Hours() / 24
}
markers[i].Y = float64(maxTemp) / 2 // put markers in the middle of the data
}
p.plot = plot.New()
p.plot.Title.Text = p.options.Title
p.plot.X.Label.Text = "Hours"
p.plot.Y.Label.Text = "Temperature"
if err := p.ambient(ambient); err != nil {
return nil, fmt.Errorf("ambient: %w", err)
}
if err := p.grill(grill, grillSet); err != nil {
return nil, fmt.Errorf("grill: %w", err)
}
if err := p.probe(probe, probeSet); err != nil {
return nil, fmt.Errorf("probe: %w", err)
}
if len(markers) > 0 {
if err := p.markers(markers); err != nil {
return nil, fmt.Errorf("markers: %w", err)
}
}
p.plot.Add(plotter.NewGrid())
return p.plot, nil
}
func (p *Plotter) ambient(data plotter.XYs) error {
if data == nil {
return errors.New("no ambient data")
}
line, err := plotter.NewLine(data)
if err != nil {
return err
}
line.Color = p.options.AmbientColor
line.FillColor = p.options.AmbientFillColor
p.plot.Add(line)
p.plot.Legend.Add("ambient", line)
return nil
}
func (p *Plotter) grill(actual, set plotter.XYs) error {
if actual == nil {
return errors.New("no grill data")
}
a, err := plotter.NewLine(actual)
if err != nil {
return err
}
a.Color = p.options.GrillColor
p.plot.Add(a)
p.plot.Legend.Add("grill", a)
if set == nil {
return nil
}
s, err := plotter.NewLine(set)
if err != nil {
return err
}
s.Color = p.options.GrillColor
s.LineStyle.Dashes = []vg.Length{vg.Points(1), vg.Points(5)}
p.plot.Add(s)
return nil
}
func (p *Plotter) probe(actual, set plotter.XYs) error {
if actual == nil {
return errors.New("no probe data")
}
a, err := plotter.NewLine(actual)
if err != nil {
return err
}
a.Color = p.options.ProbeColor
p.plot.Add(a)
p.plot.Legend.Add("probe", a)
if set == nil {
return nil
}
s, err := plotter.NewLine(set)
if err != nil {
return err
}
s.Color = p.options.ProbeColor
s.LineStyle.Dashes = []vg.Length{vg.Points(1), vg.Points(5)}
p.plot.Add(s)
return nil
}
func (p *Plotter) markers(marks plotter.XYs) error {
if marks == nil {
return nil // markers are optional
}
m, err := plotter.NewScatter(marks)
if err != nil {
return err
}
m.GlyphStyle.Shape = draw.CrossGlyph{}
m.GlyphStyle.Radius = vg.Points(4)
m.Color = p.options.MarkerColor
p.plot.Add(m)
p.plot.Legend.Add("events", m)
return nil
}
func normalizeStatus(s []Status) []time.Duration {
if len(s) == 0 {
return nil
}
d := make([]time.Duration, len(s))
t0 := s[0].Time
for i := range s {
d[i] = s[i].Time.Sub(t0)
}
return d
}