-
Notifications
You must be signed in to change notification settings - Fork 140
/
charts.go
347 lines (317 loc) · 8.91 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
package main
import (
"bytes"
"embed"
"encoding/json"
"fmt"
"net"
"os"
"os/exec"
"runtime"
"strings"
"text/template"
"time"
_ "embed"
cors "github.com/AdhityaRamadhanus/fasthttpcors"
"github.com/go-echarts/go-echarts/v2/charts"
"github.com/go-echarts/go-echarts/v2/components"
"github.com/go-echarts/go-echarts/v2/opts"
"github.com/go-echarts/go-echarts/v2/templates"
"github.com/valyala/fasthttp"
)
//go:embed echarts.min.js
//go:embed jquery.min.js
var assetsFS embed.FS
var (
assetsPath = "/echarts/statics/"
apiPath = "/data/"
latencyView = "latency"
rpsView = "rps"
codeView = "code"
timeFormat = "15:04:05"
refreshInterval = time.Second
templateRegistry = map[string]string{
rpsView: ViewTpl,
latencyView: ViewTpl,
codeView: CodeViewTpl,
}
)
const (
ViewTpl = `
$(function () { setInterval({{ .ViewID }}_sync, {{ .Interval }}); });
function {{ .ViewID }}_sync() {
$.ajax({
type: "GET",
url: "{{ .APIPath }}{{ .Route }}",
dataType: "json",
success: function (result) {
let opt = goecharts_{{ .ViewID }}.getOption();
let x = opt.xAxis[0].data;
x.push(result.time);
opt.xAxis[0].data = x;
for (let i = 0; i < result.values.length; i++) {
let y = opt.series[i].data;
y.push({ value: result.values[i] });
opt.series[i].data = y;
goecharts_{{ .ViewID }}.setOption(opt);
}
}
});
}`
PageTpl = `
{{- define "page" }}
<!DOCTYPE html>
<html>
{{- template "header" . }}
<body>
<p align="center">🚀 <a href="https://github.com/six-ddc/plow"><b>Plow</b></a> %s</p>
<style> .box { justify-content:center; display:flex; flex-wrap:wrap } </style>
<div class="box"> {{- range .Charts }} {{ template "base" . }} {{- end }} </div>
</body>
</html>
{{ end }}
`
CodeViewTpl = `
$(function () { setInterval({{ .ViewID }}_sync, {{ .Interval }}); });
function {{ .ViewID }}_sync() {
$.ajax({
type: "GET",
url: "{{ .APIPath }}{{ .Route }}",
dataType: "json",
success: function (result) {
let opt = goecharts_{{ .ViewID }}.getOption();
let x = opt.xAxis[0].data;
x.push(result.time);
opt.xAxis[0].data = x;
let nameAndSeriesMapping = {};
for (let i = 0; i < opt.series.length; i++) {
nameAndSeriesMapping[opt.series[i].name] = opt.series[i];
}
let code200Count = nameAndSeriesMapping['200'].data.length;
let codes = result.values[0];
if (codes === null){
for (let key in nameAndSeriesMapping) {
let series = nameAndSeriesMapping[key];
series.data.push({value:null});
}
}else{
if (!('200' in codes)) {
codes['200'] = null;
}
for (let code in codes) {
let count = codes[code];
if (code in nameAndSeriesMapping){
let series = nameAndSeriesMapping[code];
series.data.push({value:count});
}else{
let data = [];
for (let i = 0; i < code200Count; i++) {
data.push[null];
}
var newSeries = {
name: code,
type: 'line',
data: data
};
opt.series.push(newSeries);
}
}
}
goecharts_{{ .ViewID }}.setOption(opt);
}
});
}`
)
func (c *Charts) genViewTemplate(vid, route string) string {
tpl, err := template.New("view").Parse(templateRegistry[route])
if err != nil {
panic("failed to parse template " + err.Error())
}
var d = struct {
Interval int
APIPath string
Route string
ViewID string
}{
Interval: int(refreshInterval.Milliseconds()),
APIPath: apiPath,
Route: route,
ViewID: vid,
}
buf := bytes.Buffer{}
if err := tpl.Execute(&buf, d); err != nil {
panic("failed to execute template " + err.Error())
}
return buf.String()
}
func (c *Charts) newBasicView(route string) *charts.Line {
graph := charts.NewLine()
graph.SetGlobalOptions(
charts.WithTooltipOpts(opts.Tooltip{Show: opts.Bool(true), Trigger: "axis"}),
charts.WithXAxisOpts(opts.XAxis{Name: "Time"}),
charts.WithInitializationOpts(opts.Initialization{
Width: "700px",
Height: "400px",
}),
charts.WithDataZoomOpts(opts.DataZoom{
Type: "slider",
XAxisIndex: []int{0},
}),
)
graph.SetXAxis([]string{}).SetSeriesOptions(charts.WithLineChartOpts(opts.LineChart{Smooth: opts.Bool(true)}))
graph.AddJSFuncs(c.genViewTemplate(graph.ChartID, route))
return graph
}
func (c *Charts) newLatencyView() components.Charter {
graph := c.newBasicView(latencyView)
graph.SetGlobalOptions(
charts.WithTitleOpts(opts.Title{Title: "Latency"}),
charts.WithYAxisOpts(opts.YAxis{Scale: opts.Bool(true), AxisLabel: &opts.AxisLabel{Formatter: "{value} ms"}}),
charts.WithLegendOpts(opts.Legend{Show: opts.Bool(true), Selected: map[string]bool{"Min": false, "Max": false}}),
)
graph.AddSeries("Min", []opts.LineData{}).
AddSeries("Mean", []opts.LineData{}).
AddSeries("Max", []opts.LineData{})
return graph
}
func (c *Charts) newRPSView() components.Charter {
graph := c.newBasicView(rpsView)
graph.SetGlobalOptions(
charts.WithTitleOpts(opts.Title{Title: "Reqs/sec"}),
charts.WithYAxisOpts(opts.YAxis{Scale: opts.Bool(true)}),
)
graph.AddSeries("RPS", []opts.LineData{})
return graph
}
func (c *Charts) newCodeView() components.Charter {
graph := c.newBasicView(codeView)
graph.SetGlobalOptions(
charts.WithTitleOpts(opts.Title{Title: "Response Status"}),
charts.WithYAxisOpts(opts.YAxis{Scale: opts.Bool(true)}),
charts.WithLegendOpts(opts.Legend{Show: opts.Bool(true)}),
)
graph.AddSeries("200", []opts.LineData{})
return graph
}
type Metrics struct {
Values []interface{} `json:"values"`
Time string `json:"time"`
}
type Charts struct {
page *components.Page
ln net.Listener
dataFunc func() *ChartsReport
}
func NewCharts(ln net.Listener, dataFunc func() *ChartsReport, desc string) (*Charts, error) {
templates.PageTpl = fmt.Sprintf(PageTpl, desc)
c := &Charts{ln: ln, dataFunc: dataFunc}
c.page = components.NewPage()
c.page.PageTitle = "plow"
c.page.AssetsHost = assetsPath
c.page.Assets.JSAssets.Add("jquery.min.js")
c.page.AddCharts(c.newLatencyView(), c.newRPSView(), c.newCodeView())
return c, nil
}
func (c *Charts) Handler(ctx *fasthttp.RequestCtx) {
path := string(ctx.Path())
if strings.HasPrefix(path, apiPath) {
view := path[len(apiPath):]
var values []interface{}
reportData := c.dataFunc()
switch view {
case latencyView:
if reportData != nil {
values = append(values, reportData.Latency.min/1e6)
values = append(values, reportData.Latency.Mean()/1e6)
values = append(values, reportData.Latency.max/1e6)
} else {
values = append(values, nil, nil, nil)
}
case rpsView:
if reportData != nil {
values = append(values, reportData.RPS)
} else {
values = append(values, nil)
}
case codeView:
if reportData != nil {
values = append(values, reportData.CodeMap)
} else {
values = append(values, nil)
}
}
metrics := &Metrics{
Time: time.Now().Format(timeFormat),
Values: values,
}
_ = json.NewEncoder(ctx).Encode(metrics)
} else if path == "/" {
ctx.SetContentType("text/html")
_ = c.page.Render(ctx)
} else if strings.HasPrefix(path, assetsPath) {
ap := path[len(assetsPath):]
f, err := assetsFS.Open(ap)
if err != nil {
ctx.Error(err.Error(), 404)
} else {
ctx.SetBodyStream(f, -1)
}
} else {
ctx.Error("NotFound", fasthttp.StatusNotFound)
}
}
func (c *Charts) Serve(open bool) {
server := fasthttp.Server{
Handler: cors.DefaultHandler().CorsMiddleware(c.Handler),
}
if open {
go openBrowser("http://" + c.ln.Addr().String())
}
_ = server.Serve(c.ln)
}
// openBrowser go/src/cmd/internal/browser/browser.go
func openBrowser(url string) bool {
var cmds [][]string
if exe := os.Getenv("BROWSER"); exe != "" {
cmds = append(cmds, []string{exe})
}
switch runtime.GOOS {
case "darwin":
cmds = append(cmds, []string{"/usr/bin/open"})
case "windows":
cmds = append(cmds, []string{"cmd", "/c", "start"})
default:
if os.Getenv("DISPLAY") != "" {
// xdg-open is only for use in a desktop environment.
cmds = append(cmds, []string{"xdg-open"})
}
}
cmds = append(cmds,
[]string{"chrome"},
[]string{"google-chrome"},
[]string{"chromium"},
[]string{"firefox"},
)
for _, args := range cmds {
cmd := exec.Command(args[0], append(args[1:], url)...)
if cmd.Start() == nil && appearsSuccessful(cmd, 3*time.Second) {
return true
}
}
return false
}
// appearsSuccessful reports whether the command appears to have run successfully.
// If the command runs longer than the timeout, it's deemed successful.
// If the command runs within the timeout, it's deemed successful if it exited cleanly.
func appearsSuccessful(cmd *exec.Cmd, timeout time.Duration) bool {
errc := make(chan error, 1)
go func() {
errc <- cmd.Wait()
}()
select {
case <-time.After(timeout):
return true
case err := <-errc:
return err == nil
}
}