Skip to content

Commit

Permalink
Use common#ContactRole directly instead of incident#RecipientState
Browse files Browse the repository at this point in the history
  • Loading branch information
sukhwinder33445 committed Dec 20, 2023
1 parent 03f3203 commit 2b84d22
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 34 deletions.
10 changes: 5 additions & 5 deletions internal/eventhandler/eventhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type EventHandler struct {
Incident *incident.Incident

Rules map[ruleID]struct{}
Recipients map[recipient.Key]*incident.RecipientState
Recipients map[recipient.Key]common.ContactRole

db *icingadb.DB
logger *zap.SugaredLogger
Expand Down Expand Up @@ -87,7 +87,7 @@ func NewEventHandler(
Event: ev,
Incident: i,
Rules: map[ruleID]struct{}{},
Recipients: map[recipient.Key]*incident.RecipientState{},
Recipients: make(map[recipient.Key]common.ContactRole),
}
}

Expand Down Expand Up @@ -481,7 +481,7 @@ func (eh *EventHandler) AddRecipient(ctx context.Context, tx *sqlx.Tx, escalatio

_, ok := eh.Recipients[recipientKey]
if !ok {
eh.Recipients[recipientKey] = &incident.RecipientState{Role: common.RoleRecipient}
eh.Recipients[recipientKey] = common.RoleRecipient
}
}

Expand All @@ -499,13 +499,13 @@ func (eh *EventHandler) getRecipientsChannel() incident.ContactChannels {
// Check whether all the incident recipients do have an appropriate contact channel configured.
// When a recipient has subscribed/managed this incident via the UI or using an ACK, fallback
// to the default contact channel.
for recipientKey, state := range eh.Recipients {
for recipientKey, role := range eh.Recipients {
r := eh.runtimeConfig.GetRecipient(recipientKey)
if r == nil {
continue
}

if i == nil || i.IsNotifiable(state.Role) {
if i == nil || i.IsNotifiable(role) {
for _, contact := range r.GetContactsAt(eventTime) {
if contactChs[contact] == nil {
contactChs[contact] = make(map[int64]bool)
Expand Down
41 changes: 19 additions & 22 deletions internal/incident/incident.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type Incident struct {

EscalationState map[escalationID]*EscalationState
Rules map[ruleID]struct{}
Recipients map[recipient.Key]*RecipientState
Recipients map[recipient.Key]common.ContactRole

incidentRowID int64

Expand All @@ -55,7 +55,7 @@ func NewIncident(
runtimeConfig: runtimeConfig,
EscalationState: map[escalationID]*EscalationState{},
Rules: map[ruleID]struct{}{},
Recipients: map[recipient.Key]*RecipientState{},
Recipients: make(map[recipient.Key]common.ContactRole),
}
}

Expand All @@ -76,8 +76,8 @@ func (i *Incident) ID() int64 {
}

func (i *Incident) HasManager() bool {
for _, state := range i.Recipients {
if state.Role == common.RoleManager {
for _, role := range i.Recipients {
if role == common.RoleManager {
return true
}
}
Expand Down Expand Up @@ -343,18 +343,19 @@ func (i *Incident) processAcknowledgementEvent(ctx context.Context, tx *sqlx.Tx,
}

recipientKey := recipient.ToKey(contact)
state := i.Recipients[recipientKey]
role := i.Recipients[recipientKey]

if role == common.RoleManager {
// The user is already a manager
return nil
}

oldRole := common.RoleNone
newRole := common.RoleManager
if state != nil {
oldRole = state.Role

if oldRole == common.RoleManager {
// The user is already a manager
return nil
}
if role != oldRole {
oldRole = role
} else {
i.Recipients[recipientKey] = &RecipientState{Role: newRole}
i.Recipients[recipientKey] = newRole
}

i.logger.Infof("Contact %q role changed from %s to %s", contact.String(), oldRole.String(), newRole.String())
Expand Down Expand Up @@ -419,9 +420,9 @@ func (i *Incident) restoreRecipients(ctx context.Context) error {
return errors.New("failed to restore incident recipients")
}

recipients := make(map[recipient.Key]*RecipientState)
recipients := make(map[recipient.Key]common.ContactRole)
for _, contact := range contacts {
recipients[contact.Key] = &RecipientState{Role: contact.Role}
recipients[contact.Key] = contact.Role
}

i.Recipients = recipients
Expand Down Expand Up @@ -461,10 +462,6 @@ func (e *EscalationState) TableName() string {
return "incident_rule_escalation_state"
}

type RecipientState struct {
Role common.ContactRole
}

// ContactChannels stores a set of channel IDs for each set of individual contacts.
type ContactChannels map[*recipient.Contact]map[int64]bool

Expand All @@ -486,12 +483,12 @@ func (i *Incident) GetRecipientsChannel(evTime time.Time) ContactChannels {
// LoadEscalationRecipientsChannel loads all the configured channel of all the provided escalation recipients.
func (rct ContactChannels) LoadEscalationRecipientsChannel(escalation *rule.EscalationTemplate, i *Incident, t time.Time) {
for _, escalationRecipient := range escalation.Recipients {
state := i.Recipients[escalationRecipient.Key]
if state == nil {
role := i.Recipients[escalationRecipient.Key]
if role == common.RoleNone {
continue
}

if i.IsNotifiable(state.Role) {
if i.IsNotifiable(role) {
for _, c := range escalationRecipient.Recipient.GetContactsAt(t) {
if rct[c] == nil {
rct[c] = make(map[int64]bool)
Expand Down
14 changes: 7 additions & 7 deletions internal/incident/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,15 @@ func (i *Incident) AddRecipient(ctx context.Context, tx *sqlx.Tx, escalation *ru
recipientKey := recipient.ToKey(r)
cr.Key = recipientKey

state, ok := i.Recipients[recipientKey]
role, ok := i.Recipients[recipientKey]
if !ok {
i.Recipients[recipientKey] = &RecipientState{Role: newRole}
i.Recipients[recipientKey] = newRole
} else {
if state.Role < newRole {
oldRole := state.Role
state.Role = newRole
if role < newRole {
oldRole := role
role = newRole

i.logger.Infof("Contact %q role changed from %s to %s", r, state.Role.String(), newRole.String())
i.logger.Infof("Contact %q role changed from %s to %s", r, oldRole.String(), newRole.String())

hr := &HistoryRow{
IncidentID: i.incidentRowID,
Expand All @@ -120,7 +120,7 @@ func (i *Incident) AddRecipient(ctx context.Context, tx *sqlx.Tx, escalation *ru
return errors.New("failed to insert recipient role changed incident history")
}
}
cr.Role = state.Role
cr.Role = role
}

stmt, _ := i.db.BuildUpsertStmt(cr)
Expand Down

0 comments on commit 2b84d22

Please sign in to comment.