Skip to content

Commit

Permalink
Notify recipients on NonStateTypeEvent
Browse files Browse the repository at this point in the history
NonStateTypeEvent are events such as comment_(added/removed), downtime_(started/ended) etc., which are triggered independently of the object state.
  • Loading branch information
sukhwinder33445 committed Jan 8, 2024
1 parent 396a0b3 commit 71fd83b
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 5 deletions.
32 changes: 32 additions & 0 deletions internal/incident/history_event_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package incident
import (
"database/sql/driver"
"fmt"
"github.com/icinga/icinga-notifications/internal/event"
)

type HistoryEventType int
Expand Down Expand Up @@ -95,3 +96,34 @@ func (h HistoryEventType) Value() (driver.Value, error) {
func (h *HistoryEventType) String() string {
return historyEventTypeToName[*h]
}

// GetNonStateHistoryEventType returns HistoryEventType of the given non-state event type
func GetNonStateHistoryEventType(eventType string) (HistoryEventType, error) {
var historyEvType HistoryEventType
switch eventType {
case event.TypeDowntimeStart:
historyEvType = DowntimeStarted
case event.TypeDowntimeEnd:
historyEvType = DowntimeEnded
case event.TypeDowntimeCancelled:
historyEvType = DowntimeCancelled
case event.TypeFlappingStart:
historyEvType = FlappingStarted
case event.TypeFlappingEnd:
historyEvType = FlappingEnded
case event.TypeCustom:
historyEvType = Custom
case event.TypeCommentAdded:
historyEvType = CommentAdded
case event.TypeCommentRemoved:
historyEvType = CommentRemoved
case event.TypeAcknowledgementSet:
historyEvType = AcknowledgementSet
case event.TypeAcknowledgementCleared:
historyEvType = AcknowledgementCleared
default:
return historyEvType, fmt.Errorf("unsupported type %q", eventType)
}

return historyEvType, nil
}
39 changes: 34 additions & 5 deletions internal/incident/incident.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,12 @@ func (i *Incident) ProcessEvent(ctx context.Context, ev *event.Event, created bo
return errors.New("can't insert incident event to the database")
}

if ev.Type == event.TypeAcknowledgement {
return i.processAcknowledgementEvent(ctx, tx, ev)
}

var causedBy types.Int
if !created {
if ev.Type != event.TypeState {
if err := i.processNonStateTypeEvent(ctx, tx, ev); err != nil {
return err
}
} else if !created {
causedBy, err = i.processSeverityChangedEvent(ctx, tx, ev)
if err != nil {
return err
Expand Down Expand Up @@ -335,6 +335,35 @@ func (i *Incident) processIncidentOpenedEvent(ctx context.Context, tx *sqlx.Tx,
return nil
}

func (i *Incident) processNonStateTypeEvent(ctx context.Context, tx *sqlx.Tx, ev *event.Event) error {
if ev.Type == event.TypeAcknowledgementSet {
if err := i.processAcknowledgementEvent(ctx, tx, ev); err != nil {
return err
}
}

historyEvType, err := GetNonStateHistoryEventType(ev.Type)
if err != nil {
return err
}

hr := &HistoryRow{
EventID: utils.ToDBInt(ev.ID),
Time: types.UnixMilli(time.Now()),
Type: historyEvType,
Message: utils.ToDBString(ev.Message),
}

_, err = i.AddHistory(ctx, tx, hr, false)
if err != nil {
i.logger.Errorw("Failed to add incident history", zap.String("type", historyEvType.String()), zap.Error(err))

return fmt.Errorf("failed to add %s incident history", historyEvType.String())
}

return nil
}

// evaluateRules evaluates all the configured rules for this *incident.Object and
// generates history entries for each matched rule.
// Returns error on database failure.
Expand Down

0 comments on commit 71fd83b

Please sign in to comment.