-
Notifications
You must be signed in to change notification settings - Fork 3
/
charts.go
348 lines (321 loc) · 7.81 KB
/
charts.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// SPDX-License-Identifier: GPL-3.0
// Copyright 2022 Pete Heist
package antler
import (
"context"
_ "embed"
"fmt"
"regexp"
"sort"
"strings"
"unicode"
"html/template"
"io"
"github.com/heistp/antler/node"
)
// chartsTemplate is the template for Google Charts reporters.
//
//go:embed charts.html.tmpl
var chartsTemplate string
// chartsTemplateData contains the data for chartsTemplate execution.
type chartsTemplateData struct {
Class template.JS
Data chartsData
Options map[string]any
Stream []StreamAnalysis
Packet []PacketAnalysis
}
// ChartsTimeSeries is a reporter that makes time series plots using Google
// Charts.
type ChartsTimeSeries struct {
// FlowLabel sets custom labels for Flows.
FlowLabel map[node.Flow]string
// To lists the names of files to execute the template to. A file of "-"
// emits to stdout.
To []string
// Options is an arbitrary structure of Charts options, with defaults
// defined in config.cue.
// https://developers.google.com/chart/interactive/docs/gallery/linechart#configuration-options
Options map[string]any
}
// report implements reporter
func (g *ChartsTimeSeries) report(ctx context.Context, rw rwer, in <-chan any,
out chan<- any) (err error) {
t := template.New("Style")
if t, err = t.Parse(styleTemplate); err != nil {
return
}
t = t.New("ChartsTimeSeries")
t = t.Funcs(template.FuncMap{
"flowLabel": func(flow node.Flow) (label string) {
label, ok := g.FlowLabel[flow]
if !ok {
return string(flow)
}
return label
},
})
if t, err = t.Parse(chartsTemplate); err != nil {
return
}
var a analysis
for d := range in {
out <- d
switch v := d.(type) {
case analysis:
a = v
}
}
td := chartsTemplateData{
"google.visualization.LineChart",
g.data(a.streams.byTime(), a.packets.byTime()),
g.Options,
a.streams.byTime(),
a.packets.byTime(),
}
var ww []io.WriteCloser
for _, to := range g.To {
ww = append(ww, rw.Writer(to))
}
defer func() {
for _, w := range ww {
if e := w.Close(); e != nil && err == nil {
err = e
}
}
}()
err = t.Execute(multiWriteCloser(ww...), td)
return
}
// data returns the chart data.
func (g *ChartsTimeSeries) data(san []StreamAnalysis, pan []PacketAnalysis) (
data chartsData) {
data.set(0, 0, "Time (sec)")
col := 1
row := 1
for _, d := range san {
l := string(d.Client.Flow)
if ll, ok := g.FlowLabel[d.Client.Flow]; ok {
l = ll
}
if len(d.GoodputPoint) > 1 {
data.set(0, col, fmt.Sprintf("%s goodput", l))
for _, g := range d.GoodputPoint {
data.set(row, 0, g.T.Duration().Seconds())
data.set(row, col, g.Goodput.Mbps())
row++
}
col++
}
if len(d.TCPInfo) > 0 {
data.set(0, col, fmt.Sprintf("%s delivery rate", l))
for _, t := range d.TCPInfo {
data.set(row, 0, t.T.Duration().Seconds())
data.set(row, col, t.DeliveryRate.Mbps())
row++
}
col++
}
if len(d.TCPInfo) > 0 {
data.set(0, col, fmt.Sprintf("%s TCP RTT", l))
for _, t := range d.TCPInfo {
data.set(row, 0, t.T.Duration().Seconds())
data.set(row, col, t.RTT.Seconds()*1000.0)
row++
}
col++
}
}
for _, d := range pan {
l := string(d.Client.Flow)
if ll, ok := g.FlowLabel[d.Client.Flow]; ok {
l = ll
}
if len(d.Up.OWD) > 0 {
data.set(0, col, fmt.Sprintf("%s OWD up", l))
for _, o := range d.Up.OWD {
data.set(row, 0, o.T.Duration().Seconds())
data.set(row, col, o.Delay.Seconds()*1000.0)
row++
}
col++
}
}
data.normalize()
return
}
// ChartsFCT is a reporter that makes time series plots using Google Charts.
type ChartsFCT struct {
// To lists the names of files to execute the template to. A file of "-"
// emits to stdout.
To []string
// Series matches Flows to series.
Series []FlowSeries
// Options is an arbitrary structure of Charts options, with defaults
// defined in config.cue.
// https://developers.google.com/chart/interactive/docs/gallery/scatterchart#configuration-options
Options map[string]any
}
// report implements reporter
func (g *ChartsFCT) report(ctx context.Context, rw rwer, in <-chan any,
out chan<- any) (err error) {
t := template.New("Style")
if t, err = t.Parse(styleTemplate); err != nil {
return
}
t = t.New("ChartsFCT")
t = t.Funcs(template.FuncMap{})
if t, err = t.Parse(chartsTemplate); err != nil {
return
}
var a analysis
for d := range in {
out <- d
switch v := d.(type) {
case analysis:
a = v
}
}
if len(g.Series) == 0 {
var f flows
for _, s := range a.streams {
f.add(s.Client.Flow)
}
g.Series = append(g.Series, FlowSeries{f.commonPrefix(), ".*", nil})
}
for i := 0; i < len(g.Series); i++ {
s := &g.Series[i]
if err = s.Compile(); err != nil {
err = fmt.Errorf("regex error in series %s: %w", s.Name, err)
return
}
}
td := chartsTemplateData{
"google.visualization.ScatterChart",
g.data(a.streams.byTime()),
g.Options,
a.streams.byTime(),
a.packets.byTime(),
}
var ww []io.WriteCloser
for _, to := range g.To {
ww = append(ww, rw.Writer(to))
}
defer func() {
for _, w := range ww {
if e := w.Close(); e != nil && err == nil {
err = e
}
}
}()
err = t.Execute(multiWriteCloser(ww...), td)
return
}
// data returns the chart data.
func (g *ChartsFCT) data(san []StreamAnalysis) (data chartsData) {
data.set(0, 0, "Length (kB)")
for i, s := range g.Series {
data.set(0, i+1, s.Name)
}
row := 1
for _, a := range san {
data.set(row, 0, a.Length.Kilobytes())
col := 1
for _, s := range g.Series {
if s.Match(a.Client.Flow) {
data.set(row, col, a.FCT.Seconds())
}
col++
}
row++
}
data.normalize()
return
}
// FlowSeries groups flows into series by matching the Flow ID with a Regex.
type FlowSeries struct {
Name string
Pattern string
rgx *regexp.Regexp
}
// Compile compiles Pattern to a Regexp.
func (s *FlowSeries) Compile() (err error) {
s.rgx, err = regexp.Compile(s.Pattern)
return
}
// Match returns true if Flow matches Regex.
func (s *FlowSeries) Match(flow node.Flow) (matches bool) {
return s.rgx.MatchString(string(flow))
}
// chartsData represents tabular data for use in Google Charts. Callers should
// first use the set method to set any values, then the normalize method to
// prepare the data for use with Charts.
type chartsData [][]any
// set records the given value in the given row and column, expanding the
// underlying slice as necessary.
func (c *chartsData) set(row int, column int, value any) {
for i := len(*c) - 1; i < row; i++ {
*c = append(*c, []any{})
}
for i := len((*c)[row]) - 1; i < column; i++ {
(*c)[row] = append((*c)[row], nil)
}
(*c)[row][column] = value
}
// normalize finalizes the table by equalizing the number of columns for each
// row, and returns the data for convenience.
func (c *chartsData) normalize() [][]any {
var m int
for i := 0; i < len(*c); i++ {
if len((*c)[i]) > m {
m = len((*c)[i])
}
}
for i := 0; i < len(*c); i++ {
for j := len((*c)[i]); j < m; j++ {
(*c)[i] = append((*c)[i], nil)
}
}
return *c
}
// flows wraps []node.Flow with additional functionality.
type flows []node.Flow
// add adds a Flow.
func (f *flows) add(flow node.Flow) {
(*f) = append(*f, flow)
}
// sort sorts the Flows lexically.
func (f *flows) sort() {
sort.Slice(*f, func(i, j int) bool {
return string((*f)[i]) < string((*f)[j])
})
}
// strings returns the Flows as strings.
func (f *flows) strings() (s []string) {
s = make([]string, 0, len(*f))
for _, n := range *f {
s = append(s, string(n))
}
return
}
// commonPrefix returns the longest common prefix to all flows.
func (f *flows) commonPrefix() (prefix string) {
if len(*f) == 0 {
return
}
s := f.strings()
sort.Strings(s)
r := s[0]
l := s[len(s)-1]
for i := 0; i < len(r); i++ {
if l[i] == r[i] {
prefix += string(l[i])
} else {
break
}
}
prefix = strings.TrimRightFunc(prefix, func(r rune) bool {
return !unicode.IsLetter(r) && !unicode.IsNumber(r)
})
return
}