Skip to content

Commit 272efd5

Browse files
committed
Write also schedule/contactgroup_id for contact notified histories
1 parent bd94e4b commit 272efd5

File tree

1 file changed

+57
-10
lines changed

1 file changed

+57
-10
lines changed

internal/listener/listener.go

Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,9 @@ func (l *Listener) ProcessEvent(w http.ResponseWriter, req *http.Request) {
399399
contactChannels := make(map[*recipient.Contact]map[string]struct{})
400400

401401
escalationRecipients := make(map[recipient.Key]bool)
402+
groupsOrSchedules := make(map[*recipient.Contact]recipient.Key)
403+
groupsOrSchedulesWithoutMembers := make(map[recipient.Recipient]bool)
404+
402405
for escalationID, state := range currentIncident.EscalationState {
403406
escalation := l.runtimeConfig.GetRuleEscalation(escalationID)
404407
if state.TriggeredAt.Time().IsZero() {
@@ -447,7 +450,14 @@ func (l *Listener) ProcessEvent(w http.ResponseWriter, req *http.Request) {
447450
escalationRecipients[escalationRecipient.Key] = true
448451

449452
if !managed || state.Role > incident.RoleRecipient {
450-
for _, c := range escalationRecipient.Recipient.GetContactsAt(ev.Time) {
453+
r := escalationRecipient.Recipient
454+
_, isContact := r.(*recipient.Contact)
455+
contacts := r.GetContactsAt(ev.Time)
456+
for _, c := range contacts {
457+
if !isContact {
458+
groupsOrSchedules[c] = recipient.ToKey(r)
459+
}
460+
451461
if contactChannels[c] == nil {
452462
contactChannels[c] = make(map[string]struct{})
453463
}
@@ -457,6 +467,10 @@ func (l *Listener) ProcessEvent(w http.ResponseWriter, req *http.Request) {
457467
contactChannels[c][c.DefaultChannel] = struct{}{}
458468
}
459469
}
470+
471+
if !isContact && len(contacts) == 0 {
472+
groupsOrSchedulesWithoutMembers[r] = true
473+
}
460474
}
461475
}
462476
}
@@ -469,25 +483,43 @@ func (l *Listener) ProcessEvent(w http.ResponseWriter, req *http.Request) {
469483

470484
isEscalationRecipient := escalationRecipients[recipientKey]
471485
if !isEscalationRecipient && (!managed || state.Role > incident.RoleRecipient) {
472-
for _, contact := range r.GetContactsAt(ev.Time) {
486+
_, isContact := r.(*recipient.Contact)
487+
contacts := r.GetContactsAt(ev.Time)
488+
for _, contact := range contacts {
489+
if !isContact {
490+
groupsOrSchedules[contact] = recipient.ToKey(r)
491+
}
492+
473493
if contactChannels[contact] == nil {
474494
contactChannels[contact] = make(map[string]struct{})
475495
}
476496
contactChannels[contact][contact.DefaultChannel] = struct{}{}
477497
}
498+
499+
if !isContact && len(contacts) == 0 {
500+
groupsOrSchedulesWithoutMembers[r] = true
501+
}
478502
}
479503
}
480504

481505
for contact, channels := range contactChannels {
506+
rk := recipient.ToKey(contact)
507+
hr := &incident.HistoryRow{
508+
EventID: utils.ToDBInt(ev.ID),
509+
Time: types.UnixMilli(ev.Time),
510+
Type: incident.Notified,
511+
CausedByIncidentHistoryID: causedByIncidentHistoryId,
512+
}
513+
514+
groupOrSchedule := groupsOrSchedules[contact]
515+
if groupsOrSchedules != nil {
516+
rk = rk.CopyNonNil(groupOrSchedule)
517+
}
518+
519+
hr.Key = rk
520+
482521
for chType := range channels {
483-
hr := &incident.HistoryRow{
484-
Key: recipient.ToKey(contact),
485-
EventID: utils.ToDBInt(ev.ID),
486-
Time: types.UnixMilli(time.Now()),
487-
Type: incident.Notified,
488-
ChannelType: utils.ToDBString(chType),
489-
CausedByIncidentHistoryID: causedByIncidentHistoryId,
490-
}
522+
hr.ChannelType = utils.ToDBString(chType)
491523

492524
l.logger.Infof("[%s %s] notify %q via %q", obj.DisplayName(), currentIncident.String(), contact.FullName, chType)
493525

@@ -516,6 +548,21 @@ func (l *Listener) ProcessEvent(w http.ResponseWriter, req *http.Request) {
516548
}
517549
}
518550

551+
for gs, _ := range groupsOrSchedulesWithoutMembers {
552+
hr := &incident.HistoryRow{
553+
Key: recipient.ToKey(gs),
554+
EventID: utils.ToDBInt(ev.ID),
555+
Time: types.UnixMilli(ev.Time),
556+
Type: incident.Notified,
557+
CausedByIncidentHistoryID: causedByIncidentHistoryId,
558+
}
559+
560+
_, err = currentIncident.AddHistory(hr, false)
561+
if err != nil {
562+
l.logger.Errorln(err)
563+
}
564+
}
565+
519566
_, _ = fmt.Fprintln(w)
520567
}
521568

0 commit comments

Comments
 (0)