forked from dolthub/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queryz.go
156 lines (139 loc) · 3.52 KB
/
queryz.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
// Copyright 2012, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package tabletserver
import (
"fmt"
"html/template"
"net/http"
"sort"
"time"
log "github.com/golang/glog"
"github.com/youtube/vitess/go/acl"
"github.com/youtube/vitess/go/vt/tabletserver/planbuilder"
)
var (
queryzHeader = []byte(`<thead>
<tr>
<th>Query</th>
<th>Table</th>
<th>Plan</th>
<th>Reason</th>
<th>Count</th>
<th>Time</th>
<th>Rows</th>
<th>Errors</th>
<th>Time per query</th>
<th>Rows per query</th>
<th>Errors per query</th>
</tr>
</thead>
`)
queryzTmpl = template.Must(template.New("example").Parse(`
<tr class="{{.Color}}">
<td>{{.Query}}</td>
<td>{{.Table}}</td>
<td>{{.Plan}}</td>
<td>{{.Reason}}</td>
<td>{{.Count}}</td>
<td>{{.Time}}</td>
<td>{{.Rows}}</td>
<td>{{.Errors}}</td>
<td>{{.TimePQ}}</td>
<td>{{.RowsPQ}}</td>
<td>{{.ErrorsPQ}}</td>
</tr>
`))
)
// queryzRow is used for rendering query stats
// using go's template.
type queryzRow struct {
Query string
Table string
Plan planbuilder.PlanType
Reason planbuilder.ReasonType
Count int64
tm time.Duration
Rows int64
Errors int64
Color string
}
// Time returns the total time as a string.
func (qzs *queryzRow) Time() string {
return fmt.Sprintf("%.6f", float64(qzs.tm)/1e9)
}
func (qzs *queryzRow) timePQ() float64 {
return float64(qzs.tm) / (1e9 * float64(qzs.Count))
}
// TimePQ returns the time per query as a string.
func (qzs *queryzRow) TimePQ() string {
return fmt.Sprintf("%.6f", qzs.timePQ())
}
func (qzs *queryzRow) rowsPQ() float64 {
return float64(qzs.Rows) / float64(qzs.Count)
}
// RowsPQ returns the row count per query as a string.
func (qzs *queryzRow) RowsPQ() string {
return fmt.Sprintf("%.6f", qzs.rowsPQ())
}
// ErrorsPQ returns the error count per query as a string.
func (qzs *queryzRow) ErrorsPQ() string {
return fmt.Sprintf("%.6f", float64(qzs.Errors)/float64(qzs.Count))
}
type queryzSorter struct {
rows []*queryzRow
less func(row1, row2 *queryzRow) bool
}
func (sorter *queryzSorter) Len() int {
return len(sorter.rows)
}
func (sorter *queryzSorter) Swap(i, j int) {
sorter.rows[i], sorter.rows[j] = sorter.rows[j], sorter.rows[i]
}
func (sorter *queryzSorter) Less(i, j int) bool {
return sorter.less(sorter.rows[i], sorter.rows[j])
}
func queryzHandler(si *SchemaInfo, w http.ResponseWriter, r *http.Request) {
if err := acl.CheckAccessHTTP(r, acl.DEBUGGING); err != nil {
acl.SendError(w, err)
return
}
startHTMLTable(w)
defer endHTMLTable(w)
w.Write(queryzHeader)
keys := si.queries.Keys()
sorter := queryzSorter{
rows: make([]*queryzRow, 0, len(keys)),
less: func(row1, row2 *queryzRow) bool {
return row1.timePQ() > row2.timePQ()
},
}
for _, v := range si.queries.Keys() {
plan := si.getQuery(v)
if plan == nil {
continue
}
Value := &queryzRow{
Query: wrappable(v),
Table: plan.TableName,
Plan: plan.PlanId,
Reason: plan.Reason,
}
Value.Count, Value.tm, Value.Rows, Value.Errors = plan.Stats()
timepq := time.Duration(int64(Value.tm) / Value.Count)
if timepq < 10*time.Millisecond {
Value.Color = "low"
} else if timepq < 100*time.Millisecond {
Value.Color = "medium"
} else {
Value.Color = "high"
}
sorter.rows = append(sorter.rows, Value)
}
sort.Sort(&sorter)
for _, Value := range sorter.rows {
if err := queryzTmpl.Execute(w, Value); err != nil {
log.Errorf("queryz: couldn't execute template: %v", err)
}
}
}