From 604c883b8164d01928286478d0966d9ee1a48bfe Mon Sep 17 00:00:00 2001 From: Sukhwinder Dhillon Date: Fri, 22 Dec 2023 11:47:15 +0100 Subject: [PATCH] history_event_type.go: Move file to package `common` and rename to `history.go` Move `HistoryRow` struct to this file, as history table has also been generalised --- .../history.go} | 32 ++++++++++++++++++- internal/eventhandler/eventhandler.go | 20 ++++++------ internal/incident/db_types.go | 28 ---------------- internal/incident/incident.go | 28 ++++++++-------- internal/incident/sync.go | 10 +++--- 5 files changed, 60 insertions(+), 58 deletions(-) rename internal/{incident/history_event_type.go => common/history.go} (65%) diff --git a/internal/incident/history_event_type.go b/internal/common/history.go similarity index 65% rename from internal/incident/history_event_type.go rename to internal/common/history.go index bd93727bc..769357a6f 100644 --- a/internal/incident/history_event_type.go +++ b/internal/common/history.go @@ -1,9 +1,11 @@ -package incident +package common import ( "database/sql/driver" "fmt" "github.com/icinga/icinga-notifications/internal/event" + "github.com/icinga/icinga-notifications/internal/recipient" + "github.com/icinga/icingadb/pkg/types" ) type HistoryEventType int @@ -117,3 +119,31 @@ func GetHistoryEventType(eventType string) (HistoryEventType, error) { return historyEvType, nil } + +// HistoryRow represents a single history database entry. +type HistoryRow struct { + ID int64 `db:"id"` + ObjectID types.Binary `db:"object_id"` + IncidentID types.Int `db:"incident_id"` + RuleEscalationID types.Int `db:"rule_escalation_id"` + RuleNonStateEscalationID types.Int `db:"rule_non_state_escalation_id"` + EventID types.Int `db:"event_id"` + recipient.Key `db:",inline"` + RuleID types.Int `db:"rule_id"` + CausedByHistoryID types.Int `db:"caused_by_history_id"` + Time types.UnixMilli `db:"time"` + Type HistoryEventType `db:"type"` + ChannelID types.Int `db:"channel_id"` + NewSeverity event.Severity `db:"new_severity"` + OldSeverity event.Severity `db:"old_severity"` + NewRecipientRole ContactRole `db:"new_recipient_role"` + OldRecipientRole ContactRole `db:"old_recipient_role"` + Message types.String `db:"message"` + NotificationState NotificationState `db:"notification_state"` + SentAt types.UnixMilli `db:"sent_at"` +} + +// TableName implements the contracts.TableNamer interface. +func (h *HistoryRow) TableName() string { + return "history" +} diff --git a/internal/eventhandler/eventhandler.go b/internal/eventhandler/eventhandler.go index e8a151878..3bbcb3e6b 100644 --- a/internal/eventhandler/eventhandler.go +++ b/internal/eventhandler/eventhandler.go @@ -172,12 +172,12 @@ func (eh *EventHandler) handle(ctx context.Context) error { eh.timer = nil } } else { - historyEvType, err := incident.GetHistoryEventType(ev.Type) + historyEvType, err := common.GetHistoryEventType(ev.Type) if err != nil { return err } - hr := &incident.HistoryRow{ + hr := &common.HistoryRow{ EventID: utils.ToDBInt(ev.ID), Time: types.UnixMilli(time.Now()), Type: historyEvType, @@ -262,11 +262,11 @@ func (eh *EventHandler) evaluateRules(ctx context.Context, tx *sqlx.Tx, eventID } } else { //TODO: NON-incident history entry - history := &incident.HistoryRow{ + history := &common.HistoryRow{ Time: types.UnixMilli(time.Now()), EventID: utils.ToDBInt(eventID), RuleID: utils.ToDBInt(r.ID), - Type: incident.RuleMatched, + Type: common.RuleMatched, CausedByHistoryID: causedBy, } @@ -501,12 +501,12 @@ func (eh *EventHandler) triggerEscalations(ctx context.Context, tx *sqlx.Tx, ev } else { //TODO: non incident eh.logger.Infof("Rule %q reached escalation %q", r.Name, escalation.DisplayName()) - history := &incident.HistoryRow{ + history := &common.HistoryRow{ Time: types.UnixMilli(time.Now()), EventID: utils.ToDBInt(ev.ID), RuleNonStateEscalationID: utils.ToDBInt(escalation.ID), RuleID: utils.ToDBInt(r.ID), - Type: incident.EscalationTriggered, + Type: common.EscalationTriggered, CausedByHistoryID: causedBy, } @@ -593,14 +593,14 @@ func (eh *EventHandler) addPendingNotifications(ctx context.Context, tx *sqlx.Tx return nil, err } } else { - hr := &incident.HistoryRow{ + hr := &common.HistoryRow{ Key: recipient.ToKey(contact), EventID: utils.ToDBInt(ev.ID), Time: types.UnixMilli(time.Now()), - Type: incident.Notified, + Type: common.Notified, ChannelID: utils.ToDBInt(chID), CausedByHistoryID: causedBy, - NotificationState: incident.NotificationStatePending, + NotificationState: common.NotificationStatePending, } id, err := eh.addNonIncidentHistory(ctx, tx, hr, true) @@ -628,7 +628,7 @@ func (eh *EventHandler) addPendingNotifications(ctx context.Context, tx *sqlx.Tx return notifications, nil } -func (eh *EventHandler) addNonIncidentHistory(ctx context.Context, tx *sqlx.Tx, historyRow *incident.HistoryRow, fetchId bool) (types.Int, error) { +func (eh *EventHandler) addNonIncidentHistory(ctx context.Context, tx *sqlx.Tx, historyRow *common.HistoryRow, fetchId bool) (types.Int, error) { historyRow.ObjectID = eh.Object.ID stmt := utils.BuildInsertStmtWithout(eh.db, historyRow, "id") if fetchId { diff --git a/internal/incident/db_types.go b/internal/incident/db_types.go index 3d6d66d26..f34a1953e 100644 --- a/internal/incident/db_types.go +++ b/internal/incident/db_types.go @@ -108,31 +108,3 @@ type RuleRow struct { func (r *RuleRow) TableName() string { return "incident_rule" } - -// HistoryRow represents a single incident history database entry. -type HistoryRow struct { - ID int64 `db:"id"` - ObjectID types.Binary `db:"object_id"` - IncidentID types.Int `db:"incident_id"` - RuleEscalationID types.Int `db:"rule_escalation_id"` - RuleNonStateEscalationID types.Int `db:"rule_non_state_escalation_id"` - EventID types.Int `db:"event_id"` - recipient.Key `db:",inline"` - RuleID types.Int `db:"rule_id"` - CausedByHistoryID types.Int `db:"caused_by_history_id"` - Time types.UnixMilli `db:"time"` - Type HistoryEventType `db:"type"` - ChannelID types.Int `db:"channel_id"` - NewSeverity event.Severity `db:"new_severity"` - OldSeverity event.Severity `db:"old_severity"` - NewRecipientRole common.ContactRole `db:"new_recipient_role"` - OldRecipientRole common.ContactRole `db:"old_recipient_role"` - Message types.String `db:"message"` - NotificationState common.NotificationState `db:"notification_state"` - SentAt types.UnixMilli `db:"sent_at"` -} - -// TableName implements the contracts.TableNamer interface. -func (h *HistoryRow) TableName() string { - return "history" -} diff --git a/internal/incident/incident.go b/internal/incident/incident.go index 9d2e477e3..f4cb46b44 100644 --- a/internal/incident/incident.go +++ b/internal/incident/incident.go @@ -134,11 +134,11 @@ func (i *Incident) HandleRuleMatched(ctx context.Context, tx *sqlx.Tx, r *rule.R return types.Int{}, errors.New("failed to insert incident rule") } - history := &HistoryRow{ + history := &common.HistoryRow{ Time: types.UnixMilli(time.Now()), EventID: utils.ToDBInt(eventID), RuleID: utils.ToDBInt(r.ID), - Type: RuleMatched, + Type: common.RuleMatched, CausedByHistoryID: causedBy, } insertedID, err := i.AddHistory(ctx, tx, history, true) @@ -170,10 +170,10 @@ func (i *Incident) processSeverityChangedEvent(ctx context.Context, tx *sqlx.Tx, i.logger.Infof("Incident severity changed from %s to %s", oldSeverity.String(), newSeverity.String()) - history := &HistoryRow{ + history := &common.HistoryRow{ EventID: utils.ToDBInt(ev.ID), Time: types.UnixMilli(time.Now()), - Type: SeverityChanged, + Type: common.SeverityChanged, NewSeverity: newSeverity, OldSeverity: oldSeverity, Message: utils.ToDBString(ev.Message), @@ -194,10 +194,10 @@ func (i *Incident) processSeverityChangedEvent(ctx context.Context, tx *sqlx.Tx, RemoveCurrent(i.Object) - history := &HistoryRow{ + history := &common.HistoryRow{ EventID: utils.ToDBInt(ev.ID), Time: types.UnixMilli(i.RecoveredAt), - Type: Closed, + Type: common.Closed, } _, err = i.AddHistory(ctx, tx, history, false) @@ -229,8 +229,8 @@ func (i *Incident) processIncidentOpenedEvent(ctx context.Context, tx *sqlx.Tx, i.logger.Infow(fmt.Sprintf("Source %d opened incident at severity %q", ev.SourceId, i.Severity.String()), zap.String("message", ev.Message)) - historyRow := &HistoryRow{ - Type: Opened, + historyRow := &common.HistoryRow{ + Type: common.Opened, Time: types.UnixMilli(ev.Time), EventID: utils.ToDBInt(ev.ID), NewSeverity: i.Severity, @@ -250,11 +250,11 @@ func (i *Incident) processIncidentOpenedEvent(ctx context.Context, tx *sqlx.Tx, // Returns an error on database failure. func (i *Incident) TriggerEscalation(ctx context.Context, tx *sqlx.Tx, ev *event.Event, causedBy types.Int, escalation *rule.EscalationTemplate, r *rule.Rule) error { i.logger.Infof("Rule %q reached escalation %q", r.Name, escalation.DisplayName()) - history := &HistoryRow{ + history := &common.HistoryRow{ Time: types.UnixMilli(time.Now()), EventID: utils.ToDBInt(ev.ID), RuleID: utils.ToDBInt(r.ID), - Type: EscalationTriggered, + Type: common.EscalationTriggered, CausedByHistoryID: causedBy, } @@ -297,12 +297,12 @@ func (i *Incident) processNonStateTypeEvent(ctx context.Context, tx *sqlx.Tx, ev return i.processAcknowledgementEvent(ctx, tx, ev) } - historyEvType, err := GetHistoryEventType(ev.Type) + historyEvType, err := common.GetHistoryEventType(ev.Type) if err != nil { return err } - hr := &HistoryRow{ + hr := &common.HistoryRow{ EventID: utils.ToDBInt(ev.ID), Time: types.UnixMilli(time.Now()), Type: historyEvType, @@ -348,10 +348,10 @@ func (i *Incident) processAcknowledgementEvent(ctx context.Context, tx *sqlx.Tx, i.logger.Infof("Contact %q role changed from %s to %s", contact.String(), oldRole.String(), newRole.String()) - hr := &HistoryRow{ + hr := &common.HistoryRow{ Key: recipientKey, EventID: utils.ToDBInt(ev.ID), - Type: RecipientRoleChanged, + Type: common.RecipientRoleChanged, Time: types.UnixMilli(time.Now()), NewRecipientRole: newRole, OldRecipientRole: oldRole, diff --git a/internal/incident/sync.go b/internal/incident/sync.go index 37d0cabad..405298505 100644 --- a/internal/incident/sync.go +++ b/internal/incident/sync.go @@ -36,7 +36,7 @@ func (i *Incident) Sync(ctx context.Context, tx *sqlx.Tx) error { return nil } -func (i *Incident) AddHistory(ctx context.Context, tx *sqlx.Tx, historyRow *HistoryRow, fetchId bool) (types.Int, error) { +func (i *Incident) AddHistory(ctx context.Context, tx *sqlx.Tx, historyRow *common.HistoryRow, fetchId bool) (types.Int, error) { historyRow.IncidentID = utils.ToDBInt(i.incidentRowID) historyRow.ObjectID = i.Object.ID @@ -101,11 +101,11 @@ func (i *Incident) AddRecipient(ctx context.Context, tx *sqlx.Tx, escalation *ru i.logger.Infof("Contact %q role changed from %s to %s", r, oldRole.String(), newRole.String()) - hr := &HistoryRow{ + hr := &common.HistoryRow{ EventID: utils.ToDBInt(eventId), Key: cr.Key, Time: types.UnixMilli(time.Now()), - Type: RecipientRoleChanged, + Type: common.RecipientRoleChanged, NewRecipientRole: newRole, OldRecipientRole: oldRole, } @@ -151,11 +151,11 @@ func (i *Incident) AddRuleMatched(ctx context.Context, tx *sqlx.Tx, r *rule.Rule func (i *Incident) AddPendingNotificationHistory( ctx context.Context, tx *sqlx.Tx, eventID int64, contact *recipient.Contact, causedBy types.Int, chID int64, ) (int64, error) { - hr := &HistoryRow{ + hr := &common.HistoryRow{ Key: recipient.ToKey(contact), EventID: utils.ToDBInt(eventID), Time: types.UnixMilli(time.Now()), - Type: Notified, + Type: common.Notified, ChannelID: utils.ToDBInt(chID), CausedByHistoryID: causedBy, NotificationState: common.NotificationStatePending,