From 368f231b89066ea9f1bed75c1dd3dca7b3007d7c Mon Sep 17 00:00:00 2001 From: Daniel Ponte Date: Wed, 20 Nov 2024 16:45:41 -0500 Subject: [PATCH] Alerting improvements --- config.sample.yaml | 2 +- pkg/alerting/alert/alert.go | 2 ++ pkg/alerting/alerting.go | 11 +++++++++++ pkg/database/extend.go | 6 +++++- pkg/notify/notify.go | 2 +- pkg/talkgroups/importer/import.go | 1 + pkg/talkgroups/store.go | 1 - 7 files changed, 21 insertions(+), 4 deletions(-) diff --git a/config.sample.yaml b/config.sample.yaml index 6384f65..4e66425 100644 --- a/config.sample.yaml +++ b/config.sample.yaml @@ -36,7 +36,7 @@ notify: # subjectTemplate: "Stillbox Alert ({{ highest . }})" # bodyTemplate: | # {{ range . -}} -# {{ .TGName }} is active with a score of {{ f .Score.Score 4 }}! ({{ f .Score.RecentCount 0 }}/{{ .Score.Count }} recent calls) +# {{ .TGName }}{{ if (and .Talkgroup .Talkgroup.AlphaTag) }} ({{ .Talkgroup.StringTag false -}}){{ end }} is active with a score of {{ f .Score.Score 4 }}! ({{ f .Score.RecentCount 0 }}/{{ .Score.Count }} recent calls) # # {{ end -}} config: diff --git a/pkg/alerting/alert/alert.go b/pkg/alerting/alert/alert.go index c3b0f60..f57de08 100644 --- a/pkg/alerting/alert/alert.go +++ b/pkg/alerting/alert/alert.go @@ -16,6 +16,7 @@ type Alert struct { ID int Timestamp time.Time TGName string + Talkgroup *talkgroups.Talkgroup Score trending.Score[talkgroups.ID] OrigScore float64 Weight float32 @@ -56,6 +57,7 @@ func Make(ctx context.Context, store talkgroups.Store, score trending.Score[talk case nil: d.Weight = tgRecord.Talkgroup.Weight d.TGName = tgRecord.String() + d.Talkgroup = tgRecord default: system, has := store.SystemName(ctx, int(score.ID.System)) if has { diff --git a/pkg/alerting/alerting.go b/pkg/alerting/alerting.go index 98da519..8249ff2 100644 --- a/pkg/alerting/alerting.go +++ b/pkg/alerting/alerting.go @@ -3,6 +3,7 @@ package alerting import ( "context" "fmt" + "math/rand" "net/http" "sort" "sync" @@ -200,6 +201,14 @@ func (as *alerter) eval(ctx context.Context, now time.Time, testMode bool) ([]al func (as *alerter) testNotifyHandler(w http.ResponseWriter, r *http.Request) { ctx := r.Context() + ridx := rand.Intn(len(as.scores)) + a, err := alert.Make(ctx, talkgroups.StoreFrom(ctx), as.scores[ridx], 1.0) + if err != nil { + log.Error().Err(err).Msg("test notify make alert fail") + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + alerts, err := as.eval(ctx, time.Now(), true) if err != nil { log.Error().Err(err).Msg("test notification eval") @@ -207,6 +216,8 @@ func (as *alerter) testNotifyHandler(w http.ResponseWriter, r *http.Request) { return } + alerts = append(alerts, a) + err = as.notifier.Send(ctx, alerts) if err != nil { log.Error().Err(err).Msg("test notification send") diff --git a/pkg/database/extend.go b/pkg/database/extend.go index 21328e3..4444ea9 100644 --- a/pkg/database/extend.go +++ b/pkg/database/extend.go @@ -21,8 +21,12 @@ func (g Talkgroup) GetSystem() System { return S func (g Talkgroup) GetLearned() bool { return false } func (g Talkgroup) String() string { + return g.StringTag(true) +} + +func (g Talkgroup) StringTag(withTag bool) string { switch { - case g.AlphaTag != nil: + case withTag && g.AlphaTag != nil: return *g.AlphaTag case g.Name != nil && g.TGGroup != nil: return *g.TGGroup + " " + *g.Name diff --git a/pkg/notify/notify.go b/pkg/notify/notify.go index 1bec7c4..286c5a5 100644 --- a/pkg/notify/notify.go +++ b/pkg/notify/notify.go @@ -52,7 +52,7 @@ var alertFm = template.FuncMap{ const ( defaultBodyTemplStr = `{{ range . -}} -{{ .TGName }} is active with a score of {{ f .Score.Score 4 }}! ({{ f .Score.RecentCount 0 }}/{{ .Score.Count }} recent calls) +{{ .TGName }}{{ if (and .Talkgroup .Talkgroup.AlphaTag) }} ({{ .Talkgroup.StringTag false -}}){{ end }} is active with a score of {{ f .Score.Score 4 }}! ({{ f .Score.RecentCount 0 }}/{{ .Score.Count }} recent calls) {{ end -}}` defaultSubjectTemplStr = `Stillbox Alert ({{ highest . }})` diff --git a/pkg/talkgroups/importer/import.go b/pkg/talkgroups/importer/import.go index 86d5190..c765b78 100644 --- a/pkg/talkgroups/importer/import.go +++ b/pkg/talkgroups/importer/import.go @@ -119,6 +119,7 @@ func (rr *radioReferenceImporter) importTalkgroups(ctx context.Context, sys int, Metadata: metadata, Tags: tags, Weight: 1.0, + Alert: true, }, System: database.System{ ID: sys, diff --git a/pkg/talkgroups/store.go b/pkg/talkgroups/store.go index 26e94e9..1d39246 100644 --- a/pkg/talkgroups/store.go +++ b/pkg/talkgroups/store.go @@ -320,7 +320,6 @@ func (t *cache) UpsertTGs(ctx context.Context, system int, input []database.Upse input[i].SystemID = int32(system) input[i].Learned = common.PtrTo(false) - } var oerr error