-
-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathlogs_handler.go
More file actions
107 lines (102 loc) · 3.08 KB
/
logs_handler.go
File metadata and controls
107 lines (102 loc) · 3.08 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright 2022-present Kuei-chun Chen. All rights reserved.
package hatchet
import (
"fmt"
"log"
"net/http"
"strings"
"github.com/julienschmidt/httprouter"
)
// LogsHandler responds to charts API calls
func LogsHandler(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
/** APIs
* /hatchets/{hatchet}/logs/all
* /hatchets/{hatchet}/logs/slowops
*/
hatchetName := params.ByName("hatchet")
attr := params.ByName("attr")
dbase, err := GetDatabase(hatchetName)
if err != nil {
renderErrorPage(w, r, hatchetName, err.Error())
return
}
defer dbase.Close()
if dbase.GetVerbose() {
log.Println("LogsHandler", r.URL.Path, hatchetName, attr)
}
info := dbase.GetHatchetInfo()
summary := GetHatchetSummary(info)
duration := r.URL.Query().Get("duration")
if attr == "all" {
var hasMore bool
component := r.URL.Query().Get("component")
context := r.URL.Query().Get("context")
severity := r.URL.Query().Get("severity")
limit := r.URL.Query().Get("limit")
if limit == "" {
limit = fmt.Sprintf("%v", LIMIT)
}
offset, nlimit := GetOffsetLimit(limit)
searchOpts := []string{
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),
}
logs, err := dbase.GetLogs(searchOpts...)
if err != nil {
renderErrorPage(w, r, hatchetName, err.Error())
return
}
// Get total count for search results
totalCount, _ := dbase.CountLogs(searchOpts...)
templ, err := GetLogTableTemplate(attr, "")
if err != nil {
renderErrorPage(w, r, hatchetName, err.Error())
return
}
toks := strings.Split(limit, ",")
seq := 1
if len(toks) > 1 {
seq = ToInt(toks[0]) + 1
}
hasMore = len(logs) > nlimit
if hasMore {
logs = logs[:len(logs)-1]
}
limit = fmt.Sprintf("%v,%v", offset+nlimit, nlimit)
url := fmt.Sprintf("%v?component=%v&context=%v&severity=%v&duration=%v&limit=%v", r.URL.Path,
component, context, severity, duration, limit)
doc := map[string]interface{}{"Hatchet": hatchetName, "Merge": info.Merge, "Logs": logs, "Seq": seq,
"Summary": summary, "Context": context, "Component": component, "Severity": severity,
"HasMore": hasMore, "URL": url, "TotalCount": totalCount, "Version": GetLogv2().version}
if err = templ.Execute(w, doc); err != nil {
renderErrorPage(w, r, hatchetName, err.Error())
return
}
return
} else if attr == "slowops" {
topN := ToInt(r.URL.Query().Get("topN"))
download := r.URL.Query().Get("download")
if topN == 0 {
topN = TOP_N
}
logstrs, err := dbase.GetSlowestLogs(topN)
if err != nil {
renderErrorPage(w, r, hatchetName, err.Error())
return
}
templ, err := GetLogTableTemplate(attr, download)
if err != nil {
renderErrorPage(w, r, hatchetName, err.Error())
return
}
doc := map[string]interface{}{"Hatchet": hatchetName, "Merge": info.Merge, "Logs": logstrs, "Summary": summary, "Version": GetLogv2().version}
if err = templ.Execute(w, doc); err != nil {
renderErrorPage(w, r, hatchetName, err.Error())
return
}
return
}
}