-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
logz_utils.go
179 lines (157 loc) · 4.51 KB
/
logz_utils.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
/*
Copyright 2019 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package logz provides an infrastructure to expose a list of entries as
// a sortable table on a webpage.
//
// It is used by many internal vttablet pages e.g. /queryz, /querylogz, /schemaz
// /livequeryz or /txlogz.
//
// See tabletserver/querylogz.go for an example how to use it.
package logz
import (
"bytes"
"net/http"
)
// StartHTMLTable writes the start of a logz-style table to an HTTP response.
func StartHTMLTable(w http.ResponseWriter) {
w.Write([]byte(`<!DOCTYPE html>
<style type="text/css">
table.gridtable {
font-family: verdana,arial,sans-serif;
font-size:11px;
border-width: 1px;
border-collapse: collapse;
table-layout:fixed;
overflow: hidden;
}
table.gridtable th {
border-width: 1px;
padding: 8px;
border-style: solid;
background-color: #dedede;
white-space: nowrap;
}
table.gridtable tr.low {
background-color: #f0f0f0;
}
table.gridtable tr.medium {
background-color: #ffcc00;
}
table.gridtable tr.high {
background-color: #ff3300;
}
table.gridtable tr.error {
background-color: #00ddff;
}
table.gridtable td {
border-width: 1px;
padding: 4px;
border-style: solid;
}
table.gridtable th {
padding-left: 2em;
padding-right: 2em;
}
table.gridtable thead th {
cursor: pointer;
}
table.gridtable th.descending:before {
content: "▲";
float: left;
}
table.gridtable th.ascending:before {
content: "▼";
float: left;
}
</style>
<table class="gridtable">
`))
}
// EndHTMLTable writes the end of a logz-style table to an HTTP response.
func EndHTMLTable(w http.ResponseWriter) {
w.Write([]byte(`
</table>
<script type="text/javascript">
function wrapInner(parent, element, style) {
const wrapper = document.createElement(element);
wrapper.style = style
const div = parent.appendChild(wrapper);
while(parent.firstChild !== wrapper) {
wrapper.appendChild(parent.firstChild);
}
}
function sortableByColumn(element) {
const body = element.querySelector('tbody')
const header = element.querySelector('thead')
const contents = (element, i) => {
const data = element.querySelectorAll("td")[i].innerText.toLowerCase();
if (data == "n/a") {
return -1;
}
var asNumber = parseFloat(data);
if (data.slice(-1) == "s" && !isNaN(asNumber) && data.slice(0, -1) == asNumber) {
// Return durations (e.g. "11.2s") always as number.
return asNumber;
}
return data == asNumber ? asNumber : data;
}
element.querySelectorAll('thead tr th').forEach((th, index) => {
wrapInner(th, "div", "overflow: auto; display: inline-block;");
let direction = -1;
th.onclick=() => {
direction *= -1;
const headerCells = header.querySelectorAll('th');
headerCells.forEach((hc) => {
hc.classList.remove('ascending', 'descending');
})
th.classList.add(direction > 0? 'ascending' : 'descending');
const rows = body.querySelectorAll('tr');
const sortedRows = Array.from(rows).sort(function(left, right) {
var cl = contents(left, index);
var cr = contents(right, index);
if (cl === cr) {
return 0
} else {
return contents(left, index) > contents(right, index)? direction : -direction;
}
});
// Remove original rows
rows.forEach(row => row.remove());
// Add new sorted rows
sortedRows.forEach(row => {
body.appendChild(row);
});
}
})
}
// Execute the function when the DOM loads
(function() {
const table = document.querySelector('table');
sortableByColumn(table)
})();
</script>
`))
}
// Wrappable inserts zero-width whitespaces to make
// the string wrappable.
func Wrappable(in string) string {
buf := bytes.NewBuffer(nil)
for _, ch := range in {
buf.WriteRune(ch)
if ch == ',' || ch == ')' {
// zero-width whitespace
buf.WriteRune('\u200B')
}
}
return buf.String()
}