-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphql.go
380 lines (356 loc) · 10.4 KB
/
graphql.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"sort"
"strconv"
"strings"
"time"
)
const (
GraphQlEndpoint = "https://api.newrelic.com/graphql"
GrQl_Parallel = 10
PolicyQuery = `query($cursor: String) {actor {account(id: %d) {alerts {policiesSearch(cursor: $cursor) {policies {id incidentPreference name accountId} nextCursor}}}}}`
ConditionQuery = `query EntitySearchQuery($cursor: String) {actor {entitySearch(query: "domain = 'AIOPS' AND type = 'CONDITION' AND accountId = %d", options: {tagFilter: ["id","policyId"]}) {results(cursor: $cursor) {entities {guid accountId type name tags {key values}} nextCursor}}}}`
DetailQuery = `query getConditionDetail($accountId: Int!, $conditionId: ID!) {actor {account(id: $accountId) {alerts {nrqlCondition(id: $conditionId) {nrql {query} name id}}}}}`
)
// Alert entities
type Policy struct {
AccountId int `json:"accountId"`
Id string `json:"id"`
Name string `json:"name"`
IncidentPreference string `json:"IncidentPreference"`
ConditionIds []int
TF string
}
type Condition struct {
AccountId int `json:"accountId"`
PolicyId string `json:"policyId"`
Id string `json:"id"`
Name string `json:"name"`
Guid string `json:"guid"`
Query string
}
type Entity struct {
AccountId int `json:"accountId"`
Guid string `json:"guid"`
Name string `json:"name"`
Tags []struct {
Key string `json:"key"`
Values []string `json:"values"`
} `json:"tags"`
Type string `json:"type"`
}
type Output struct {
ConditionId int
Query string
}
// GraphQl request and result formats
type GraphQlPayload struct {
Query string `json:"query"`
Variables struct {
AccountId int `json:"accountId,omitempty"`
ConditionId string `json:"conditionId,omitempty"`
Cursor string `json:"cursor,omitempty"`
} `json:"variables"`
}
type GraphQlResult struct {
Errors []Error `json:"errors"`
Data struct {
Actor struct {
EntitySearch struct {
Results struct {
Entities []Entity `json:"entities"`
NextCursor interface{} `json:"nextCursor"`
} `json:"results"`
} `json:"entitySearch"`
Account struct {
Alerts struct {
NrqlCondition NrqlCondition `json:"nrqlCondition"`
PoliciesSearch struct {
Policies []Policy `json:"policies"`
NextCursor interface{} `json:"nextCursor"`
} `json:"policiesSearch"`
} `json:"alerts"`
} `json:"account"`
} `json:"actor"`
} `json:"data"`
}
type NrqlCondition struct {
Id string `json:"id"`
Name string `json:"name"`
Nrql struct {
Query string `json:"query"`
} `json:"nrql"`
}
type Error struct {
Message string `json:"message"`
}
// Make API request with error retry
func retryQuery(client *http.Client, method, url, data string, headers []string) (b []byte) {
var res *http.Response
var err error
var body io.Reader
if len(data) > 0 {
body = strings.NewReader(data)
}
// up to 3 retries on API error
for j := 1; j <= 3; j++ {
req, _ := http.NewRequest(method, url, body)
for _, h := range headers {
params := strings.Split(h, ":")
req.Header.Set(params[0], params[1])
}
res, err = client.Do(req)
if err != nil {
log.Println(err)
}
if res != nil {
if res.StatusCode == http.StatusOK || res.StatusCode == http.StatusAccepted {
break
}
log.Printf("Retry %d: http status %d", j, res.StatusCode)
} else {
log.Printf("Retry %d: no response", j)
}
time.Sleep(500 * time.Millisecond)
}
b, err = io.ReadAll(res.Body)
if err != nil {
log.Println(err)
return
}
res.Body.Close()
return
}
func (data *LocalData) getPolicies() {
var gQuery GraphQlPayload
var j []byte
var err error
// Get Policies with IncidentPreference
gQuery.Query = fmt.Sprintf(PolicyQuery, data.AccountId)
for {
// make query payload
j, err = json.Marshal(gQuery)
if err != nil {
log.Printf("Error creating GraphQl policies query: %v", err)
}
b := retryQuery(data.Client, "POST", GraphQlEndpoint, string(j), data.GraphQlHeaders)
// parse results
var graphQlResult GraphQlResult
log.Printf("Parsing GraphQl policies response %d bytes", len(b))
err = json.Unmarshal(b, &graphQlResult)
if err != nil {
log.Printf("Error parsing GraphQl policies result: %v", err)
}
if len(graphQlResult.Errors) > 0 {
log.Printf("Errors with GraphQl query: %v", graphQlResult.Errors)
}
policiesSearch := graphQlResult.Data.Actor.Account.Alerts.PoliciesSearch
// store policies
for _, policy := range policiesSearch.Policies {
var id int
id, err = strconv.Atoi(policy.Id)
if err != nil {
log.Printf("Error parsing policy Id: %v (policy %+v)", err, policy)
continue
}
data.PolicyMap[id] = policy
}
if policiesSearch.NextCursor == nil {
break
}
// get next page of results
gQuery.Variables.Cursor = fmt.Sprintf("%s", policiesSearch.NextCursor)
}
// Sort policy ids
for policyId := range data.PolicyMap {
data.PolicyIds = append(data.PolicyIds, policyId)
}
sort.Ints(data.PolicyIds)
log.Printf("Found %d policies", len(data.PolicyMap))
}
func parseCondition(entity Entity) (condition Condition, err error) {
condition.Guid = entity.Guid
condition.Name = entity.Name
condition.AccountId = entity.AccountId
if entity.Type != "CONDITION" {
err = fmt.Errorf("invalid condition type: %+v", entity)
return
}
if len(entity.Tags) != 2 {
err = fmt.Errorf("invalid condition tags: %+v", entity)
return
}
for _, tag := range entity.Tags {
if tag.Key == "policyId" {
if len(tag.Values) != 1 {
err = fmt.Errorf("invalid condition entity PolicyId: %+v", entity)
return
}
condition.PolicyId = tag.Values[0]
}
if tag.Key == "id" {
if len(tag.Values) != 1 {
err = fmt.Errorf("invalid condition entity Id: %+v", entity)
return
}
condition.Id = tag.Values[0]
}
}
return
}
func (data *LocalData) getConditionDetails() {
inputChan := make(chan int, len(data.ConditionMap)+GrQl_Parallel)
outputChan := make(chan Output, len(data.ConditionMap)+GrQl_Parallel)
// Load conditions into channel
go func() {
for _, policyId := range data.PolicyIds {
policy := data.PolicyMap[policyId]
// Sort condition ids
sort.Ints(policy.ConditionIds)
for _, id := range policy.ConditionIds {
inputChan <- id
}
}
for n := 0; n < GrQl_Parallel; n++ {
inputChan <- 0
}
}()
for i := 1; i <= GrQl_Parallel; i++ {
log.Printf("GraphQL - starting condition detail requestor #%d", i)
go func() {
var gQuery GraphQlPayload
var j []byte
var err error
gQuery.Query = DetailQuery
client := &http.Client{}
for {
conditionId := <-inputChan
if conditionId == 0 {
outputChan <- Output{}
break
}
gQuery.Variables.ConditionId = fmt.Sprintf("%d", conditionId)
gQuery.Variables.AccountId = data.AccountId
// make query payload
j, err = json.Marshal(gQuery)
if err != nil {
log.Printf("Error creating GraphQl condition detail query: %v", err)
continue
}
b := retryQuery(client, "POST", GraphQlEndpoint, string(j), data.GraphQlHeaders)
// parse results
var graphQlResult GraphQlResult
err = json.Unmarshal(b, &graphQlResult)
if err != nil {
log.Printf("Error parsing GraphQl condition detail result: %v", err)
continue
}
if len(graphQlResult.Errors) > 0 {
if graphQlResult.Errors[0].Message == "Not Found" {
continue
}
log.Printf("Errors with GraphQl query: %v", graphQlResult.Errors)
continue
}
outputChan <- Output{
ConditionId: conditionId,
Query: graphQlResult.Data.Actor.Account.Alerts.NrqlCondition.Nrql.Query,
}
}
}()
}
queries := 0
for i := 0; i < GrQl_Parallel; i++ {
for {
output := <-outputChan
if output.ConditionId == 0 {
log.Printf("GraphQL - ending condition detail requestor #%d", i+1)
break
}
condition, ok := data.ConditionMap[output.ConditionId]
if !ok {
log.Printf("GraphQL condition detail, no condition for id %d", output.ConditionId)
continue
}
condition.Query = output.Query
data.ConditionMap[output.ConditionId] = condition
queries++
}
}
log.Printf("GraphQL - finished condition detail requesters, %d nrql conditions found", queries)
}
func (data *LocalData) getConditions() {
var gQuery GraphQlPayload
var j []byte
var err error
var conditionCount int
// Get conditions, story in Policy map by guid
gQuery.Query = fmt.Sprintf(ConditionQuery, data.AccountId)
for {
// make query payload
j, err = json.Marshal(gQuery)
if err != nil {
log.Printf("Error creating GraphQl conditions query: %v", err)
}
b := retryQuery(data.Client, "POST", GraphQlEndpoint, string(j), data.GraphQlHeaders)
// parse results
var graphQlResult GraphQlResult
log.Printf("Parsing GraphQl conditions response %d bytes", len(b))
err = json.Unmarshal(b, &graphQlResult)
if err != nil {
log.Printf("Error parsing GraphQl conditions result: %v", err)
}
if len(graphQlResult.Errors) > 0 {
log.Printf("Errors with GraphQl query: %v", graphQlResult.Errors)
}
conditionsSearch := graphQlResult.Data.Actor.EntitySearch.Results
// store conditions
for _, entity := range conditionsSearch.Entities {
var condition Condition
var policy Policy
var ok bool
var id, policyId int
condition, err = parseCondition(entity)
if err != nil {
log.Printf("Error parsing condition: %v", err)
continue
}
policyId, err = strconv.Atoi(condition.PolicyId)
if err != nil {
log.Printf("Error parsing condition policyId: %v (condition %+v)", err, condition)
continue
}
policy, ok = data.PolicyMap[policyId]
if !ok {
log.Printf("Error locating policy for conditon: %+v", condition)
continue
}
id, err = strconv.Atoi(condition.Id)
if err != nil {
log.Printf("Error parsing condition Id: %v (condition %+v)", err, condition)
continue
}
data.ConditionMap[id] = condition
policy.ConditionIds = append(policy.ConditionIds, id)
data.PolicyMap[policyId] = policy
conditionCount++
}
if conditionsSearch.NextCursor == nil {
break
}
// get next page of results
gQuery.Variables.Cursor = fmt.Sprintf("%s", conditionsSearch.NextCursor)
}
log.Printf("Found %d conditions", conditionCount)
}
func (data *LocalData) makeClient() {
data.Client = &http.Client{}
data.GraphQlHeaders = []string{"Content-Type:application/json", "API-Key:" + data.UserKey}
data.PolicyMap = make(map[int]Policy)
data.ConditionMap = make(map[int]Condition)
}