-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdashboard_handler.go
More file actions
87 lines (69 loc) · 1.79 KB
/
dashboard_handler.go
File metadata and controls
87 lines (69 loc) · 1.79 KB
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
package main
import (
_ "embed"
"html/template"
"log"
"net/http"
"github.com/tinyauthapp/analytics/queries"
)
//go:embed dashboard.html
var dashboardTemplate string
type DashboardHandler struct {
queries *queries.Queries
}
type versionStats struct {
Total int
MostUsed string
VersionLabels []string
VersionValues []int
}
func NewDashboardHandler(queries *queries.Queries) *DashboardHandler {
return &DashboardHandler{
queries: queries,
}
}
func (h *DashboardHandler) compileVersionStats(instances []queries.Instance) versionStats {
stats := make(map[string]int)
total := 0
for _, instance := range instances {
stats[instance.Version]++
total++
}
mostUsed := "unknown"
maxCount := 0
versionLabels := make([]string, 0, len(stats))
versionValues := make([]int, 0, len(stats))
for version, count := range stats {
if count > maxCount {
maxCount = count
mostUsed = version
}
versionLabels = append(versionLabels, version)
versionValues = append(versionValues, count)
}
return versionStats{
Total: total,
MostUsed: mostUsed,
VersionLabels: versionLabels,
VersionValues: versionValues,
}
}
func (h *DashboardHandler) Dashboard(w http.ResponseWriter, r *http.Request) {
instances, err := h.queries.GetAllInstances(r.Context())
if err != nil {
log.Printf("failed to get instances: %v", err)
http.Error(w, "Failed to retrieve instances", http.StatusInternalServerError)
return
}
versionStats := h.compileVersionStats(instances)
tmpl, err := template.New("dashboard").Parse(dashboardTemplate)
if err != nil {
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}
err = tmpl.Execute(w, versionStats)
if err != nil {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
}