Skip to content

Commit

Permalink
Write also schedule/contactgroup_id for contact notified histories
Browse files Browse the repository at this point in the history
  • Loading branch information
yhabteab committed May 22, 2023
1 parent bd94e4b commit 272efd5
Showing 1 changed file with 57 additions and 10 deletions.
67 changes: 57 additions & 10 deletions internal/listener/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,9 @@ func (l *Listener) ProcessEvent(w http.ResponseWriter, req *http.Request) {
contactChannels := make(map[*recipient.Contact]map[string]struct{})

escalationRecipients := make(map[recipient.Key]bool)
groupsOrSchedules := make(map[*recipient.Contact]recipient.Key)
groupsOrSchedulesWithoutMembers := make(map[recipient.Recipient]bool)

for escalationID, state := range currentIncident.EscalationState {
escalation := l.runtimeConfig.GetRuleEscalation(escalationID)
if state.TriggeredAt.Time().IsZero() {
Expand Down Expand Up @@ -447,7 +450,14 @@ func (l *Listener) ProcessEvent(w http.ResponseWriter, req *http.Request) {
escalationRecipients[escalationRecipient.Key] = true

if !managed || state.Role > incident.RoleRecipient {
for _, c := range escalationRecipient.Recipient.GetContactsAt(ev.Time) {
r := escalationRecipient.Recipient
_, isContact := r.(*recipient.Contact)
contacts := r.GetContactsAt(ev.Time)
for _, c := range contacts {
if !isContact {
groupsOrSchedules[c] = recipient.ToKey(r)
}

if contactChannels[c] == nil {
contactChannels[c] = make(map[string]struct{})
}
Expand All @@ -457,6 +467,10 @@ func (l *Listener) ProcessEvent(w http.ResponseWriter, req *http.Request) {
contactChannels[c][c.DefaultChannel] = struct{}{}
}
}

if !isContact && len(contacts) == 0 {
groupsOrSchedulesWithoutMembers[r] = true
}
}
}
}
Expand All @@ -469,25 +483,43 @@ func (l *Listener) ProcessEvent(w http.ResponseWriter, req *http.Request) {

isEscalationRecipient := escalationRecipients[recipientKey]
if !isEscalationRecipient && (!managed || state.Role > incident.RoleRecipient) {
for _, contact := range r.GetContactsAt(ev.Time) {
_, isContact := r.(*recipient.Contact)
contacts := r.GetContactsAt(ev.Time)
for _, contact := range contacts {
if !isContact {
groupsOrSchedules[contact] = recipient.ToKey(r)
}

if contactChannels[contact] == nil {
contactChannels[contact] = make(map[string]struct{})
}
contactChannels[contact][contact.DefaultChannel] = struct{}{}
}

if !isContact && len(contacts) == 0 {
groupsOrSchedulesWithoutMembers[r] = true
}
}
}

for contact, channels := range contactChannels {
rk := recipient.ToKey(contact)
hr := &incident.HistoryRow{
EventID: utils.ToDBInt(ev.ID),
Time: types.UnixMilli(ev.Time),
Type: incident.Notified,
CausedByIncidentHistoryID: causedByIncidentHistoryId,
}

groupOrSchedule := groupsOrSchedules[contact]
if groupsOrSchedules != nil {
rk = rk.CopyNonNil(groupOrSchedule)
}

hr.Key = rk

for chType := range channels {
hr := &incident.HistoryRow{
Key: recipient.ToKey(contact),
EventID: utils.ToDBInt(ev.ID),
Time: types.UnixMilli(time.Now()),
Type: incident.Notified,
ChannelType: utils.ToDBString(chType),
CausedByIncidentHistoryID: causedByIncidentHistoryId,
}
hr.ChannelType = utils.ToDBString(chType)

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

Expand Down Expand Up @@ -516,6 +548,21 @@ func (l *Listener) ProcessEvent(w http.ResponseWriter, req *http.Request) {
}
}

for gs, _ := range groupsOrSchedulesWithoutMembers {
hr := &incident.HistoryRow{
Key: recipient.ToKey(gs),
EventID: utils.ToDBInt(ev.ID),
Time: types.UnixMilli(ev.Time),
Type: incident.Notified,
CausedByIncidentHistoryID: causedByIncidentHistoryId,
}

_, err = currentIncident.AddHistory(hr, false)
if err != nil {
l.logger.Errorln(err)
}
}

_, _ = fmt.Fprintln(w)
}

Expand Down

0 comments on commit 272efd5

Please sign in to comment.