From ecd7a39db070bbb37fb796218113f94d4ee6d6eb Mon Sep 17 00:00:00 2001 From: Daniel Ponte Date: Sat, 2 Nov 2024 14:26:58 -0400 Subject: [PATCH] Store suppressed alerts --- pkg/gordio/alerting/alerting.go | 24 ++++++++++++++-------- pkg/gordio/database/calls.sql.go | 7 +++++-- pkg/gordio/database/models.go | 1 + sql/postgres/migrations/001_initial.up.sql | 1 + sql/postgres/queries/calls.sql | 3 ++- 5 files changed, 25 insertions(+), 11 deletions(-) diff --git a/pkg/gordio/alerting/alerting.go b/pkg/gordio/alerting/alerting.go index d36bb04..89f3bb9 100644 --- a/pkg/gordio/alerting/alerting.go +++ b/pkg/gordio/alerting/alerting.go @@ -173,16 +173,20 @@ func (as *alerter) eval(ctx context.Context, now time.Time, testMode bool) ([]Al continue } s.Score *= float64(tgr.Weight) - s.Score = as.tgCache.ScaleScore(s, now) } if s.Score > as.cfg.AlertThreshold || testMode { if old, inCache := as.alertCache[s.ID]; !inCache || now.Sub(old.Timestamp) > as.renotify { + s.Score = as.tgCache.ScaleScore(s, now) a, err := as.makeAlert(ctx, s, origScore) if err != nil { return nil, fmt.Errorf("makeAlert: %w", err) } + if s.Score < as.cfg.AlertThreshold { + a.Suppressed = true + } + as.alertCache[s.ID] = a if !testMode { @@ -192,7 +196,9 @@ func (as *alerter) eval(ctx context.Context, now time.Time, testMode bool) ([]Al } } - notifications = append(notifications, a) + if !a.Suppressed { + notifications = append(notifications, a) + } } } } @@ -266,12 +272,13 @@ func (as *alerter) notify(ctx context.Context) error { } type Alert struct { - ID uuid.UUID - Timestamp time.Time - TGName string - Score trending.Score[cl.Talkgroup] - OrigScore float64 - Weight float32 + ID uuid.UUID + Timestamp time.Time + TGName string + Score trending.Score[cl.Talkgroup] + OrigScore float64 + Weight float32 + Suppressed bool } func (a *Alert) ToAddAlertParams() database.AddAlertParams { @@ -290,6 +297,7 @@ func (a *Alert) ToAddAlertParams() database.AddAlertParams { Weight: &a.Weight, Score: &f32score, OrigScore: origScore, + Notified: !a.Suppressed, } } diff --git a/pkg/gordio/database/calls.sql.go b/pkg/gordio/database/calls.sql.go index 6ed1ba5..73f3f71 100644 --- a/pkg/gordio/database/calls.sql.go +++ b/pkg/gordio/database/calls.sql.go @@ -13,7 +13,7 @@ import ( ) const addAlert = `-- name: AddAlert :exec -INSERT INTO alerts (id, time, talkgroup, weight, score, orig_score, metadata) +INSERT INTO alerts (id, time, talkgroup, weight, score, orig_score, notified, metadata) VALUES ( $1, @@ -22,7 +22,8 @@ VALUES $4, $5, $6, - $7 + $7, + $8 ) ` @@ -33,6 +34,7 @@ type AddAlertParams struct { Weight *float32 `json:"weight"` Score *float32 `json:"score"` OrigScore *float32 `json:"orig_score"` + Notified bool `json:"notified"` Metadata []byte `json:"metadata"` } @@ -44,6 +46,7 @@ func (q *Queries) AddAlert(ctx context.Context, arg AddAlertParams) error { arg.Weight, arg.Score, arg.OrigScore, + arg.Notified, arg.Metadata, ) return err diff --git a/pkg/gordio/database/models.go b/pkg/gordio/database/models.go index c9995b2..7a2b67d 100644 --- a/pkg/gordio/database/models.go +++ b/pkg/gordio/database/models.go @@ -20,6 +20,7 @@ type Alert struct { Weight *float32 `json:"weight"` Score *float32 `json:"score"` OrigScore *float32 `json:"orig_score"` + Notified bool `json:"notified"` Metadata []byte `json:"metadata"` } diff --git a/sql/postgres/migrations/001_initial.up.sql b/sql/postgres/migrations/001_initial.up.sql index b9145b8..b9159e6 100644 --- a/sql/postgres/migrations/001_initial.up.sql +++ b/sql/postgres/migrations/001_initial.up.sql @@ -80,6 +80,7 @@ CREATE TABLE IF NOT EXISTS alerts( weight REAL, score REAL, orig_score REAL, + notified BOOLEAN NOT NULL DEFAULT 'false', metadata JSONB ); diff --git a/sql/postgres/queries/calls.sql b/sql/postgres/queries/calls.sql index b4e7c19..9f4ff97 100644 --- a/sql/postgres/queries/calls.sql +++ b/sql/postgres/queries/calls.sql @@ -24,7 +24,7 @@ RETURNING id; UPDATE calls SET transcript = $2 WHERE id = $1; -- name: AddAlert :exec -INSERT INTO alerts (id, time, talkgroup, weight, score, orig_score, metadata) +INSERT INTO alerts (id, time, talkgroup, weight, score, orig_score, notified, metadata) VALUES ( sqlc.arg(id), @@ -33,6 +33,7 @@ VALUES sqlc.arg(weight), sqlc.arg(score), sqlc.arg(orig_score), + sqlc.arg(notified), sqlc.arg(metadata) );