forked from prymitive/karma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautocomplete.go
93 lines (74 loc) · 1.95 KB
/
autocomplete.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
package main
import (
"encoding/json"
"net/http"
"sort"
"strings"
"time"
"github.com/gin-gonic/gin"
"github.com/prymitive/karma/internal/alertmanager"
log "github.com/sirupsen/logrus"
)
// knownLabelNames allows querying known label names
func knownLabelNames(c *gin.Context) {
noCache(c)
start := time.Now()
cacheKey := c.Request.RequestURI
data, found := apiCache.Get(cacheKey)
if found {
c.Data(http.StatusOK, gin.MIMEJSON, data.([]byte))
logAlertsView(c, "HIT", time.Since(start))
return
}
labels := alertmanager.DedupKnownLabels()
acData := []string{}
term, found := c.GetQuery("term")
if !found || term == "" {
// return everything
sort.Strings(labels)
acData = labels
} else {
// return what matches
for _, key := range labels {
if strings.Contains(key, term) {
acData = append(acData, key)
}
}
sort.Strings(acData)
}
data, err := json.Marshal(acData)
if err != nil {
log.Error(err.Error())
panic(err)
}
apiCache.Set(cacheKey, data, time.Second*15)
c.Data(http.StatusOK, gin.MIMEJSON, data.([]byte))
logAlertsView(c, "MIS", time.Since(start))
}
func knownLabelValues(c *gin.Context) {
noCache(c)
start := time.Now()
cacheKey := c.Request.RequestURI
data, found := apiCache.Get(cacheKey)
if found {
c.Data(http.StatusOK, gin.MIMEJSON, data.([]byte))
logAlertsView(c, "HIT", time.Since(start))
return
}
name, found := c.GetQuery("name")
if !found || name == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "missing name=<token> parameter"})
log.Infof("[%s] <%d> %s %s took %s", c.ClientIP(), http.StatusBadRequest, c.Request.Method, c.Request.RequestURI, time.Since(start))
return
}
values := alertmanager.DedupKnownLabelValues(name)
sort.Strings(values)
data, err := json.Marshal(values)
if err != nil {
log.Error(err.Error())
panic(err)
}
apiCache.Set(cacheKey, data, time.Second*15)
c.Data(http.StatusOK, gin.MIMEJSON, data.([]byte))
logAlertsView(c, "MIS", time.Since(start))
}