Skip to content

Commit

Permalink
history_event_type.go: Move file to package common and rename to `h…
Browse files Browse the repository at this point in the history
…istory.go`

Move `HistoryRow` struct to this file, as history table has  also been generalised
  • Loading branch information
sukhwinder33445 committed Jan 4, 2024
1 parent 10dae94 commit 604c883
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 58 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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"
}
20 changes: 10 additions & 10 deletions internal/eventhandler/eventhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
}

Expand Down Expand Up @@ -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,
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
28 changes: 0 additions & 28 deletions internal/incident/db_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
28 changes: 14 additions & 14 deletions internal/incident/incident.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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),
Expand All @@ -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)
Expand Down Expand Up @@ -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,
Expand All @@ -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,
}

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
10 changes: 5 additions & 5 deletions internal/incident/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 604c883

Please sign in to comment.