alertconfig #21

Merged
amigan merged 5 commits from alertconfig into trunk 2024-11-02 14:28:38 -04:00
5 changed files with 25 additions and 11 deletions
Showing only changes of commit ecd7a39db0 - Show all commits

View file

@ -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,
}
}

View file

@ -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

View file

@ -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"`
}

View file

@ -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
);

View file

@ -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)
);