-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.go
126 lines (113 loc) · 2.96 KB
/
web.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
package main
import (
"context"
"html/template"
"log"
"net/http"
"github.com/childoftheuniverse/red-cloud/common"
"go.opencensus.io/trace"
)
var funcMap = map[string]interface{}{
"bytes": common.PrettyBytes,
"since": common.TimeSince,
}
var mainStatusTemplate = template.Must(
template.New("statuspage").Funcs(funcMap).Parse(`
<!DOCTYPE html PUBLIC>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>{{.Instance}} Data Node Status</title>
<link rel="stylesheet" type="text/css" href="{{ .BootstrapCSSPath }}"
integrity="{{ .BootstrapCSSHash }}" crossorigin="anonymous" />
</head>
<body>
<div class="container">
<h1>Data Node Status <small>{{.Instance}}</small></h1>
<div class="row">
<div class="col-md-12">
<h2>Loaded Tablets</h2>
<table class="table table-striped">
<thead>
<tr><th>Table</th><th>Start Key</th><th>End Key</th></tr>
</thead>
<tbody>
{{range .Tablets}}
<tr>
<td>{{ .Table }}</td>
<td>{{ .KeyRange.StartKey }}</td>
<td>{{ .KeyRange.EndKey }}</td>
</tr>
{{end}}
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>
`))
type tableKeyRange struct {
Table string
KeyRange *common.KeyRange
}
type mainStatusData struct {
BootstrapCSSPath string
BootstrapCSSHash string
Instance string
Tablets []*tableKeyRange
}
/*
StatusWebService provides a small amount of information about the current
status of the red-cloud data node as a web interface.
*/
type StatusWebService struct {
registry *ServingRangeRegistry
bootstrapCSSPath string
bootstrapCSSHash string
}
/*
NewStatusWebService creates a new status web service object which can be
exported via HTTP.
*/
func NewStatusWebService(
bootstrapCSSPath, bootstrapCSSHash string,
registry *ServingRangeRegistry) *StatusWebService {
return &StatusWebService{
registry: registry,
bootstrapCSSPath: bootstrapCSSPath,
bootstrapCSSHash: bootstrapCSSHash,
}
}
/*
ServeHTTP serves status information about the state of the caretaker node
as an HTML page over HTTP.
*/
func (s *StatusWebService) ServeHTTP(
rw http.ResponseWriter, req *http.Request) {
var span *trace.Span
var mainData = mainStatusData{
BootstrapCSSPath: s.bootstrapCSSPath,
BootstrapCSSHash: s.bootstrapCSSHash,
Instance: s.registry.Instance(),
}
var ranges []*common.KeyRange
var table string
var err error
_, span = trace.StartSpan(
context.Background(), "red-cloud.StatusWebService/ServeHTTP")
defer span.End()
for table, ranges = range s.registry.GetRanges() {
var rn *common.KeyRange
for _, rn = range ranges {
mainData.Tablets = append(mainData.Tablets, &tableKeyRange{
Table: table,
KeyRange: rn,
})
}
}
if err = mainStatusTemplate.Execute(rw, mainData); err != nil {
span.AddAttributes(trace.StringAttribute("error", err.Error()))
span.Annotate(nil, "Error executing web status page template")
log.Print("Error sending status page: ", err)
}
}