Skip to content

Commit

Permalink
Drop incident_event table & incident#ProcessEvent() function
Browse files Browse the repository at this point in the history
The `incident_event` table is now obsolete and is no longer required,
and the `incident#ProcessEvent()` method has been replaced by the new
`events.Router` type.
  • Loading branch information
yhabteab committed Apr 26, 2024
1 parent 4fafe36 commit c32dfbf
Show file tree
Hide file tree
Showing 5 changed files with 1 addition and 73 deletions.
10 changes: 0 additions & 10 deletions internal/incident/incident.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,6 @@ func (i *Incident) ProcessEvent(ctx context.Context, ev *event.Event) error {
i.logger = i.logger.With(zap.String("incident", i.String()))
}

if err = i.AddEvent(ctx, tx, ev); err != nil {
i.logger.Errorw("Can't insert incident event to the database", zap.Error(err))

return errors.New("can't insert incident event to the database")
}

switch ev.Type {
case event.TypeState:
if !isNew {
Expand Down Expand Up @@ -233,10 +227,6 @@ func (i *Incident) RetriggerEscalations(ev *event.Event) {
return err
}

if err = i.AddEvent(ctx, tx, ev); err != nil {
return fmt.Errorf("can't insert incident event to the database: %w", err)
}

if err = i.triggerEscalations(ctx, tx, ev, escalations); err != nil {
return err
}
Expand Down
47 changes: 0 additions & 47 deletions internal/incident/incidents.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package incident

import (
"context"
"fmt"
"github.com/icinga/icinga-notifications/internal/config"
"github.com/icinga/icinga-notifications/internal/event"
"github.com/icinga/icinga-notifications/internal/object"
Expand Down Expand Up @@ -194,49 +193,3 @@ func GetCurrentIncidents() map[int64]*Incident {
}
return m
}

// ProcessEvent from an event.Event.
//
// This function first gets this Event's object.Object and its incident.Incident. Then, after performing some safety
// checks, it calls the Incident.ProcessEvent method.
//
// The returned error might be wrapped around event.ErrSuperfluousStateChange.
func ProcessEvent(
ctx context.Context,
db *icingadb.DB,
logs *logging.Logging,
runtimeConfig *config.RuntimeConfig,
ev *event.Event,
) error {
obj, err := object.FromEvent(ctx, db, ev)
if err != nil {
return fmt.Errorf("cannot sync event object: %w", err)
}

createIncident := ev.Severity != event.SeverityNone && ev.Severity != event.SeverityOK
currentIncident, err := GetCurrent(
ctx,
db,
obj,
logs.GetChildLogger("incident"),
runtimeConfig,
createIncident)
if err != nil {
return fmt.Errorf("cannot get current incident for %q: %w", obj.DisplayName(), err)
}

if currentIncident == nil {
switch {
// ignore non-state event without incident
case ev.Severity == event.SeverityNone:
return fmt.Errorf("%q does not have an active incident, ignoring %q event from source %d",
obj.DisplayName(), ev.Type, ev.SourceId)
case ev.Severity != event.SeverityOK:
panic(fmt.Sprintf("cannot process event %v with a non-OK state %v without a known incident", ev, ev.Severity))
default:
return fmt.Errorf("%w: ok state event from source %d", event.ErrSuperfluousStateChange, ev.SourceId)
}
}

return currentIncident.ProcessEvent(ctx, ev)
}
9 changes: 0 additions & 9 deletions internal/incident/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,6 @@ func (i *Incident) AddEscalationTriggered(ctx context.Context, tx *sqlx.Tx, stat
return err
}

// AddEvent Inserts incident history record to the database and returns an error on db failure.
func (i *Incident) AddEvent(ctx context.Context, tx *sqlx.Tx, ev *event.Event) error {
ie := &EventRow{IncidentID: i.Id, EventID: ev.ID}
stmt, _ := i.db.BuildInsertStmt(ie)
_, err := tx.NamedExecContext(ctx, stmt, ie)

return err
}

// AddRecipient adds recipient from the given *rule.Escalation to this incident.
// Syncs also all the recipients with the database and returns an error on db failure.
func (i *Incident) AddRecipient(ctx context.Context, tx *sqlx.Tx, escalation *rule.Escalation, eventId int64) error {
Expand Down
7 changes: 0 additions & 7 deletions schema/pgsql/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -254,13 +254,6 @@ CREATE TABLE incident (
CONSTRAINT pk_incident PRIMARY KEY (id)
);

CREATE TABLE incident_event (
incident_id bigint NOT NULL REFERENCES incident(id),
event_id bigint NOT NULL REFERENCES event(id),

CONSTRAINT pk_incident_event PRIMARY KEY (incident_id, event_id)
);

CREATE TYPE incident_contact_role AS ENUM ('recipient', 'subscriber', 'manager');

CREATE TABLE incident_contact (
Expand Down
1 change: 1 addition & 0 deletions schema/pgsql/upgrades/some-unique-file-name.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE incident_event;

0 comments on commit c32dfbf

Please sign in to comment.