-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenrichment.go
More file actions
595 lines (518 loc) · 14.3 KB
/
Copy pathenrichment.go
File metadata and controls
595 lines (518 loc) · 14.3 KB
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
package flashduty
import (
"context"
"golang.org/x/sync/errgroup"
)
// fetchIncidentTimeline fetches timeline for a single incident
func (c *Client) fetchIncidentTimeline(ctx context.Context, incidentID string) ([]RawTimelineItem, error) {
requestBody := map[string]any{
"incident_id": incidentID,
"limit": 100,
"asc": true,
}
result, err := postData[struct {
Items []RawTimelineItem `json:"items"`
}](c, ctx, "/incident/feed", requestBody, "unable to fetch timeline")
if err != nil {
return nil, err
}
if result == nil {
return nil, nil
}
return result.Items, nil
}
// fetchIncidentAlerts fetches alerts for a single incident
func (c *Client) fetchIncidentAlerts(ctx context.Context, incidentID string, limit int) ([]AlertPreview, int, error) {
requestBody := map[string]any{
"incident_id": incidentID,
"p": 1,
"limit": limit,
}
result, err := postData[struct {
Total int `json:"total"`
Items []struct {
AlertID string `json:"alert_id"`
Title string `json:"title"`
Severity string `json:"severity"`
Status string `json:"status"`
TriggerTime int64 `json:"trigger_time"`
Labels map[string]string `json:"labels,omitempty"`
} `json:"items"`
}](c, ctx, "/incident/alert/list", requestBody, "unable to fetch alerts")
if err != nil {
return nil, 0, err
}
if result == nil {
return nil, 0, nil
}
alerts := make([]AlertPreview, 0, len(result.Items))
for _, item := range result.Items {
alerts = append(alerts, AlertPreview{
AlertID: item.AlertID,
Title: item.Title,
Severity: item.Severity,
Status: item.Status,
StartTime: Timestamp(item.TriggerTime),
Labels: item.Labels,
})
}
return alerts, result.Total, nil
}
// fetchPersonInfos fetches person information by IDs
func (c *Client) fetchPersonInfos(ctx context.Context, personIDs []int64) (map[int64]PersonInfo, error) {
if len(personIDs) == 0 {
return make(map[int64]PersonInfo), nil
}
// Deduplicate person IDs
idSet := make(map[int64]struct{})
for _, id := range personIDs {
if id != 0 {
idSet[id] = struct{}{}
}
}
uniqueIDs := make([]int64, 0, len(idSet))
for id := range idSet {
uniqueIDs = append(uniqueIDs, id)
}
if len(uniqueIDs) == 0 {
return make(map[int64]PersonInfo), nil
}
requestBody := map[string]any{
"person_ids": uniqueIDs,
}
result, err := postData[struct {
Items []struct {
PersonID int64 `json:"person_id"`
PersonName string `json:"person_name"`
Email string `json:"email,omitempty"`
Avatar string `json:"avatar,omitempty"`
As string `json:"as,omitempty"`
} `json:"items"`
}](c, ctx, "/person/infos", requestBody, "unable to fetch person information")
if err != nil {
return nil, err
}
personMap := make(map[int64]PersonInfo)
if result != nil {
for _, item := range result.Items {
personMap[item.PersonID] = PersonInfo{
PersonID: item.PersonID,
PersonName: item.PersonName,
Email: item.Email,
Avatar: item.Avatar,
As: item.As,
}
}
}
return personMap, nil
}
// fetchTeamInfos fetches team information by IDs
func (c *Client) fetchTeamInfos(ctx context.Context, teamIDs []int64) (map[int64]TeamInfo, error) {
if len(teamIDs) == 0 {
return make(map[int64]TeamInfo), nil
}
// Deduplicate team IDs
idSet := make(map[int64]struct{})
for _, id := range teamIDs {
if id != 0 {
idSet[id] = struct{}{}
}
}
uniqueIDs := make([]int64, 0, len(idSet))
for id := range idSet {
uniqueIDs = append(uniqueIDs, id)
}
if len(uniqueIDs) == 0 {
return make(map[int64]TeamInfo), nil
}
requestBody := map[string]any{
"team_ids": uniqueIDs,
}
result, err := postData[struct {
Items []struct {
TeamID int64 `json:"team_id"`
TeamName string `json:"team_name"`
} `json:"items"`
}](c, ctx, "/team/infos", requestBody, "unable to fetch team information")
if err != nil {
return nil, err
}
teamMap := make(map[int64]TeamInfo)
if result != nil {
for _, item := range result.Items {
teamMap[item.TeamID] = TeamInfo{
TeamID: item.TeamID,
TeamName: item.TeamName,
}
}
}
return teamMap, nil
}
// fetchScheduleInfos fetches schedule information by IDs
func (c *Client) fetchScheduleInfos(ctx context.Context, scheduleIDs []int64) (map[int64]ScheduleInfo, error) {
if len(scheduleIDs) == 0 {
return make(map[int64]ScheduleInfo), nil
}
// Deduplicate schedule IDs
idSet := make(map[int64]struct{})
for _, id := range scheduleIDs {
if id != 0 {
idSet[id] = struct{}{}
}
}
uniqueIDs := make([]int64, 0, len(idSet))
for id := range idSet {
uniqueIDs = append(uniqueIDs, id)
}
if len(uniqueIDs) == 0 {
return make(map[int64]ScheduleInfo), nil
}
requestBody := map[string]any{
"schedule_ids": uniqueIDs,
}
result, err := postData[struct {
Items []struct {
ID *int64 `json:"id"`
Name *string `json:"name"`
} `json:"items"`
}](c, ctx, "/schedule/infos", requestBody, "unable to fetch schedule information")
if err != nil {
return nil, err
}
scheduleMap := make(map[int64]ScheduleInfo)
if result != nil {
for _, item := range result.Items {
if item.ID != nil {
info := ScheduleInfo{ScheduleID: *item.ID}
if item.Name != nil {
info.ScheduleName = *item.Name
}
scheduleMap[*item.ID] = info
}
}
}
return scheduleMap, nil
}
// fetchChannelInfos fetches channel information by IDs
func (c *Client) fetchChannelInfos(ctx context.Context, channelIDs []int64) (map[int64]ChannelInfo, error) {
if len(channelIDs) == 0 {
return make(map[int64]ChannelInfo), nil
}
// Deduplicate channel IDs
idSet := make(map[int64]struct{})
for _, id := range channelIDs {
if id != 0 {
idSet[id] = struct{}{}
}
}
uniqueIDs := make([]int64, 0, len(idSet))
for id := range idSet {
uniqueIDs = append(uniqueIDs, id)
}
if len(uniqueIDs) == 0 {
return make(map[int64]ChannelInfo), nil
}
requestBody := map[string]any{
"channel_ids": uniqueIDs,
}
result, err := postData[struct {
Items []struct {
ChannelID int64 `json:"channel_id"`
ChannelName string `json:"channel_name"`
TeamID int64 `json:"team_id,omitempty"`
CreatorID int64 `json:"creator_id,omitempty"`
} `json:"items"`
}](c, ctx, "/channel/infos", requestBody, "unable to fetch channel information")
if err != nil {
return nil, err
}
channelMap := make(map[int64]ChannelInfo)
if result != nil {
for _, item := range result.Items {
channelMap[item.ChannelID] = ChannelInfo{
ChannelID: item.ChannelID,
ChannelName: item.ChannelName,
TeamID: item.TeamID,
CreatorID: item.CreatorID,
}
}
}
return channelMap, nil
}
// enrichChannels enriches channel information with team and creator names
func (c *Client) enrichChannels(ctx context.Context, channels []ChannelInfo) ([]ChannelInfo, error) {
if len(channels) == 0 {
return channels, nil
}
teamIDs := make([]int64, 0)
personIDs := make([]int64, 0)
for _, ch := range channels {
if ch.TeamID != 0 {
teamIDs = append(teamIDs, ch.TeamID)
}
if ch.CreatorID != 0 {
personIDs = append(personIDs, ch.CreatorID)
}
}
var teamMap map[int64]TeamInfo
var personMap map[int64]PersonInfo
g, gctx := errgroup.WithContext(ctx)
g.Go(func() error {
var err error
teamMap, err = c.fetchTeamInfos(gctx, teamIDs)
if err != nil {
teamMap = make(map[int64]TeamInfo)
}
return nil
})
g.Go(func() error {
var err error
personMap, err = c.fetchPersonInfos(gctx, personIDs)
if err != nil {
personMap = make(map[int64]PersonInfo)
}
return nil
})
_ = g.Wait()
enriched := make([]ChannelInfo, len(channels))
for i, ch := range channels {
enriched[i] = ch
if t, ok := teamMap[ch.TeamID]; ok {
enriched[i].TeamName = t.TeamName
}
if p, ok := personMap[ch.CreatorID]; ok {
enriched[i].CreatorName = p.PersonName
}
}
return enriched, nil
}
// enrichIncidents enriches incidents with person and channel names (without timeline/alerts)
func (c *Client) enrichIncidents(ctx context.Context, rawIncidents []RawIncident) ([]EnrichedIncident, error) {
personIDs := make([]int64, 0)
channelIDs := make([]int64, 0)
for _, inc := range rawIncidents {
if inc.CreatorID != 0 {
personIDs = append(personIDs, inc.CreatorID)
}
if inc.CloserID != 0 {
personIDs = append(personIDs, inc.CloserID)
}
for _, r := range inc.Responders {
if r.PersonID != 0 {
personIDs = append(personIDs, r.PersonID)
}
}
if inc.ChannelID != 0 {
channelIDs = append(channelIDs, inc.ChannelID)
}
}
var personMap map[int64]PersonInfo
var channelMap map[int64]ChannelInfo
g, ctx := errgroup.WithContext(ctx)
g.Go(func() error {
var err error
personMap, err = c.fetchPersonInfos(ctx, personIDs)
return err
})
g.Go(func() error {
var err error
channelMap, err = c.fetchChannelInfos(ctx, channelIDs)
return err
})
if err := g.Wait(); err != nil {
return nil, err
}
enriched := make([]EnrichedIncident, 0, len(rawIncidents))
for _, raw := range rawIncidents {
inc := EnrichedIncident{
IncidentID: raw.IncidentID,
Title: raw.Title,
Description: raw.Description,
Severity: raw.Severity,
Progress: raw.Progress,
StartTime: raw.StartTime,
AckTime: raw.AckTime,
CloseTime: raw.CloseTime,
ChannelID: raw.ChannelID,
CreatorID: raw.CreatorID,
CloserID: raw.CloserID,
Labels: raw.Labels,
CustomFields: raw.Fields,
}
if ch, ok := channelMap[raw.ChannelID]; ok {
inc.ChannelName = ch.ChannelName
}
if p, ok := personMap[raw.CreatorID]; ok {
inc.CreatorName = p.PersonName
inc.CreatorEmail = p.Email
}
if p, ok := personMap[raw.CloserID]; ok {
inc.CloserName = p.PersonName
}
if len(raw.Responders) > 0 {
inc.Responders = make([]EnrichedResponder, 0, len(raw.Responders))
for _, r := range raw.Responders {
er := EnrichedResponder{
PersonID: r.PersonID,
AssignedAt: r.AssignedAt,
AcknowledgedAt: r.AcknowledgedAt,
}
if p, ok := personMap[r.PersonID]; ok {
er.PersonName = p.PersonName
er.Email = p.Email
}
inc.Responders = append(inc.Responders, er)
}
}
enriched = append(enriched, inc)
}
return enriched, nil
}
// collectTimelinePersonIDs extracts all person IDs from timeline items
func collectTimelinePersonIDs(items []RawTimelineItem) []int64 {
personIDs := make([]int64, 0)
for _, item := range items {
if actorID := timelineActorID(item); actorID != 0 {
personIDs = append(personIDs, actorID)
}
if item.Detail == nil {
continue
}
switch item.Type {
case "i_assign", "i_a_rspd":
personIDs = extractPersonIDsFromDetail(item.Detail, "to", personIDs)
personIDs = extractPersonIDsFromDetail(item.Detail, "person_ids", personIDs)
case "i_notify":
personIDs = extractPersonIDsFromDetail(item.Detail, "to", personIDs)
personIDs = extractPersonIDsFromNestedDetail(item.Detail, "persons", "person_id", personIDs)
}
}
return personIDs
}
// extractPersonIDsFromDetail extracts person IDs from a detail map field
func extractPersonIDsFromDetail(detail map[string]any, field string, personIDs []int64) []int64 {
if values, ok := detail[field].([]any); ok {
for _, v := range values {
if id, ok := toInt64(v); ok && id != 0 {
personIDs = append(personIDs, id)
}
}
}
return personIDs
}
// extractPersonIDsFromNestedDetail extracts person IDs from an array of objects.
func extractPersonIDsFromNestedDetail(detail map[string]any, field, idField string, personIDs []int64) []int64 {
values, ok := detail[field].([]any)
if !ok {
return personIDs
}
for _, v := range values {
entry, ok := v.(map[string]any)
if !ok {
continue
}
if id, ok := toInt64(entry[idField]); ok && id != 0 {
personIDs = append(personIDs, id)
}
}
return personIDs
}
// toInt64 converts any to int64
func toInt64(v any) (int64, bool) {
switch n := v.(type) {
case float64:
return int64(n), true
case int64:
return n, true
case int:
return int64(n), true
}
return 0, false
}
// enrichTimelineItems enriches raw timeline items with person names
func enrichTimelineItems(items []RawTimelineItem, personMap map[int64]PersonInfo) []TimelineEvent {
events := make([]TimelineEvent, 0, len(items))
for _, item := range items {
operatorID := timelineActorID(item)
event := TimelineEvent{
Type: item.Type,
Timestamp: item.CreatedAt,
OperatorID: operatorID,
}
if p, ok := personMap[operatorID]; ok {
event.OperatorName = p.PersonName
}
event.Detail = enrichTimelineDetail(item.Type, item.Detail, personMap)
events = append(events, event)
}
return events
}
// enrichTimelineDetail enriches the detail field based on event type
func enrichTimelineDetail(eventType string, detail map[string]any, personMap map[int64]PersonInfo) any {
if detail == nil {
return nil
}
enriched := copyMap(detail)
switch eventType {
case "i_notify":
enrichPersonIDsField(enriched, "to", personMap)
enrichNestedPersonField(enriched, "persons", personMap)
case "i_assign", "i_a_rspd":
enrichPersonIDsField(enriched, "to", personMap)
enrichPersonIDsField(enriched, "person_ids", personMap)
}
return enriched
}
// copyMap creates a shallow copy of a map
func copyMap(m map[string]any) map[string]any {
result := make(map[string]any, len(m))
for k, v := range m {
result[k] = v
}
return result
}
// enrichPersonIDsField enriches a field containing person IDs with person names
func enrichPersonIDsField(enriched map[string]any, field string, personMap map[int64]PersonInfo) {
values, ok := enriched[field].([]any)
if !ok {
return
}
enrichedValues := make([]map[string]any, 0, len(values))
for _, v := range values {
id, ok := toInt64(v)
if !ok {
continue
}
entry := map[string]any{"person_id": id}
if p, ok := personMap[id]; ok {
entry["person_name"] = p.PersonName
}
enrichedValues = append(enrichedValues, entry)
}
enriched[field] = enrichedValues
}
func enrichNestedPersonField(enriched map[string]any, field string, personMap map[int64]PersonInfo) {
values, ok := enriched[field].([]any)
if !ok {
return
}
for _, v := range values {
entry, ok := v.(map[string]any)
if !ok {
continue
}
id, ok := toInt64(entry["person_id"])
if !ok {
continue
}
if p, ok := personMap[id]; ok {
entry["person_name"] = p.PersonName
}
}
}
func timelineActorID(item RawTimelineItem) int64 {
if item.CreatorID != 0 {
return item.CreatorID
}
return item.PersonID
}