diff --git a/internal/incident/history_event_type.go b/internal/incident/history_event_type.go index aa352a1a9..e55d36ba4 100644 --- a/internal/incident/history_event_type.go +++ b/internal/incident/history_event_type.go @@ -3,6 +3,7 @@ package incident import ( "database/sql/driver" "fmt" + "github.com/icinga/icinga-notifications/internal/event" ) type HistoryEventType int @@ -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 +} diff --git a/internal/incident/incident.go b/internal/incident/incident.go index d4bebb9c3..371889875 100644 --- a/internal/incident/incident.go +++ b/internal/incident/incident.go @@ -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 @@ -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.