forked from fwtpe/owl-backend
/
owl_graph_controller.go
171 lines (160 loc) · 4.96 KB
/
owl_graph_controller.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
package graph
import (
"github.com/emirpasic/gods/lists/arraylist"
"fmt"
"net/http"
"regexp"
"strconv"
"strings"
h "github.com/fwtpe/owl-backend/modules/f2e-api/app/helper"
"github.com/fwtpe/owl-backend/modules/f2e-api/app/helper/filter"
m "github.com/fwtpe/owl-backend/modules/f2e-api/app/model/graph"
"github.com/fwtpe/owl-backend/modules/f2e-api/app/model/boss"
"github.com/gin-gonic/gin"
)
func HostsSearching(c *gin.Context) {
AccpectTypes := arraylist.New()
AccpectTypes.Add("platform", "idc", "isp", "province", "hostname", "hostgroup")
q := c.DefaultQuery("q", "--")
limitStr := c.DefaultQuery("limit", "100")
limit := 100
if ok, _ := regexp.MatchString(`\d+`, limitStr); ok {
limit, _ = strconv.Atoi(limitStr)
}
ftype := c.DefaultQuery("filter_type", "all")
if q == "--" {
h.JSONR(c, badstatus, "q is empty, please check it!")
return
}
if !(ftype == "all" || AccpectTypes.Contains(ftype)) {
h.JSONR(c, badstatus, fmt.Sprintf("filter_type got error type, please check it!, only support: %v", AccpectTypes))
return
}
bossList := []boss.BossHost{}
if ftype != "hostname" && ftype != "hostgroup" {
bossList = boss.GetBossObjs()
}
res := map[string]interface{}{}
switch ftype {
case "platform":
res = map[string]interface{}{
"platform": filter.PlatformFilter(bossList, q, limit),
}
case "idc":
res = map[string]interface{}{
"idc": filter.IdcFilter(bossList, q, limit),
}
case "isp":
res = map[string]interface{}{
"isp": filter.IspFilter(bossList, q, limit),
}
case "province":
res = map[string]interface{}{
"province": filter.ProvinceFilter(bossList, q, limit),
}
case "hostname":
res = map[string]interface{}{
"hostname": filter.HostFilter(q, limit),
}
case "hostgroup":
res = map[string]interface{}{
"hostgroup": filter.HostGroupFilter(q, limit),
}
case "all":
res = map[string]interface{}{
"platform": filter.PlatformFilter(bossList, q, limit),
"idc": filter.IdcFilter(bossList, q, limit),
"isp": filter.IspFilter(bossList, q, limit),
"province": filter.ProvinceFilter(bossList, q, limit),
"hostname": filter.HostFilter(q, limit),
"hostgroup": filter.HostGroupFilter(q, limit),
}
}
h.JSONR(c, res)
return
}
type APIEndpointsQuerySubMetricInputs struct {
Endpoints []string `json:"endpoints" form:"endpoints" binding:"required"`
MetricQuery string `json:"metric_query" form:"metric_query"`
RawKeys bool `json:"raw_keys" form:"raw_keys"`
}
func EndpointsQuerySubMetric(c *gin.Context) {
inputs := APIEndpointsQuerySubMetricInputs{
MetricQuery: ".+",
RawKeys: true,
}
if err := c.Bind(&inputs); err != nil {
h.JSONR(c, http.StatusBadRequest, err.Error())
return
}
enps := inputs.Endpoints
enpids := []int64{}
db.Graph.Table("endpoint").Select("id").Where("endpoint IN (?)", enps).Pluck("id", &enpids)
metricSlist := map[string]int{}
if len(enpids) != 0 {
counters := []string{}
db.Graph.Table("endpoint_counter").Where("endpoint_id IN (?) AND counter regexp ? ", enpids, inputs.MetricQuery).Pluck("DISTINCT(counter)", &counters)
if inputs.RawKeys {
h.JSONR(c, counters)
} else {
for _, ct := range counters {
c2 := getFirstCutStringOfMetric(ct)
if v, ok := metricSlist[c2]; ok {
metricSlist[c2] = v + 1
} else {
metricSlist[c2] = 1
}
}
h.JSONR(c, metricSlist)
}
} else {
h.JSONR(c, badstatus, "no metrice found")
}
return
}
type APIEndpointsGetMetricBySubStarInput struct {
Endpoints []string `json:"endpoints" form:"endpoints" binding:"required"`
MetricQuery string `json:"metric_query" form:"metric_query" binding:"required"`
StarStr []string `json:"star_strings" form:"star_strings" binding:"required"`
SelectedMetric []string `json:"selected_metric" form:"selected_metric"`
}
func EndpointsGetMetricBySubStar(c *gin.Context) {
//the same with other one , so reuse it
inputs := APIEndpointsGetMetricBySubStarInput{}
if err := c.Bind(&inputs); err != nil {
h.JSONR(c, http.StatusBadRequest, err.Error())
return
}
enps := inputs.Endpoints
enpids := []int{}
db.Graph.Table("endpoint").Select("id").Where("endpoint IN (?)", enps).Pluck("id", &enpids)
var regexFilterTmp []string
for _, n := range inputs.StarStr {
n2 := fmt.Sprintf("counter regexp '^%s\\.'", n)
regexFilterTmp = append(regexFilterTmp, n2)
}
regexFilter := strings.Join(regexFilterTmp, " OR ")
metricSlist := inputs.SelectedMetric
if len(enpids) != 0 {
var counters []m.EndpointCounter
db.Graph.Table("endpoint_counter").
Select("DISTINCT(counter) as counter").
Where(fmt.Sprintf(
"endpoint_id IN (?) AND (%s) AND counter regexp ? ", regexFilter), enpids, inputs.MetricQuery).
Scan(&counters)
for _, ct := range counters {
metricSlist = append(metricSlist, ct.Counter)
}
}
h.JSONR(c, metricSlist)
return
}
func getFirstCutStringOfMetric(c string) string {
patt := regexp.MustCompile("^([^\\.]+)\\.")
matchPatt := patt.FindStringSubmatch(c)
if len(matchPatt) == 0 {
return c
} else {
return matchPatt[1]
}
}