-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.go
142 lines (122 loc) · 2.98 KB
/
http.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
package heatmap
import (
"encoding/json"
"net/http"
"sort"
"strings"
)
type httpServer struct {
storage datastore
config *config
}
type metricsFindReturn struct {
Text string `json:"text"`
Expandable int `json:"expandable"`
Leaf int `json:"leaf"`
ID string `json:"id"`
AllowChildren int `json:"allowChildren"`
}
type renderReturn struct {
Target string `json:"target"`
Datapoints []*datapoint `json:"datapoints"`
}
func (h *httpServer) tags(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(""))
}
func (h *httpServer) version(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("1.1.5\n"))
}
func (h *httpServer) functions(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`{
"inBuckets": {
"name": "inBuckets",
"function": "inBuckets(seriesList)",
"description": "Puts metrics in buckets",
"module": "graphite.render.functions",
"group": "Buckets",
"params": [
{
"name": "seriesList",
"type": "seriesList",
"required": true
},
{
"name": "bucketCount",
"type": "string",
"required": false
},
{
"name": "yLimit",
"type": "string",
"required": false
},
{
"name": "logScale",
"type": "string",
"required": false
}
]
}
}`))
}
func lastSegment(key string) string {
i := strings.LastIndex(key, ".")
if i != -1 && i+1 < len(key) {
return key[i+1:]
}
return key
}
func boolToInt(v bool) int {
if v {
return 1
}
return 0
}
func requestParam(r *http.Request, name string) (val string) {
arr, ok := r.URL.Query()[name]
if !ok {
err := r.ParseForm()
if err != nil {
return
}
val = r.Form.Get(name)
} else {
val = arr[0]
}
return
}
type sortableMetricsFindReturn []*metricsFindReturn
func (m sortableMetricsFindReturn) Len() int {
return len(m)
}
func (m sortableMetricsFindReturn) Less(i, j int) bool {
return strings.Compare(m[i].Text, m[j].Text) < 0
}
func (m sortableMetricsFindReturn) Swap(i, j int) {
m[i], m[j] = m[j], m[i]
}
func (h *httpServer) metricsFind(w http.ResponseWriter, r *http.Request) {
results := []*metricsFindReturn{}
for _, globResult := range h.storage.Glob(requestParam(r, "query")) {
results = append(results, &metricsFindReturn{
Text: lastSegment(globResult.name),
Expandable: boolToInt(globResult.hasChildren),
Leaf: boolToInt(globResult.isLeaf),
ID: globResult.name,
AllowChildren: boolToInt(globResult.hasChildren),
})
}
sort.Sort(sortableMetricsFindReturn(results))
e := json.NewEncoder(w)
w.Header().Set("Content-Type", "application/json")
e.Encode(results)
}
// Main is the main entrypoint
func (h *httpServer) start() {
http.HandleFunc("/tags/autoComplete/tags", h.tags)
http.HandleFunc("/metrics/find", h.metricsFind)
http.HandleFunc("/functions", h.functions)
http.HandleFunc("/version", h.version)
http.HandleFunc("/render", h.renderer)
logError.Fatalln(http.ListenAndServe(h.config.httpAddr, nil))
}