-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.go
101 lines (91 loc) · 2.47 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
package impl
import (
"fmt"
"log"
"net/http"
"sort"
"time"
"github.com/nopsleds/pingo/impl/timeserie"
)
const dateFormat = "Jan 02, 2006 15:04:05 UTC"
type IndexDataTarget struct {
Name string
Type string
LastCheck string
LastChange string
Status string
Class string
Reason string
Timeseries map[string][]timeserie.Entry
}
type IndexData struct {
Now string
Targets []IndexDataTarget
}
// ByAge implements sort.Interface for []Person based on
// the Age field.
type ByName []IndexDataTarget
func (a ByName) Len() int { return len(a) }
func (a ByName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByName) Less(i, j int) bool { return a[i].Name < a[j].Name }
func statusToLabel(status TargetStatus) string {
switch status {
case TargetStatusOk:
return "OK"
case TargetStatusError:
return "Error"
default:
return "Unknown"
}
}
func statusToClass(status TargetStatus) string {
switch status {
case TargetStatusOk:
return "ok"
case TargetStatusError:
return "error"
default:
return "unknown"
}
}
func handleGetIndex(instance *PingoInstance) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
var dataTargets []IndexDataTarget
for targetName, targetState := range instance.State() {
timeseries := make(map[string][]timeserie.Entry)
for k, ts := range targetState.Timeseries {
timeseries[k] = ts.Get()
}
dataTargets = append(dataTargets, IndexDataTarget{
Name: targetName,
Type: targetState.Config.Type,
LastCheck: targetState.LastCheck.Format(dateFormat),
LastChange: targetState.LastChange.Format(dateFormat),
Status: statusToLabel(targetState.Status),
Class: statusToClass(targetState.Status),
Reason: targetState.Reason,
Timeseries: timeseries,
})
}
sort.Sort(ByName(dataTargets))
data := IndexData{
Now: time.Now().Format(dateFormat),
Targets: dataTargets,
}
err := tplIndex.Execute(w, data)
if err != nil {
panic(err)
}
}
}
func handleGetStyleCss(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "text/css")
tplStyleCss.Execute(w, nil)
}
func RunWeb(config ConfigWeb, instance *PingoInstance) error {
log.Printf("starting web UI on http://localhost:%d\n", config.Port)
addr := fmt.Sprintf(":%d", config.Port)
http.HandleFunc("/style.css", handleGetStyleCss)
http.HandleFunc("/", handleGetIndex(instance))
return http.ListenAndServe(addr, nil)
}