-
Notifications
You must be signed in to change notification settings - Fork 24
/
charts_handler.go
240 lines (230 loc) · 7.58 KB
/
charts_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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
* Copyright 2022-present Kuei-chun Chen. All rights reserved.
* charts_handler.go
*/
package hatchet
import (
"encoding/json"
"fmt"
"log"
"net/http"
"strings"
"github.com/julienschmidt/httprouter"
)
const (
BAR_CHART = "bar_chart"
BUBBLE_CHART = "bubble_chart"
PIE_CHART = "pie_chart"
T_OPS = "ops"
T_RESLEN_UP = "reslen-ip"
T_OPS_COUNTS = "ops-counts"
T_CONNS_ACCEPTED = "connections-accepted"
T_CONNS_TIME = "connections-time"
T_CONNS_TOTAL = "connections-total"
T_RESLEN_NS = "reslen-ns"
)
type Chart struct {
Index int
Title string
Descr string
URL string
}
var charts = map[string]Chart{
"instruction": {0, "select a chart", "", ""},
T_OPS: {1, "Average Operation Time",
"Display average operations time over a period of time", "/ops?type=stats"},
T_OPS_COUNTS: {2, "Operation Counts",
"Display total counts of operations", "/ops?type=counts"},
T_CONNS_TIME: {3, "Average Connections",
"Display accepted vs ended connections over a period of time", "/connections?type=time"},
T_CONNS_ACCEPTED: {4, "Accepted Connections",
"Display accepted connections from clients", "/connections?type=accepted"},
T_CONNS_TOTAL: {5, "Accepted & Ended from IPs",
"Display accepted vs ended connections by client IPs", "/connections?type=total"},
T_RESLEN_UP: {6, "Response Length by IPs ",
"Display total response length by client IPs", "/reslen-ip?ip="},
T_RESLEN_NS: {7, "Response Length by Namespaces ",
"Display total response length by namespaces", "/reslen-ns?ns="},
}
// ChartsHandler responds to charts API calls
func ChartsHandler(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
/** APIs
* /hatchets/{hatchet}/charts/ops
*/
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("ChartsHandler", r.URL.Path, hatchetName, attr)
}
info := dbase.GetHatchetInfo()
summary := GetHatchetSummary(info)
start, end := getStartEndDates(fmt.Sprintf("%v,%v", info.Start, info.End))
duration := r.URL.Query().Get("duration")
if duration != "" {
start, end = getStartEndDates(duration)
}
if attr == T_OPS {
chartType := r.URL.Query().Get("type")
op := r.URL.Query().Get("op")
if chartType == "stats" {
chartType := T_OPS
docs, err := dbase.GetAverageOpTime(op, duration)
if len(docs) > 0 {
start = docs[0].Date
end = docs[len(docs)-1].Date
}
if err != nil {
json.NewEncoder(w).Encode(map[string]interface{}{"ok": 0, "error": err.Error()})
return
}
templ, err := GetChartTemplate(BUBBLE_CHART)
if err != nil {
json.NewEncoder(w).Encode(map[string]interface{}{"ok": 0, "error": err.Error()})
return
}
doc := map[string]interface{}{"Hatchet": hatchetName, "OpCounts": docs, "Chart": charts[chartType],
"Type": chartType, "Summary": summary, "Start": start, "End": end, "VAxisLabel": "seconds"}
if err = templ.Execute(w, doc); err != nil {
json.NewEncoder(w).Encode(map[string]interface{}{"ok": 0, "error": err.Error()})
return
}
} else if chartType == "counts" {
chartType = T_OPS_COUNTS
docs, err := dbase.GetOpsCounts(duration)
if err != nil {
json.NewEncoder(w).Encode(map[string]interface{}{"ok": 0, "error": err.Error()})
return
}
templ, err := GetChartTemplate(PIE_CHART)
if err != nil {
json.NewEncoder(w).Encode(map[string]interface{}{"ok": 0, "error": err.Error()})
return
}
doc := map[string]interface{}{"Hatchet": hatchetName, "NameValues": docs, "Chart": charts[chartType],
"Type": chartType, "Summary": summary, "Start": start, "End": end}
if err = templ.Execute(w, doc); err != nil {
json.NewEncoder(w).Encode(map[string]interface{}{"ok": 0, "error": err.Error()})
return
}
return
}
return
} else if attr == "connections" {
chartType := r.URL.Query().Get("type")
if dbase.GetVerbose() {
log.Println("type", chartType, "duration", duration)
}
if chartType == "" || chartType == "accepted" {
chartType = T_CONNS_ACCEPTED
docs, err := dbase.GetAcceptedConnsCounts(duration)
if err != nil {
json.NewEncoder(w).Encode(map[string]interface{}{"ok": 0, "error": err.Error()})
return
}
templ, err := GetChartTemplate(PIE_CHART)
if err != nil {
json.NewEncoder(w).Encode(map[string]interface{}{"ok": 0, "error": err.Error()})
return
}
doc := map[string]interface{}{"Hatchet": hatchetName, "NameValues": docs, "Chart": charts[chartType],
"Type": chartType, "Summary": summary, "Start": start, "End": end}
if err = templ.Execute(w, doc); err != nil {
json.NewEncoder(w).Encode(map[string]interface{}{"ok": 0, "error": err.Error()})
return
}
return
} else { // type is time or total
docs, err := dbase.GetConnectionStats(chartType, duration)
if err != nil {
json.NewEncoder(w).Encode(map[string]interface{}{"ok": 0, "error": err.Error()})
return
}
chartType = "connections-" + chartType
templ, err := GetChartTemplate(BAR_CHART)
if err != nil {
json.NewEncoder(w).Encode(map[string]interface{}{"ok": 0, "error": err.Error()})
return
}
doc := map[string]interface{}{"Hatchet": hatchetName, "Remote": docs, "Chart": charts[chartType],
"Type": chartType, "Summary": summary, "Start": start, "End": end}
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 == T_RESLEN_UP {
ip := r.URL.Query().Get("ip")
chartType := attr
if dbase.GetVerbose() {
log.Println("type", chartType, "duration", duration)
}
docs, err := dbase.GetReslenByIP(ip, duration)
if err != nil {
json.NewEncoder(w).Encode(map[string]interface{}{"ok": 0, "error": err.Error()})
return
}
templ, err := GetChartTemplate(PIE_CHART)
if err != nil {
json.NewEncoder(w).Encode(map[string]interface{}{"ok": 0, "error": err.Error()})
return
}
chart := charts[chartType]
if ip != "" {
chart.Title += fmt.Sprintf(" (%v)", ip)
}
doc := map[string]interface{}{"Hatchet": hatchetName, "NameValues": docs, "Chart": chart,
"Type": chartType, "Summary": summary, "Start": start, "End": end}
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 == T_RESLEN_NS {
ns := r.URL.Query().Get("ns")
chartType := attr
if dbase.GetVerbose() {
log.Println("type", chartType, "duration", duration)
}
docs, err := dbase.GetReslenByNamespace(ns, duration)
if err != nil {
json.NewEncoder(w).Encode(map[string]interface{}{"ok": 0, "error": err.Error()})
return
}
templ, err := GetChartTemplate(PIE_CHART)
if err != nil {
json.NewEncoder(w).Encode(map[string]interface{}{"ok": 0, "error": err.Error()})
return
}
chart := charts[chartType]
if ns != "" {
chart.Title += fmt.Sprintf(" (%v)", ns)
}
doc := map[string]interface{}{"Hatchet": hatchetName, "NameValues": docs, "Chart": chart,
"Type": chartType, "Summary": summary, "Start": start, "End": end}
if err = templ.Execute(w, doc); err != nil {
json.NewEncoder(w).Encode(map[string]interface{}{"ok": 0, "error": err.Error()})
return
}
return
}
}
func getStartEndDates(duration string) (string, string) {
var start, end string
toks := strings.Split(duration, ",")
if len(toks) == 2 {
if len(toks[0]) >= 16 {
start = toks[0][:16]
}
if len(toks[1]) >= 16 {
end = toks[1][:16]
}
}
return start, end
}