-
Notifications
You must be signed in to change notification settings - Fork 24
/
api_handler.go
114 lines (109 loc) · 3.63 KB
/
api_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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright 2022-present Kuei-chun Chen. All rights reserved.
package hatchet
import (
"encoding/json"
"fmt"
"log"
"net/http"
"github.com/julienschmidt/httprouter"
)
// APIHandler responds to API calls
func APIHandler(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
/** APIs
* /api/hatchet/v1.0/hatchets/{hatchet}/logs/all
* /api/hatchet/v1.0/hatchets/{hatchet}/logs/slowops
* /api/hatchet/v1.0/hatchets/{hatchet}/stats/slowops
*/
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
hatchetName := params.ByName("hatchet")
attr := params.ByName("attr")
category := params.ByName("category")
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("LogsHandler", r.URL.Path, hatchetName, attr)
}
if category == "stats" && attr == "slowops" {
orderBy := r.URL.Query().Get("orderBy")
if orderBy == "" {
orderBy = "avg_ms"
}
ops, err := dbase.GetSlowOps(orderBy, "DESC", false)
if err != nil {
json.NewEncoder(w).Encode(map[string]interface{}{"ok": 0, "error": err.Error()})
}
doc := map[string]interface{}{"hatchet": hatchetName, "has_more": false, "offset": 0, "limit": len(ops), "ops": ops}
b, err := json.Marshal(doc)
if err != nil {
json.NewEncoder(w).Encode(map[string]interface{}{"ok": 0, "error": err.Error()})
} else {
w.Write(b)
}
return
} else if category == "stats" && attr == "audit" {
data, err := dbase.GetAuditData()
if err != nil {
json.NewEncoder(w).Encode(map[string]interface{}{"ok": 0, "error": err.Error()})
}
doc := map[string]interface{}{"hatchet": hatchetName, "audit": data}
b, err := json.Marshal(doc)
if err != nil {
json.NewEncoder(w).Encode(map[string]interface{}{"ok": 0, "error": err.Error()})
} else {
w.Write(b)
}
return
} else if category == "logs" && attr == "slowops" {
topN := ToInt(r.URL.Query().Get("topN"))
if topN == 0 {
topN = TOP_N
}
logs, err := dbase.GetSlowestLogs(topN)
if err != nil {
json.NewEncoder(w).Encode(map[string]interface{}{"ok": 0, "error": err.Error()})
}
doc := map[string]interface{}{"hatchet": hatchetName, "has_more": false, "offset": 0, "limit": len(logs), "logs": logs}
b, err := json.Marshal(doc)
if err != nil {
json.NewEncoder(w).Encode(map[string]interface{}{"ok": 0, "error": err.Error()})
} else {
w.Write(b)
}
return
} else if category == "logs" && attr == "all" {
var hasMore bool
component := r.URL.Query().Get("component")
context := r.URL.Query().Get("context")
severity := r.URL.Query().Get("severity")
duration := r.URL.Query().Get("duration")
limit := r.URL.Query().Get("limit")
if limit == "" {
limit = fmt.Sprintf("%v", LIMIT)
}
offset, nlimit := GetOffsetLimit(limit)
logs, err := dbase.GetLogs(fmt.Sprintf("component=%v", component), fmt.Sprintf("limit=%v", limit),
fmt.Sprintf("context=%v", context), fmt.Sprintf("severity=%v", severity), fmt.Sprintf("duration=%v", duration))
if err != nil {
json.NewEncoder(w).Encode(map[string]interface{}{"ok": 0, "error": err.Error()})
return
}
hasMore = len(logs) > nlimit
if hasMore {
logs = logs[:len(logs)-1]
}
var b []byte
doc := map[string]interface{}{"hatchet": hatchetName, "has_more": hasMore, "offset": offset, "limit": len(logs), "logs": logs}
if b, err = json.Marshal(doc); err != nil {
json.NewEncoder(w).Encode(map[string]interface{}{"ok": 0, "error": err.Error()})
return
}
w.Write(b)
return
}
json.NewEncoder(w).Encode(map[string]interface{}{"ok": 1, "message": "Hello Hatchet API!"})
}