Store suppressed alerts

This commit is contained in:
Daniel 2024-11-02 14:26:58 -04:00
parent a0cc7eedfd
commit ecd7a39db0
5 changed files with 25 additions and 11 deletions

View file

@ -173,16 +173,20 @@ func (as *alerter) eval(ctx context.Context, now time.Time, testMode bool) ([]Al
continue continue
} }
s.Score *= float64(tgr.Weight) s.Score *= float64(tgr.Weight)
s.Score = as.tgCache.ScaleScore(s, now)
} }
if s.Score > as.cfg.AlertThreshold || testMode { if s.Score > as.cfg.AlertThreshold || testMode {
if old, inCache := as.alertCache[s.ID]; !inCache || now.Sub(old.Timestamp) > as.renotify { 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) a, err := as.makeAlert(ctx, s, origScore)
if err != nil { if err != nil {
return nil, fmt.Errorf("makeAlert: %w", err) return nil, fmt.Errorf("makeAlert: %w", err)
} }
if s.Score < as.cfg.AlertThreshold {
a.Suppressed = true
}
as.alertCache[s.ID] = a as.alertCache[s.ID] = a
if !testMode { 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 { type Alert struct {
ID uuid.UUID ID uuid.UUID
Timestamp time.Time Timestamp time.Time
TGName string TGName string
Score trending.Score[cl.Talkgroup] Score trending.Score[cl.Talkgroup]
OrigScore float64 OrigScore float64
Weight float32 Weight float32
Suppressed bool
} }
func (a *Alert) ToAddAlertParams() database.AddAlertParams { func (a *Alert) ToAddAlertParams() database.AddAlertParams {
@ -290,6 +297,7 @@ func (a *Alert) ToAddAlertParams() database.AddAlertParams {
Weight: &a.Weight, Weight: &a.Weight,
Score: &f32score, Score: &f32score,
OrigScore: origScore, OrigScore: origScore,
Notified: !a.Suppressed,
} }
} }

View file

@ -13,7 +13,7 @@ import (
) )
const addAlert = `-- name: AddAlert :exec 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 VALUES
( (
$1, $1,
@ -22,7 +22,8 @@ VALUES
$4, $4,
$5, $5,
$6, $6,
$7 $7,
$8
) )
` `
@ -33,6 +34,7 @@ type AddAlertParams struct {
Weight *float32 `json:"weight"` Weight *float32 `json:"weight"`
Score *float32 `json:"score"` Score *float32 `json:"score"`
OrigScore *float32 `json:"orig_score"` OrigScore *float32 `json:"orig_score"`
Notified bool `json:"notified"`
Metadata []byte `json:"metadata"` Metadata []byte `json:"metadata"`
} }
@ -44,6 +46,7 @@ func (q *Queries) AddAlert(ctx context.Context, arg AddAlertParams) error {
arg.Weight, arg.Weight,
arg.Score, arg.Score,
arg.OrigScore, arg.OrigScore,
arg.Notified,
arg.Metadata, arg.Metadata,
) )
return err return err

View file

@ -20,6 +20,7 @@ type Alert struct {
Weight *float32 `json:"weight"` Weight *float32 `json:"weight"`
Score *float32 `json:"score"` Score *float32 `json:"score"`
OrigScore *float32 `json:"orig_score"` OrigScore *float32 `json:"orig_score"`
Notified bool `json:"notified"`
Metadata []byte `json:"metadata"` Metadata []byte `json:"metadata"`
} }

View file

@ -80,6 +80,7 @@ CREATE TABLE IF NOT EXISTS alerts(
weight REAL, weight REAL,
score REAL, score REAL,
orig_score REAL, orig_score REAL,
notified BOOLEAN NOT NULL DEFAULT 'false',
metadata JSONB metadata JSONB
); );

View file

@ -24,7 +24,7 @@ RETURNING id;
UPDATE calls SET transcript = $2 WHERE id = $1; UPDATE calls SET transcript = $2 WHERE id = $1;
-- name: AddAlert :exec -- 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 VALUES
( (
sqlc.arg(id), sqlc.arg(id),
@ -33,6 +33,7 @@ VALUES
sqlc.arg(weight), sqlc.arg(weight),
sqlc.arg(score), sqlc.arg(score),
sqlc.arg(orig_score), sqlc.arg(orig_score),
sqlc.arg(notified),
sqlc.arg(metadata) sqlc.arg(metadata)
); );