forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
status.go
149 lines (132 loc) · 4.48 KB
/
status.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
// 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 status defines a few useful functions for our binaries,
// mainly to link the status page with a vtctld instance.
package status
import (
"flag"
"fmt"
"html/template"
"net/url"
"strings"
"github.com/youtube/vitess/go/vt/servenv"
"github.com/youtube/vitess/go/vt/topo"
pb "github.com/youtube/vitess/go/vt/proto/topodata"
)
var (
vtctldAddr = flag.String("vtctld_addr", "", "address of a vtctld instance")
)
// MakeVtctldRedirect returns an absolute vtctld url that will
// redirect to the page for the topology object specified in q.
func MakeVtctldRedirect(text string, q map[string]string) template.HTML {
query := url.Values{}
for k, v := range q {
query.Set(k, v)
}
url := "/explorers/redirect" + "?" + query.Encode()
return VtctldLink(text, url)
}
// VtctldLink returns the HTML to display a link to the fully
// qualified vtctld url whose path is given as parameter.
// If no vtctld_addr flag was passed in, we just return the text with no link.
func VtctldLink(text, urlPath string) template.HTML {
if *vtctldAddr == "" {
return template.HTML(text)
}
var fullURL string
if strings.HasSuffix(*vtctldAddr, "/") {
fullURL = *vtctldAddr + urlPath
} else {
fullURL = *vtctldAddr + "/" + urlPath
}
return template.HTML(fmt.Sprintf(`<a href="%v">%v</a>`, fullURL, text))
}
// VtctldKeyspace returns the keyspace name, possibly linked to the
// keyspace page in vtctld.
func VtctldKeyspace(keyspace string) template.HTML {
return MakeVtctldRedirect(keyspace,
map[string]string{
"type": "keyspace",
"keyspace": keyspace,
})
}
// VtctldShard returns the shard name, possibly linked to the shard
// page in vtctld.
func VtctldShard(keyspace, shard string) template.HTML {
return MakeVtctldRedirect(shard, map[string]string{
"type": "shard",
"keyspace": keyspace,
"shard": shard,
})
}
// VtctldSrvCell returns the cell name, possibly linked to the
// serving graph page in vtctld for that page.
func VtctldSrvCell(cell string) template.HTML {
return VtctldLink(cell, "/serving_graph/"+cell)
}
// VtctldSrvKeyspace returns the keyspace name, possibly linked to the
// SrvKeyspace page in vtctld.
func VtctldSrvKeyspace(cell, keyspace string) template.HTML {
return MakeVtctldRedirect(keyspace, map[string]string{
"type": "srv_keyspace",
"cell": cell,
"keyspace": keyspace,
})
}
// VtctldSrvShard returns the shard name, possibly linked to the
// SrvShard page in vtctld.
func VtctldSrvShard(cell, keyspace, shard string) template.HTML {
return MakeVtctldRedirect(shard, map[string]string{
"type": "srv_shard",
"cell": cell,
"keyspace": keyspace,
"shard": shard,
})
}
// VtctldSrvType returns the tablet type, possibly linked to the
// EndPoints page in vtctld.
func VtctldSrvType(cell, keyspace, shard string, tabletType pb.TabletType) template.HTML {
strTabletType := strings.ToLower(tabletType.String())
if !topo.IsInServingGraph(tabletType) {
return template.HTML(strTabletType)
}
return MakeVtctldRedirect(strTabletType, map[string]string{
"type": "srv_type",
"cell": cell,
"keyspace": keyspace,
"shard": shard,
"tablet_type": strTabletType,
})
}
// VtctldReplication returns 'cell/keyspace/shard', possibly linked to the
// ShardReplication page in vtctld.
func VtctldReplication(cell, keyspace, shard string) template.HTML {
return MakeVtctldRedirect(fmt.Sprintf("%v/%v/%v", cell, keyspace, shard),
map[string]string{
"type": "replication",
"keyspace": keyspace,
"shard": shard,
"cell": cell,
})
}
// VtctldTablet returns the tablet alias, possibly linked to the
// Tablet page in vtctld.
func VtctldTablet(aliasName string) template.HTML {
return MakeVtctldRedirect(aliasName, map[string]string{
"type": "tablet",
"alias": aliasName,
})
}
func init() {
servenv.AddStatusFuncs(template.FuncMap{
"github_com_youtube_vitess_vtctld_keyspace": VtctldKeyspace,
"github_com_youtube_vitess_vtctld_shard": VtctldShard,
"github_com_youtube_vitess_vtctld_srv_cell": VtctldSrvCell,
"github_com_youtube_vitess_vtctld_srv_keyspace": VtctldSrvKeyspace,
"github_com_youtube_vitess_vtctld_srv_shard": VtctldSrvShard,
"github_com_youtube_vitess_vtctld_srv_type": VtctldSrvType,
"github_com_youtube_vitess_vtctld_replication": VtctldReplication,
"github_com_youtube_vitess_vtctld_tablet": VtctldTablet,
})
}