forked from open-falcon-archive/graph
/
index_http.go
124 lines (107 loc) · 3.09 KB
/
index_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
package http
import (
"fmt"
"net/http"
"strconv"
"strings"
"github.com/open-falcon/graph/index"
)
func configIndexRoutes() {
// 触发索引全量更新, 同步操作
http.HandleFunc("/index/updateAll", func(w http.ResponseWriter, r *http.Request) {
go index.UpdateIndexAllByDefaultStep()
RenderDataJson(w, "ok")
})
// 获取索引全量更新的并行数
http.HandleFunc("/index/updateAll/concurrent", func(w http.ResponseWriter, r *http.Request) {
RenderDataJson(w, index.GetConcurrentOfUpdateIndexAll())
})
// 更新一条索引数据,用于手动建立索引 endpoint metric step dstype tags
http.HandleFunc("/index/update/", func(w http.ResponseWriter, r *http.Request) {
urlParam := r.URL.Path[len("/index/update/"):]
args := strings.Split(urlParam, "/")
argsLen := len(args)
if !(argsLen == 4 || argsLen == 5) {
RenderDataJson(w, "bad args")
return
}
endpoint := args[0]
metric := args[1]
step, _ := strconv.ParseInt(args[2], 10, 32)
dstype := args[3]
tags := make(map[string]string)
if argsLen == 5 {
tagVals := strings.Split(args[4], ",")
for _, tag := range tagVals {
tagPairs := strings.Split(tag, "=")
if len(tagPairs) == 2 {
tags[tagPairs[0]] = tagPairs[1]
}
}
}
err := index.UpdateIndexOne(endpoint, metric, tags, dstype, int(step))
if err != nil {
RenderDataJson(w, fmt.Sprintf("%v", err))
return
}
RenderDataJson(w, "ok")
})
// index.cached
http.HandleFunc("/index/cache/", func(w http.ResponseWriter, r *http.Request) {
urlParam := r.URL.Path[len("/index/cache/"):]
args := strings.Split(urlParam, "/")
argsLen := len(args)
if !(argsLen == 4 || argsLen == 5) {
RenderDataJson(w, "bad args")
return
}
endpoint := args[0]
metric := args[1]
step, _ := strconv.ParseInt(args[2], 10, 32)
dstype := args[3]
tags := make(map[string]string)
if argsLen == 5 {
tagVals := strings.Split(args[4], ",")
for _, tag := range tagVals {
tagPairs := strings.Split(tag, "=")
if len(tagPairs) == 2 {
tags[tagPairs[0]] = tagPairs[1]
}
}
}
item, err := index.GetIndexedItemCache(endpoint, metric, tags, dstype, int(step))
if err != nil {
RenderDataJson(w, fmt.Sprintf("%v", err))
return
}
RenderDataJson(w, item)
})
http.HandleFunc("/v2/index/cache", func(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
if !(len(r.Form["e"]) > 0 && len(r.Form["m"]) > 0 && len(r.Form["step"]) > 0 && len(r.Form["type"]) > 0) {
RenderDataJson(w, "bad args")
return
}
endpoint := r.Form["e"][0]
metric := r.Form["m"][0]
step, _ := strconv.ParseInt(r.Form["step"][0], 10, 32)
dstype := r.Form["type"][0]
tags := make(map[string]string)
if len(r.Form["t"]) > 0 {
tagstr := r.Form["t"][0]
tagVals := strings.Split(tagstr, ",")
for _, tag := range tagVals {
tagPairs := strings.Split(tag, "=")
if len(tagPairs) == 2 {
tags[tagPairs[0]] = tagPairs[1]
}
}
}
item, err := index.GetIndexedItemCache(endpoint, metric, tags, dstype, int(step))
if err != nil {
RenderDataJson(w, fmt.Sprintf("%v", err))
return
}
RenderDataJson(w, item)
})
}