forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 4
/
endpoint.go
328 lines (295 loc) · 7.64 KB
/
endpoint.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package sqlstore
import (
"fmt"
"strings"
"time"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/events"
m "github.com/grafana/grafana/pkg/models"
)
func init() {
bus.AddHandler("sql", GetEndpoints)
bus.AddHandler("sql", GetEndpointById)
bus.AddHandler("sql", AddEndpoint)
bus.AddHandler("sql", UpdateEndpoint)
bus.AddHandler("sql", DeleteEndpoint)
bus.AddHandler("sql", GetAllEndpointTags)
}
type EndpointWithTag struct {
Id int64
OrgId int64
Name string
Slug string
Tags string
Created time.Time
Updated time.Time
}
func GetEndpointById(query *m.GetEndpointByIdQuery) error {
sess := session{Session: x.Table("endpoint")}
return GetEndpointByIdTransaction(query, &sess)
}
func GetEndpointByIdTransaction(query *m.GetEndpointByIdQuery, sess *session) error {
rawParams := make([]interface{}, 0)
rawSql := `SELECT
GROUP_CONCAT(DISTINCT(endpoint_tag.tag)) as tags,
endpoint.*
FROM endpoint
LEFT JOIN endpoint_tag ON endpoint.id = endpoint_tag.endpoint_id
WHERE endpoint.org_id=? AND endpoint.id=?
GROUP BY endpoint.id
`
rawParams = append(rawParams, query.OrgId, query.Id)
results := make([]*EndpointWithTag, 0)
err := sess.Sql(rawSql, rawParams...).Find(&results)
if err != nil {
return err
}
if len(results) < 1 {
return m.ErrEndpointNotFound
}
result := results[0]
tags := make([]string, 0)
if result.Tags != "" {
tags = strings.Split(result.Tags, ",")
}
query.Result = &m.EndpointDTO{
Id: result.Id,
OrgId: result.OrgId,
Name: result.Name,
Slug: result.Slug,
Tags: tags,
}
return nil
}
func GetEndpoints(query *m.GetEndpointsQuery) error {
sess := x.Table("endpoint")
rawParams := make([]interface{}, 0)
rawSql := `SELECT
GROUP_CONCAT(DISTINCT(endpoint_tag.tag)) as tags,
endpoint.*
FROM endpoint
LEFT JOIN endpoint_tag ON endpoint.id = endpoint_tag.endpoint_id
`
whereSql := make([]string, 0)
whereSql = append(whereSql, "endpoint.org_id=?")
rawParams = append(rawParams, query.OrgId)
if len(query.Tag) > 0 {
// this is a bit complicated because we want to
// match only endpoints that have one of the specified tags
// but we still need to return all of the tags that
// the endpoint has.
rawSql += "LEFT JOIN endpoint_tag AS et ON et.endpoint_id = endpoint.id\n"
p := make([]string, len(query.Tag))
for i, t := range query.Tag {
p[i] = "?"
rawParams = append(rawParams, t)
}
whereSql = append(whereSql, fmt.Sprintf("et.tag IN (%s)", strings.Join(p, ",")))
}
rawSql += "WHERE " + strings.Join(whereSql, " AND ")
rawSql += " GROUP BY endpoint.id"
result := make([]*EndpointWithTag, 0)
err := sess.Sql(rawSql, rawParams...).Find(&result)
if err != nil {
return err
}
endpoints := make([]*m.EndpointDTO, 0)
//iterate through all of the results and build out our model.
for _, row := range result {
tags := make([]string, 0)
if row.Tags != "" {
tags = strings.Split(row.Tags, ",")
}
endpoints = append(endpoints, &m.EndpointDTO{
Id: row.Id,
OrgId: row.OrgId,
Name: row.Name,
Slug: row.Slug,
Tags: tags,
})
}
query.Result = endpoints
return nil
}
func AddEndpoint(cmd *m.AddEndpointCommand) error {
return inTransaction2(func(sess *session) error {
endpoint := &m.Endpoint{
OrgId: cmd.OrgId,
Name: cmd.Name,
Created: time.Now(),
Updated: time.Now(),
}
endpoint.UpdateEndpointSlug()
if _, err := sess.Insert(endpoint); err != nil {
return err
}
if len(cmd.Tags) > 0 {
endpointTags := make([]m.EndpointTag, 0, len(cmd.Tags))
for _, tag := range cmd.Tags {
endpointTags = append(endpointTags, m.EndpointTag{
OrgId: cmd.OrgId,
EndpointId: endpoint.Id,
Tag: tag,
})
}
sess.Table("endpoint_tag")
if _, err := sess.Insert(&endpointTags); err != nil {
return err
}
}
cmd.Result = &m.EndpointDTO{
Id: endpoint.Id,
OrgId: endpoint.OrgId,
Name: endpoint.Name,
Slug: endpoint.Slug,
Tags: cmd.Tags,
}
sess.publishAfterCommit(&events.EndpointCreated{
EndpointPayload: events.EndpointPayload{
Id: endpoint.Id,
OrgId: endpoint.OrgId,
Name: endpoint.Name,
Slug: endpoint.Slug,
Tags: cmd.Tags,
},
Timestamp: endpoint.Updated,
})
// add any included momitors.
if len(cmd.Monitors) > 0 {
for _, monitorCmd := range cmd.Monitors {
monitorCmd.OrgId = cmd.OrgId
monitorCmd.EndpointId = endpoint.Id
if err := addMonitorTransaction(monitorCmd, sess); err != nil {
fmt.Println(err)
return err
}
}
}
return nil
})
}
func UpdateEndpoint(cmd *m.UpdateEndpointCommand) error {
return inTransaction2(func(sess *session) error {
q := m.GetEndpointByIdQuery{
Id: cmd.Id,
OrgId: cmd.OrgId,
}
err := GetEndpointById(&q)
if err != nil {
return err
}
lastState := q.Result
endpoint := &m.Endpoint{
OrgId: cmd.OrgId,
Name: cmd.Name,
Created: time.Now(),
Updated: time.Now(),
}
endpoint.UpdateEndpointSlug()
_, err = sess.Id(cmd.Id).Update(endpoint)
if err != nil {
return err
}
rawSql := "DELETE FROM endpoint_tag WHERE endpoint_id=? and org_id=?"
if _, err := sess.Exec(rawSql, cmd.Id, cmd.OrgId); err != nil {
return err
}
if len(cmd.Tags) > 0 {
endpointTags := make([]m.EndpointTag, 0, len(cmd.Tags))
for _, tag := range cmd.Tags {
endpointTags = append(endpointTags, m.EndpointTag{
OrgId: cmd.OrgId,
EndpointId: cmd.Id,
Tag: tag,
})
}
sess.Table("endpoint_tag")
if _, err := sess.Insert(&endpointTags); err != nil {
return err
}
}
cmd.Result = &m.EndpointDTO{
Id: cmd.Id,
OrgId: endpoint.OrgId,
Name: endpoint.Name,
Slug: endpoint.Slug,
Tags: cmd.Tags,
}
sess.publishAfterCommit(&events.EndpointUpdated{
EndpointPayload: events.EndpointPayload{
Id: cmd.Id,
OrgId: endpoint.OrgId,
Name: endpoint.Name,
Slug: endpoint.Slug,
Tags: cmd.Tags,
},
Timestamp: endpoint.Updated,
LastState: &events.EndpointPayload{
Id: lastState.Id,
OrgId: lastState.OrgId,
Name: lastState.Name,
Slug: lastState.Slug,
Tags: lastState.Tags,
},
})
return nil
})
}
func DeleteEndpoint(cmd *m.DeleteEndpointCommand) error {
return inTransaction2(func(sess *session) error {
monitorQuery := m.GetMonitorsQuery{
OrgId: cmd.OrgId,
EndpointId: []int64{cmd.Id},
}
err := GetMonitors(&monitorQuery)
if err != nil {
return err
}
if len(monitorQuery.Result) > 0 {
for _, mon := range monitorQuery.Result {
monitorDelCmd := &m.DeleteMonitorCommand{Id: mon.Id, OrgId: cmd.OrgId}
if err := DeleteMonitorTransaction(monitorDelCmd, sess); err != nil {
return err
}
}
}
q := m.GetEndpointByIdQuery{
Id: cmd.Id,
OrgId: cmd.OrgId,
}
err = GetEndpointById(&q)
if err != nil {
return err
}
var rawSql = "DELETE FROM endpoint WHERE id=? and org_id=?"
_, err = sess.Exec(rawSql, cmd.Id, cmd.OrgId)
if err != nil {
return err
}
rawSql = "DELETE FROM endpoint_tag WHERE endpoint_id=? and org_id=?"
if _, err := sess.Exec(rawSql, cmd.Id, cmd.OrgId); err != nil {
return err
}
sess.publishAfterCommit(&events.EndpointRemoved{
Timestamp: time.Now(),
Id: cmd.Id,
OrgId: cmd.OrgId,
Name: q.Result.Name,
Slug: q.Result.Slug,
})
return err
})
}
func GetAllEndpointTags(query *m.GetAllEndpointTagsQuery) error {
rawSql := `SELECT tag FROM endpoint_tag WHERE org_id=? GROUP BY tag`
sess := x.Sql(rawSql, query.OrgId)
tags := make([]tagNameWrapper, 0)
if err := sess.Find(&tags); err != nil {
return err
}
query.Result = make([]string, 0)
for _, tag := range tags {
query.Result = append(query.Result, tag.Tag)
}
return nil
}