-
-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathstats_template.go
More file actions
238 lines (232 loc) · 8.29 KB
/
Copy pathstats_template.go
File metadata and controls
238 lines (232 loc) · 8.29 KB
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
/*
* Copyright 2022-present Kuei-chun Chen. All rights reserved.
* stats_template.go
*/
package hatchet
import (
"fmt"
"html/template"
"strings"
"golang.org/x/text/language"
"golang.org/x/text/message"
)
const MIN_MONGO_VER = "5.0"
// GetStatsTableTemplate returns HTML
func GetStatsTableTemplate(collscan bool, orderBy string, download string) (*template.Template, error) {
html := headers
if download == "" {
html = getContentHTML()
}
html += getStatsTable(collscan, orderBy, download)
if download == "" {
html += "</div><!-- end content-container -->"
}
html += "</body></html>"
return template.New("hatchet").Funcs(template.FuncMap{
"add": func(a int, b int) int {
return a + b
},
"hasPrefix": func(str string, pre string) bool {
return strings.HasPrefix(str, pre)
},
"numPrinter": func(n interface{}) string {
printer := message.NewPrinter(language.English)
return printer.Sprintf("%v", ToInt(n))
},
"getMarkerHTML": func(marker int) template.HTML {
return template.HTML(GetMarkerHTML(marker))
}}).Parse(html)
}
func getStatsTable(collscan bool, orderBy string, download string) string {
checked := ""
if collscan {
checked = "checked"
}
html := fmt.Sprintf(`
<script>
function getSlowopsStats() {
var b = document.getElementById('collscan').checked;
loadData('/hatchets/{{.Hatchet}}/stats/slowops?orderBy=%v&COLLSCAN='+b);
}
function downloadStats() {
anchor = document.createElement('a');
anchor.download = '{{.Hatchet}}_stats.html';
anchor.href = '/hatchets/{{.Hatchet}}/stats/slowops?type=stats&download=true';
anchor.dataset.downloadurl = ['text/html', anchor.download, anchor.href].join(':');
anchor.click();
}
function toggleStatsJson(id) {
var row = document.getElementById('json-stats-' + id);
var btn = document.getElementById('btn-stats-' + id);
if (row.style.display === 'none' || row.style.display === '') {
row.style.display = 'table-row';
btn.classList.add('active');
// Format on first open
var indexEl = document.getElementById('index-content-' + id);
var patternEl = document.getElementById('pattern-content-' + id);
if (indexEl && !indexEl.dataset.formatted) {
indexEl.innerHTML = formatJsonContent(indexEl.textContent);
indexEl.dataset.formatted = 'true';
}
if (patternEl && !patternEl.dataset.formatted) {
patternEl.innerHTML = formatJsonContent(patternEl.textContent);
patternEl.dataset.formatted = 'true';
}
} else {
row.style.display = 'none';
btn.classList.remove('active');
}
}
function syntaxHighlightStats(json) {
json = json.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) {
var cls = 'json-number';
if (/^"/.test(match)) {
if (/:$/.test(match)) {
cls = 'json-key';
} else {
cls = 'json-string';
}
} else if (/true|false/.test(match)) {
cls = 'json-boolean';
} else if (/null/.test(match)) {
cls = 'json-null';
}
return '<span class="' + cls + '">' + match + '</span>';
});
}
function formatJsonContent(text) {
try {
var json = JSON.parse(text);
return syntaxHighlightStats(JSON.stringify(json, null, 2));
} catch(e) {
// Try to fix unquoted keys (Go-style map format)
try {
// Add quotes around unquoted keys: word: or $word:
var fixed = text.replace(/([{,]\s*)(\$?[a-zA-Z_][a-zA-Z0-9_.]*)\s*:/g, '$1"$2":');
var json = JSON.parse(fixed);
return syntaxHighlightStats(JSON.stringify(json, null, 2));
} catch(e2) {
// Still not valid, return as-is
return text;
}
}
}
</script>
<style>
.stats-json-btn {
background: #f0f0f0;
border: 1px solid #ccc;
border-radius: 3px;
padding: 2px 6px;
cursor: pointer;
font-family: monospace;
font-size: 0.85em;
color: #666;
margin-right: 5px;
}
.stats-json-btn:hover {
background: #e0e0e0;
}
.stats-json-btn.active {
background: #d0e8d0;
border-color: #7BAF9B;
}
.stats-json-row {
display: none;
}
.stats-json-content {
background: #1e1e1e;
color: #d4d4d4;
padding: 12px;
border-radius: 4px;
font-family: 'Consolas', 'Monaco', monospace;
font-size: 0.9em;
white-space: pre-wrap;
word-break: break-all;
margin: 5px 0;
max-height: 400px;
overflow: auto;
}
</style>`, orderBy)
asc := "<i class='fa fa-sort-asc'/>"
desc := "<i class='fa fa-sort-desc'/>"
html += `<div align='left'>`
if download == "" {
html += `
<!-- Header Bar -->
<div style='display: flex; justify-content: space-between; align-items: center; margin-bottom: 15px;'>
<h2 style='margin: 0; color: #444; font-size: 1.4em;'><i class='fa fa-info-circle' style='color: #1565c0;'></i> Slow Query Patterns</h2>
<button id="download" onClick="downloadStats(); return false;"
class="download-btn"><i class="fa fa-download"></i> Download</button>
</div>`
} else {
html += "<div align='center'>{{.Summary}}</div>"
asc = ""
desc = ""
}
html += `<table width='100%'><tr><th>#</th>`
html += fmt.Sprintf(`<th>op <a class='sort' href='/hatchets/{{.Hatchet}}/stats/slowops?orderBy=op&COLLSCAN=%v'>%v</th>`, collscan, asc)
html += fmt.Sprintf(`<th>namespace <a class='sort' href='/hatchets/{{.Hatchet}}/stats/slowops?orderBy=ns&order=ASC&COLLSCAN=%v'>%v</th>`, collscan, asc)
html += fmt.Sprintf(`<th>count <a class='sort' href='/hatchets/{{.Hatchet}}/stats/slowops?orderBy=count&COLLSCAN=%v'>%v</th>`, collscan, desc)
html += fmt.Sprintf(`<th>avg ms <a class='sort' href='/hatchets/{{.Hatchet}}/stats/slowops?orderBy=avg_ms&COLLSCAN=%v'>%v</th>`, collscan, desc)
html += fmt.Sprintf(`<th>max ms <a class='sort' href='/hatchets/{{.Hatchet}}/stats/slowops?orderBy=max_ms&COLLSCAN=%v'>%v</th>`, collscan, desc)
html += fmt.Sprintf(`<th>total ms <a class='sort' href='/hatchets/{{.Hatchet}}/stats/slowops?orderBy=total_ms&COLLSCAN=%v'>%v</th>`, collscan, desc)
html += fmt.Sprintf(`<th>reslen <a class='sort' href='/hatchets/{{.Hatchet}}/stats/slowops?orderBy=reslen&COLLSCAN=%v'>%v</th>`, collscan, desc)
if download == "" {
html += fmt.Sprintf(`<th valign='middle'>index <label title='Show COLLSCAN only' style='cursor: pointer; font-weight: normal; font-size: 0.85em;'><input type='checkbox' id='collscan' onchange='getSlowopsStats(); return false;' %v> only</label></th>`, checked)
} else {
html += "<th valign='middle'>index</th>"
}
html += `<th>query pattern</th><th style='width: 40px;'></th>
</tr>
{{$merge := .Merge}}
{{range $n, $value := .Ops}}
<tr>
{{if $merge}}
<td align='right'>{{ add $n 1 }} {{getMarkerHTML $value.Marker}}</td>
{{else}}
<td align='right'>{{ add $n 1 }}</td>
{{end}}
<td style='white-space: nowrap;'>{{ $value.Op }}</td>
<td class='break'>{{ $value.Namespace }}</td>
<td align='right'>{{ numPrinter $value.Count }}</td>
<td align='right'>{{ numPrinter $value.AvgMilli }}</td>
<td align='right'>{{ numPrinter $value.MaxMilli }}</td>
<td align='right'>{{ numPrinter $value.TotalMilli }}</td>
<td align='right'>{{ numPrinter $value.Reslen }}</td>
{{ if or (eq $value.Index "COLLSCAN") }}
<td><span style='color:red;'>{{ $value.Index }}</span></td>
{{ else if (hasPrefix $value.Index "ErrMsg:") }}
<td align='center'>
<div class='tooltip'><button class="exclamation"><i class="fa fa-exclamation"></i></button>
<span class="tooltiptext">{{$value.Index}}</span></div>
</td>
{{else}}
<td>{{ $value.Index }}</td>
{{end}}
<td class='break'>{{ $value.QueryPattern }}</td>
<td align='center'><button id='btn-stats-{{$n}}' class='stats-json-btn' onclick='toggleStatsJson({{$n}})' title='View formatted'>{}</button></td>
</tr>
<tr id='json-stats-{{$n}}' class='stats-json-row'>
<td colspan='11' style='padding: 5px 10px;'>
<div style='display: flex; gap: 20px;'>
<div style='flex: 1;'>
<div style='font-weight: bold; margin-bottom: 5px; color: #666;'>Index:</div>
<pre class='stats-json-content' id='index-content-{{$n}}'>{{ $value.Index }}</pre>
</div>
<div style='flex: 2;'>
<div style='font-weight: bold; margin-bottom: 5px; color: #666;'>Query Pattern:</div>
<pre class='stats-json-content' id='pattern-content-{{$n}}'>{{ $value.QueryPattern }}</pre>
</div>
</div>
</td>
</tr>
{{end}}
</table>
</div>
<div align='center'><hr/><p/>{{.Version}}</div>
</div>
`
return html
}