-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move
currentIncidents
to incidents.go
file
Co-authored-by: Julian Brost <[email protected]>
- Loading branch information
1 parent
50f3044
commit 371636c
Showing
3 changed files
with
134 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package incident | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
"github.com/icinga/icingadb/pkg/icingadb" | ||
"github.com/icinga/icingadb/pkg/logging" | ||
"github.com/icinga/noma/internal/config" | ||
"github.com/icinga/noma/internal/event" | ||
"github.com/icinga/noma/internal/object" | ||
"github.com/icinga/noma/internal/recipient" | ||
"sync" | ||
) | ||
|
||
var ( | ||
currentIncidents = make(map[*object.Object]*Incident) | ||
currentIncidentsMu sync.Mutex | ||
) | ||
|
||
func GetCurrent(db *icingadb.DB, obj *object.Object, logger *logging.Logger, runtimeConfig *config.RuntimeConfig, create bool) (*Incident, bool, error) { | ||
currentIncidentsMu.Lock() | ||
defer currentIncidentsMu.Unlock() | ||
|
||
created := false | ||
currentIncident := currentIncidents[obj] | ||
|
||
if currentIncident == nil { | ||
ir := &IncidentRow{} | ||
incident := &Incident{Object: obj, db: db, logger: logger, runtimeConfig: runtimeConfig} | ||
incident.SeverityBySource = make(map[int64]event.Severity) | ||
incident.EscalationState = make(map[escalationID]*EscalationState) | ||
incident.Recipients = make(map[recipient.Key]*RecipientState) | ||
|
||
err := db.QueryRowx(db.Rebind(db.BuildSelectStmt(ir, ir)+` WHERE "object_id" = ? AND "recovered_at" IS NULL`), obj.ID).StructScan(ir) | ||
if err != nil && err != sql.ErrNoRows { | ||
return nil, false, fmt.Errorf("incident query failed with: %w", err) | ||
} else if err == nil { | ||
incident.incidentRowID = ir.ID | ||
incident.StartedAt = ir.StartedAt.Time() | ||
|
||
sourceSeverity := &SourceSeverity{IncidentID: ir.ID} | ||
var sources []SourceSeverity | ||
err := db.Select( | ||
&sources, | ||
db.Rebind(db.BuildSelectStmt(sourceSeverity, sourceSeverity)+` WHERE "incident_id" = ? AND "severity" != ?`), | ||
ir.ID, event.SeverityOK, | ||
) | ||
if err != nil { | ||
return nil, false, fmt.Errorf("failed to fetch incident sources Severity: %w", err) | ||
} | ||
|
||
for _, source := range sources { | ||
incident.SeverityBySource[source.SourceID] = source.Severity | ||
} | ||
|
||
state := &EscalationState{} | ||
var states []*EscalationState | ||
err = db.Select(&states, db.Rebind(db.BuildSelectStmt(state, state)+` WHERE "incident_id" = ?`), ir.ID) | ||
if err != nil { | ||
return nil, false, fmt.Errorf("failed to fetch incident rule escalation state: %w", err) | ||
} | ||
|
||
for _, state := range states { | ||
incident.EscalationState[state.RuleEscalationID] = state | ||
} | ||
|
||
currentIncident = incident | ||
} | ||
|
||
if create && currentIncident == nil { | ||
created = true | ||
currentIncident = incident | ||
} | ||
|
||
if currentIncident != nil { | ||
currentIncidents[obj] = currentIncident | ||
} | ||
} | ||
|
||
if !created && currentIncident != nil { | ||
currentIncident.Lock() | ||
defer currentIncident.Unlock() | ||
|
||
contact := &ContactRow{} | ||
var contacts []*ContactRow | ||
err := db.Select(&contacts, db.Rebind(db.BuildSelectStmt(contact, contact)+` WHERE "incident_id" = ?`), currentIncident.ID()) | ||
if err != nil { | ||
return nil, false, fmt.Errorf("failed to fetch incident recipients: %w", err) | ||
} | ||
|
||
recipients := make(map[recipient.Key]*RecipientState) | ||
for _, contact := range contacts { | ||
recipients[contact.Key] = &RecipientState{Role: contact.Role} | ||
} | ||
|
||
currentIncident.Recipients = recipients | ||
} | ||
|
||
return currentIncident, created, nil | ||
} | ||
|
||
func RemoveCurrent(obj *object.Object) { | ||
currentIncidentsMu.Lock() | ||
defer currentIncidentsMu.Unlock() | ||
|
||
currentIncident := currentIncidents[obj] | ||
|
||
if currentIncident != nil { | ||
delete(currentIncidents, obj) | ||
} | ||
} | ||
|
||
// GetCurrentIncidents returns a map of all incidents for debugging purposes. | ||
func GetCurrentIncidents() map[int64]*Incident { | ||
currentIncidentsMu.Lock() | ||
defer currentIncidentsMu.Unlock() | ||
|
||
m := make(map[int64]*Incident) | ||
for _, incident := range currentIncidents { | ||
m[incident.incidentRowID] = incident | ||
} | ||
return m | ||
} |