Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract status page templates to files #147

Merged
merged 1 commit into from
Mar 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 6 additions & 146 deletions kv/memberlist/kv_init_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package memberlist

import (
"context"
_ "embed"
"encoding/json"
"fmt"
"html/template"
Expand Down Expand Up @@ -183,7 +184,7 @@ func (kvs *KVInitService) ServeHTTP(w http.ResponseWriter, req *http.Request) {
return
}

err := pageTemplate.Execute(w, v)
err := defaultPageTemplate.Execute(w, v)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
Expand Down Expand Up @@ -285,149 +286,8 @@ type pageData struct {
ReceivedMessages []message
}

var pageTemplate = template.Must(template.New("webpage").Funcs(template.FuncMap{
//go:embed status.gohtml
var defaultPageContent string
var defaultPageTemplate = template.Must(template.New("webpage").Funcs(template.FuncMap{
"StringsJoin": strings.Join,
}).Parse(pageContent))

const pageContent = `
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Memberlist Status</title>
</head>
<body>
<h1>Memberlist Status</h1>
<p>Current time: {{ .Now }}</p>

<ul>
<li>Health Score: {{ .Memberlist.GetHealthScore }} (lower = better, 0 = healthy)</li>
<li>Members: {{ .Memberlist.NumMembers }}</li>
</ul>

<h2>KV Store</h2>

<table width="100%" border="1">
<thead>
<tr>
<th>Key</th>
<th>Value Details</th>
<th>Actions</th>
</tr>
</thead>

<tbody>
{{ range $k, $v := .Store }}
<tr>
<td>{{ $k }}</td>
<td>{{ $v }}</td>
<td>
<a href="?viewKey={{ $k }}&format=json">json</a>
| <a href="?viewKey={{ $k }}&format=json-pretty">json-pretty</a>
| <a href="?viewKey={{ $k }}&format=struct">struct</a>
| <a href="?downloadKey={{ $k }}">download</a>
</td>
</tr>
{{ end }}
</tbody>
</table>

<p>Note that value "version" is node-specific. It starts with 0 (on restart), and increases on each received update. Size is in bytes.</p>

<h2>Memberlist Cluster Members</h2>

<table width="100%" border="1">
<thead>
<tr>
<th>Name</th>
<th>Address</th>
<th>State</th>
</tr>
</thead>

<tbody>
{{ range .SortedMembers }}
<tr>
<td>{{ .Name }}</td>
<td>{{ .Address }}</td>
<td>{{ .State }}</td>
</tr>
{{ end }}
</tbody>
</table>

<p>State: 0 = Alive, 1 = Suspect, 2 = Dead, 3 = Left</p>

<h2>Received Messages</h2>

<a href="?deleteMessages=true">Delete All Messages (received and sent)</a>

<table width="100%" border="1">
<thead>
<tr>
<th>ID</th>
<th>Time</th>
<th>Key</th>
<th>Value in the Message</th>
<th>Version After Update (0 = no change)</th>
<th>Changes</th>
<th>Actions</th>
</tr>
</thead>

<tbody>
{{ range .ReceivedMessages }}
<tr>
<td>{{ .ID }}</td>
<td>{{ .Time.Format "15:04:05.000" }}</td>
<td>{{ .Pair.Key }}</td>
<td>size: {{ .Pair.Value | len }}, codec: {{ .Pair.Codec }}</td>
<td>{{ .Version }}</td>
<td>{{ StringsJoin .Changes ", " }}</td>
<td>
<a href="?viewMsg={{ .ID }}&format=json">json</a>
| <a href="?viewMsg={{ .ID }}&format=json-pretty">json-pretty</a>
| <a href="?viewMsg={{ .ID }}&format=struct">struct</a>
</td>
</tr>
{{ end }}
</tbody>
</table>

<h2>Sent Messages</h2>

<a href="?deleteMessages=true">Delete All Messages (received and sent)</a>

<table width="100%" border="1">
<thead>
<tr>
<th>ID</th>
<th>Time</th>
<th>Key</th>
<th>Value</th>
<th>Version</th>
<th>Changes</th>
<th>Actions</th>
</tr>
</thead>

<tbody>
{{ range .SentMessages }}
<tr>
<td>{{ .ID }}</td>
<td>{{ .Time.Format "15:04:05.000" }}</td>
<td>{{ .Pair.Key }}</td>
<td>size: {{ .Pair.Value | len }}, codec: {{ .Pair.Codec }}</td>
<td>{{ .Version }}</td>
<td>{{ StringsJoin .Changes ", " }}</td>
<td>
<a href="?viewMsg={{ .ID }}&format=json">json</a>
| <a href="?viewMsg={{ .ID }}&format=json-pretty">json-pretty</a>
| <a href="?viewMsg={{ .ID }}&format=struct">struct</a>
</td>
</tr>
{{ end }}
</tbody>
</table>
</body>
</html>`
}).Parse(defaultPageContent))
2 changes: 1 addition & 1 deletion kv/memberlist/kv_init_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestPage(t *testing.T) {
_ = ml.Shutdown()
})

require.NoError(t, pageTemplate.Execute(&bytes.Buffer{}, pageData{
require.NoError(t, defaultPageTemplate.Execute(&bytes.Buffer{}, pageData{
Now: time.Now(),
Memberlist: ml,
SortedMembers: ml.Members(),
Expand Down
143 changes: 143 additions & 0 deletions kv/memberlist/status.gohtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
{{- /*gotype: github.com/grafana/dskit/kv/memberlist.statusPageData */ -}}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Memberlist Status</title>
</head>
<body>
<h1>Memberlist Status</h1>
<p>Current time: {{ .Now }}</p>

<ul>
<li>Health Score: {{ .Memberlist.GetHealthScore }} (lower = better, 0 = healthy)</li>
<li>Members: {{ .Memberlist.NumMembers }}</li>
</ul>

<h2>KV Store</h2>

<table width="100%" border="1">
<thead>
<tr>
<th>Key</th>
<th>Value Details</th>
<th>Actions</th>
</tr>
</thead>

<tbody>
{{ range $k, $v := .Store }}
<tr>
<td>{{ $k }}</td>
<td>{{ $v }}</td>
<td>
<a href="?viewKey={{ $k }}&format=json">json</a>
| <a href="?viewKey={{ $k }}&format=json-pretty">json-pretty</a>
| <a href="?viewKey={{ $k }}&format=struct">struct</a>
| <a href="?downloadKey={{ $k }}">download</a>
</td>
</tr>
{{ end }}
</tbody>
</table>

<p>Note that value "version" is node-specific. It starts with 0 (on restart), and increases on each received update.
Size is in bytes.</p>

<h2>Memberlist Cluster Members</h2>

<table width="100%" border="1">
<thead>
<tr>
<th>Name</th>
<th>Address</th>
<th>State</th>
</tr>
</thead>

<tbody>
{{ range .SortedMembers }}
<tr>
<td>{{ .Name }}</td>
<td>{{ .Address }}</td>
<td>{{ .State }}</td>
</tr>
{{ end }}
</tbody>
</table>

<p>State: 0 = Alive, 1 = Suspect, 2 = Dead, 3 = Left</p>

<h2>Received Messages</h2>

<a href="?deleteMessages=true">Delete All Messages (received and sent)</a>

<table width="100%" border="1">
<thead>
<tr>
<th>ID</th>
<th>Time</th>
<th>Key</th>
<th>Value in the Message</th>
<th>Version After Update (0 = no change)</th>
<th>Changes</th>
<th>Actions</th>
</tr>
</thead>

<tbody>
{{ range .ReceivedMessages }}
<tr>
<td>{{ .ID }}</td>
<td>{{ .Time.Format "15:04:05.000" }}</td>
<td>{{ .Pair.Key }}</td>
<td>size: {{ .Pair.Value | len }}, codec: {{ .Pair.Codec }}</td>
<td>{{ .Version }}</td>
<td>{{ StringsJoin .Changes ", " }}</td>
<td>
<a href="?viewMsg={{ .ID }}&format=json">json</a>
| <a href="?viewMsg={{ .ID }}&format=json-pretty">json-pretty</a>
| <a href="?viewMsg={{ .ID }}&format=struct">struct</a>
</td>
</tr>
{{ end }}
</tbody>
</table>

<h2>Sent Messages</h2>

<a href="?deleteMessages=true">Delete All Messages (received and sent)</a>

<table width="100%" border="1">
<thead>
<tr>
<th>ID</th>
<th>Time</th>
<th>Key</th>
<th>Value</th>
<th>Version</th>
<th>Changes</th>
<th>Actions</th>
</tr>
</thead>

<tbody>
{{ range .SentMessages }}
<tr>
<td>{{ .ID }}</td>
<td>{{ .Time.Format "15:04:05.000" }}</td>
<td>{{ .Pair.Key }}</td>
<td>size: {{ .Pair.Value | len }}, codec: {{ .Pair.Codec }}</td>
<td>{{ .Version }}</td>
<td>{{ StringsJoin .Changes ", " }}</td>
<td>
<a href="?viewMsg={{ .ID }}&format=json">json</a>
| <a href="?viewMsg={{ .ID }}&format=json-pretty">json-pretty</a>
| <a href="?viewMsg={{ .ID }}&format=struct">struct</a>
</td>
</tr>
{{ end }}
</tbody>
</table>
</body>
</html>
Loading