-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
vschema_stats.go
117 lines (106 loc) · 3.02 KB
/
vschema_stats.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
/*
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 vtgate
import (
"sort"
"vitess.io/vitess/go/vt/vtgate/vindexes"
)
// VSchemaStats contains a rollup of the VSchema stats.
type VSchemaStats struct {
Error string
Keyspaces []*VSchemaKeyspaceStats
}
// VSchemaKeyspaceStats contains a rollup of the VSchema stats for a keyspace.
// It is used to display a table with the information in the status page.
type VSchemaKeyspaceStats struct {
Keyspace string
Sharded bool
TableCount int
VindexCount int
VindexUnknownParamsCount int
Error string
}
// NewVSchemaStats returns a new VSchemaStats from a VSchema.
func NewVSchemaStats(vschema *vindexes.VSchema, errorMessage string) *VSchemaStats {
stats := &VSchemaStats{
Error: errorMessage,
Keyspaces: make([]*VSchemaKeyspaceStats, 0, len(vschema.Keyspaces)),
}
for n, k := range vschema.Keyspaces {
s := &VSchemaKeyspaceStats{
Keyspace: n,
}
if k.Keyspace != nil {
s.Sharded = k.Keyspace.Sharded
s.TableCount += len(k.Tables)
for _, t := range k.Tables {
s.VindexCount += len(t.ColumnVindexes) + len(t.Ordered) + len(t.Owned)
}
for _, vdx := range k.Vindexes {
if pv, ok := vdx.(vindexes.ParamValidating); ok {
s.VindexUnknownParamsCount += len(pv.UnknownParams())
}
}
}
if k.Error != nil {
s.Error = k.Error.Error()
}
stats.Keyspaces = append(stats.Keyspaces, s)
}
sort.Slice(stats.Keyspaces, func(i, j int) bool { return stats.Keyspaces[i].Keyspace < stats.Keyspaces[j].Keyspace })
return stats
}
const (
// VSchemaTemplate is the HTML template to display VSchemaStats.
VSchemaTemplate = `
<style>
table {
border-collapse: collapse;
}
td, th {
border: 1px solid #999;
padding: 0.2rem;
}
</style>
<table class="refreshRequired">
<tr>
<th colspan="5">VSchema Cache <i><a href="/debug/vschema">in JSON</a></i></th>
</tr>
{{if .Error}}
<tr>
<th>Error</th>
<td colspan="3">{{$.Error}}</td>
</tr>
<tr>
<td>colspan="4"></td>
</tr>
{{end}}
<tr>
<th>Keyspace</th>
<th>Sharded</th>
<th>Table Count</th>
<th>Vindex Count</th>
<th>Vindex Unknown Parameters Count</th>
<th>Error</th>
</tr>
{{range $i, $ks := .Keyspaces}} <tr>
<td>{{$ks.Keyspace}}</td>
<td>{{if $ks.Sharded}}Yes{{else}}No{{end}}</td>
<td>{{$ks.TableCount}}</td>
<td>{{$ks.VindexCount}}</td>
<td>{{$ks.VindexUnknownParamsCount}}</td>
<td style="color:red">{{$ks.Error}}</td>
</tr>{{end}}
</table>
`
)