-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathstats_handler.go
91 lines (86 loc) · 2.48 KB
/
stats_handler.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
/*
* Copyright 2022-present Kuei-chun Chen. All rights reserved.
* stats_handler.go
*/
package hatchet
import (
"encoding/json"
"log"
"net/http"
"github.com/julienschmidt/httprouter"
)
// StatsHandler responds to API calls
func StatsHandler(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
/** APIs
* /hatchets/{hatchet}/stats/audit
* /hatchets/{hatchet}/stats/slowops
*/
hatchetName := params.ByName("hatchet")
attr := params.ByName("attr")
dbase, err := GetDatabase(hatchetName)
if err != nil {
json.NewEncoder(w).Encode(map[string]interface{}{"ok": 0, "error": err.Error()})
return
}
defer dbase.Close()
if dbase.GetVerbose() {
log.Println("StatsHandler", r.URL.Path, hatchetName, attr)
}
info := dbase.GetHatchetInfo()
summary := GetHatchetSummary(info)
download := r.URL.Query().Get("download")
if attr == "audit" {
data, err := dbase.GetAuditData()
if err != nil {
json.NewEncoder(w).Encode(map[string]interface{}{"ok": 0, "error": err.Error()})
return
}
templ, err := GetAuditTablesTemplate()
if err != nil {
json.NewEncoder(w).Encode(map[string]interface{}{"ok": 0, "error": err.Error()})
return
}
doc := map[string]interface{}{"Hatchet": hatchetName, "Info": info, "Summary": summary, "Data": data}
if err = templ.Execute(w, doc); err != nil {
json.NewEncoder(w).Encode(map[string]interface{}{"ok": 0, "error": err.Error()})
return
}
return
} else if attr == "slowops" {
collscan := false
if r.URL.Query().Get(COLLSCAN) == "true" {
collscan = true
}
var order, orderBy string
orderBy = r.URL.Query().Get("orderBy")
if orderBy == "" {
orderBy = "avg_ms"
} else if orderBy == "index" || orderBy == "_index" {
orderBy = "_index"
}
order = r.URL.Query().Get("order")
if order == "" {
if orderBy == "op" || orderBy == "ns" {
order = "ASC"
} else {
order = "DESC"
}
}
ops, err := dbase.GetSlowOps(orderBy, order, collscan)
if err != nil {
json.NewEncoder(w).Encode(map[string]interface{}{"ok": 0, "error": err.Error()})
return
}
templ, err := GetStatsTableTemplate(collscan, orderBy, download)
if err != nil {
json.NewEncoder(w).Encode(map[string]interface{}{"ok": 0, "error": err.Error()})
return
}
doc := map[string]interface{}{"Hatchet": hatchetName, "Merge": info.Merge, "Ops": ops, "Summary": summary}
if err = templ.Execute(w, doc); err != nil {
json.NewEncoder(w).Encode(map[string]interface{}{"ok": 0, "error": err.Error()})
return
}
return
}
}