forked from simagix/hatchet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchart_templ.go
159 lines (147 loc) · 4.77 KB
/
chart_templ.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
// Copyright 2022-present Kuei-chun Chen. All rights reserved.
package hatchet
import (
"fmt"
"html/template"
"time"
)
type NameValue struct {
Name string
Value int
}
func getFooter() string {
summary := "{{.Summary}}"
return fmt.Sprintf(`<div class="footer"><img width='32' valign="middle" src='data:image/png;base64,%v'> %v</img></div>`,
hatchetImage, summary)
}
// GetChartTemplate returns HTML
func GetChartTemplate(attr string, chartType string) (*template.Template, error) {
html := getContentHTML(attr, chartType)
if attr == "connections" {
html += getConnectionsChart()
} else if attr == "pieChart" {
html += getPieChart()
} else {
html += getOpStatsChart()
}
html += "<p/><div id='hatchetChart' align='center' width='100%'/>"
html += "</body></html>"
return template.New("hatchet").Funcs(template.FuncMap{
"descr": func(v OpCount) string {
dfmt := "2016-01-02T23:59:59"
d := v.Date + dfmt[len(v.Date):]
return fmt.Sprintf("%v %v %v %v", v.Op, d, v.Namespace, v.Filter)
},
"substr": func(str string, n int) string {
return str[:n]
},
"epoch": func(d string, s string) int64 {
dfmt := "2016-01-02T23:59:59"
sdt, _ := time.Parse("2006-01-02T15:04:05", s+dfmt[len(s):])
dt, _ := time.Parse("2006-01-02T15:04:05", d+dfmt[len(d):])
return dt.Unix() - sdt.Unix()
}}).Parse(html)
}
func getOpStatsChart() string {
return `
<script>
setChartType();
google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['op', 'secs from origin', 'count', 'op detail'],
{{$sdate := ""}}
{{range $i, $v := .OpCounts}}
{{if eq $i 0}}
{{$sdate = $v.Date}}
{{end}}
['', {{epoch $v.Date $sdate}}, {{$v.Count}}, {{descr $v}}],
{{end}}
]);
// Set chart options
var options = {
'title': 'Ops Stats',
'hAxis': { textPosition: 'none' },
'vAxis': {title: 'Count', minValue: 0},
'height': 600,
'titleTextStyle': {'fontSize': 20},
'sizeAxis': {minValue: 0, minSize: 3, maxSize: 3},
'chartArea': {'width': '90%', 'height': '80%'},
'legend': { 'position': 'none' } };
// Instantiate and draw our chart, passing in some options.
var chart = new google.visualization.BubbleChart(document.getElementById('hatchetChart'));
chart.draw(data, options);
}
function refreshOpsStatsChart() {
sd = document.getElementById('start').value;
ed = document.getElementById('end').value;
window.location.href = '/tables/{{.Table}}/charts/slowops?duration=' + sd + 'Z,' + ed + 'Z';
}
</script>
<input type='datetime-local' id='start' value='{{.Start}}'></input>
<input type='datetime-local' id='end' value='{{.End}}'></input>
<button onClick="refreshOpsStatsChart(); return false;" class="button">Refresh</button>`
}
func getPieChart() string {
return `
<script>
setChartType();
google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Name', 'Value'],
{{range $i, $v := .NameValues}}
['{{$v.Name}}', {{$v.Value}}],
{{end}}
]);
// Set chart options
var options = {
'title': '{{.Title}}',
'height': 480,
'titleTextStyle': {'fontSize': 20},
'legend': { 'position': 'left' } };
// Instantiate and draw our chart, passing in some options.
var chart = new google.visualization.PieChart(document.getElementById('hatchetChart'));
chart.draw(data, options);
}
</script>`
}
func getConnectionsChart() string {
return `
<script>
setChartType();
google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['IP', 'Accepted', 'Ended'],
{{range $i, $v := .Remote}}
['{{$v.IP}}', {{$v.Accepted}}, {{$v.Ended}}],
{{end}}
]);
// Set chart options
var options = {
'title': 'Accepted vs Ended Connections',
'hAxis': { slantedText:true, slantedTextAngle:15 },
'vAxis': {title: 'Count', minValue: 0},
'height': 480,
'titleTextStyle': {'fontSize': 20},
'legend': { 'position': 'right' } };
// Instantiate and draw our chart, passing in some options.
var chart = new google.visualization.ColumnChart(document.getElementById('hatchetChart'));
chart.draw(data, options);
}
function refreshConnsTimeChart() {
sd = document.getElementById('start').value;
ed = document.getElementById('end').value;
window.location.href = '/tables/{{.Table}}/charts/connections?type=time&duration=' + sd + 'Z,' + ed + 'Z';
}
</script>
{{ if eq .Chart "time" }}
<input type='datetime-local' id='start' value='{{.Start}}'></input>
<input type='datetime-local' id='end' value='{{.End}}'></input>
<button onClick="refreshConnsTimeChart(); return false;" class="button">Refresh</button>
{{ end }}`
}