Compare commits
2 commits
29acaf017a
...
64c73fab99
Author | SHA1 | Date | |
---|---|---|---|
64c73fab99 | |||
83b6b0e4f4 |
13 changed files with 461 additions and 95 deletions
2
Makefile
2
Makefile
|
@ -1,6 +1,6 @@
|
||||||
VPKG=dynatron.me/x/stillbox/internal/version
|
VPKG=dynatron.me/x/stillbox/internal/version
|
||||||
VER!=git describe --tags --always --dirty
|
VER!=git describe --tags --always --dirty
|
||||||
BUILDDATE!=date '+%Y-%m-%e'
|
BUILDDATE!=date '+%Y%m%d'
|
||||||
LDFLAGS=-ldflags="-X '${VPKG}.Version=${VER}' -X '${VPKG}.Built=${BUILDDATE}'"
|
LDFLAGS=-ldflags="-X '${VPKG}.Version=${VER}' -X '${VPKG}.Built=${BUILDDATE}'"
|
||||||
|
|
||||||
all: checkcalls
|
all: checkcalls
|
||||||
|
|
|
@ -23,6 +23,15 @@ type coversOptions struct {
|
||||||
|
|
||||||
type CoversOption func(*coversOptions)
|
type CoversOption func(*coversOptions)
|
||||||
|
|
||||||
|
// Must is for testing.
|
||||||
|
func Must(rt RuleTime, err error) RuleTime {
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return rt
|
||||||
|
}
|
||||||
|
|
||||||
// WithLocation makes Covers use the provided *time.Location
|
// WithLocation makes Covers use the provided *time.Location
|
||||||
func WithLocation(loc *time.Location) CoversOption {
|
func WithLocation(loc *time.Location) CoversOption {
|
||||||
return func(o *coversOptions) {
|
return func(o *coversOptions) {
|
||||||
|
@ -53,11 +62,7 @@ func (rt *RuleTime) Covers(t time.Time, opts ...CoversOption) bool {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if t.After(start) && t.Before(end) {
|
return t.After(start) && t.Before(end)
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// CoversNow returns whether the RuleTime covers this instant.
|
// CoversNow returns whether the RuleTime covers this instant.
|
||||||
|
|
|
@ -185,6 +185,18 @@ func TestCovers(t *testing.T) {
|
||||||
covers: true,
|
covers: true,
|
||||||
opts: []ruletime.CoversOption{ruletime.WithLocation(tz("America/Chicago"))},
|
opts: []ruletime.CoversOption{ruletime.WithLocation(tz("America/Chicago"))},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "normal",
|
||||||
|
timespec: "1:00+5h",
|
||||||
|
t: tM("17:07:00"),
|
||||||
|
covers: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "normal",
|
||||||
|
timespec: "1:00+5h",
|
||||||
|
t: tM("3:07:00"),
|
||||||
|
covers: true,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: "24h duration",
|
name: "24h duration",
|
||||||
timespec: "15:00+24h",
|
timespec: "15:00+24h",
|
||||||
|
|
58
pkg/calls/alertconfig.go
Normal file
58
pkg/calls/alertconfig.go
Normal file
|
@ -0,0 +1,58 @@
|
||||||
|
package calls
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"dynatron.me/x/stillbox/internal/ruletime"
|
||||||
|
"dynatron.me/x/stillbox/internal/trending"
|
||||||
|
)
|
||||||
|
|
||||||
|
type AlertConfig map[Talkgroup][]AlertRule
|
||||||
|
|
||||||
|
type AlertRule struct {
|
||||||
|
Times []ruletime.RuleTime `json:"times"`
|
||||||
|
ScoreMultiplier float32 `json:"mult"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ac AlertConfig) AddAlertConfig(tg Talkgroup, confBytes []byte) error {
|
||||||
|
if len(confBytes) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var rules []AlertRule
|
||||||
|
err := json.Unmarshal(confBytes, &rules)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
ac[tg] = rules
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ac AlertConfig) ScaleScore(score trending.Score[Talkgroup], t time.Time, coversOpts ...ruletime.CoversOption) float64 {
|
||||||
|
s, has := ac[score.ID]
|
||||||
|
if !has {
|
||||||
|
return score.Score
|
||||||
|
}
|
||||||
|
|
||||||
|
final := score.Score
|
||||||
|
|
||||||
|
for _, ar := range s {
|
||||||
|
if ar.MatchTime(t, coversOpts...) {
|
||||||
|
final *= float64(ar.ScoreMultiplier)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return final
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ar *AlertRule) MatchTime(t time.Time, coversOpts ...ruletime.CoversOption) bool {
|
||||||
|
for _, at := range ar.Times {
|
||||||
|
if at.Covers(t, coversOpts...) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
141
pkg/calls/alertconfig_test.go
Normal file
141
pkg/calls/alertconfig_test.go
Normal file
|
@ -0,0 +1,141 @@
|
||||||
|
package calls_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"math"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"dynatron.me/x/stillbox/internal/ruletime"
|
||||||
|
"dynatron.me/x/stillbox/internal/trending"
|
||||||
|
"dynatron.me/x/stillbox/pkg/calls"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestAlertConfig(t *testing.T) {
|
||||||
|
ac := make(calls.AlertConfig)
|
||||||
|
parseTests := []struct {
|
||||||
|
name string
|
||||||
|
tg calls.Talkgroup
|
||||||
|
conf string
|
||||||
|
compare []calls.AlertRule
|
||||||
|
expectErr error
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "base case",
|
||||||
|
tg: calls.TG(197, 3),
|
||||||
|
conf: `[{"times":["7:00+2h","01:00+1h","16:00+1h","19:00+4h"],"mult":0.2},{"times":["11:00+1h","15:00+30m","16:03+20m"],"mult":2.0}]`,
|
||||||
|
compare: []calls.AlertRule{
|
||||||
|
{
|
||||||
|
Times: []ruletime.RuleTime{
|
||||||
|
ruletime.Must(ruletime.New("7:00+2h")),
|
||||||
|
ruletime.Must(ruletime.New("1:00+1h")),
|
||||||
|
ruletime.Must(ruletime.New("16:00+1h")),
|
||||||
|
ruletime.Must(ruletime.New("19:00+4h")),
|
||||||
|
},
|
||||||
|
ScoreMultiplier: 0.2,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Times: []ruletime.RuleTime{
|
||||||
|
ruletime.Must(ruletime.New("11:00+1h")),
|
||||||
|
ruletime.Must(ruletime.New("15:00+30m")),
|
||||||
|
ruletime.Must(ruletime.New("16:03+20m")),
|
||||||
|
},
|
||||||
|
ScoreMultiplier: 2.0,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "bad spec",
|
||||||
|
tg: calls.TG(197, 3),
|
||||||
|
conf: `[{"times":["26:00+2h","01:00+1h","19:00+4h"],"mult":0.2},{"times":["11:00+1h","15:00+30m"],"mult":2.0}]`,
|
||||||
|
expectErr: errors.New("'26:00+2h': invalid hours"),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range parseTests {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
err := ac.AddAlertConfig(tc.tg, []byte(tc.conf))
|
||||||
|
if tc.expectErr != nil {
|
||||||
|
require.Error(t, err)
|
||||||
|
assert.Contains(t, err.Error(), tc.expectErr.Error())
|
||||||
|
} else {
|
||||||
|
assert.Equal(t, tc.compare, ac[tc.tg])
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
tMust := func(s string) time.Time {
|
||||||
|
t, err := time.ParseInLocation("2006-01-02 15:04", "2024-11-02 "+s, time.Local)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return t
|
||||||
|
}
|
||||||
|
|
||||||
|
evalTests := []struct {
|
||||||
|
name string
|
||||||
|
tg calls.Talkgroup
|
||||||
|
t time.Time
|
||||||
|
origScore float64
|
||||||
|
expectScore float64
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "base eval",
|
||||||
|
tg: calls.TG(197, 3),
|
||||||
|
t: tMust("1:20"),
|
||||||
|
origScore: 3,
|
||||||
|
expectScore: 0.6,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "base eval",
|
||||||
|
tg: calls.TG(197, 3),
|
||||||
|
t: tMust("23:03"),
|
||||||
|
origScore: 3,
|
||||||
|
expectScore: 3,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "base eval",
|
||||||
|
tg: calls.TG(197, 3),
|
||||||
|
t: tMust("8:03"),
|
||||||
|
origScore: 1.0,
|
||||||
|
expectScore: 0.2,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "base eval",
|
||||||
|
tg: calls.TG(197, 3),
|
||||||
|
t: tMust("15:15"),
|
||||||
|
origScore: 3.0,
|
||||||
|
expectScore: 6.0,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "overlapping eval",
|
||||||
|
tg: calls.TG(197, 3),
|
||||||
|
t: tMust("16:10"),
|
||||||
|
origScore: 1.0,
|
||||||
|
expectScore: 0.4,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range evalTests {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
cs := trending.Score[calls.Talkgroup]{
|
||||||
|
ID: tc.tg,
|
||||||
|
Score: tc.origScore,
|
||||||
|
}
|
||||||
|
assert.Equal(t, tc.expectScore, toFixed(ac.ScaleScore(cs, tc.t), 5))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func round(num float64) int {
|
||||||
|
return int(num + math.Copysign(0.5, num))
|
||||||
|
}
|
||||||
|
|
||||||
|
func toFixed(num float64, precision int) float64 {
|
||||||
|
output := math.Pow(10, float64(precision))
|
||||||
|
return float64(round(num*output)) / output
|
||||||
|
}
|
|
@ -2,8 +2,17 @@ package calls
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
"dynatron.me/x/stillbox/pkg/gordio/database"
|
"dynatron.me/x/stillbox/pkg/gordio/database"
|
||||||
|
|
||||||
|
"dynatron.me/x/stillbox/internal/ruletime"
|
||||||
|
"dynatron.me/x/stillbox/internal/trending"
|
||||||
|
|
||||||
|
"github.com/jackc/pgx/v5"
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Talkgroup struct {
|
type Talkgroup struct {
|
||||||
|
@ -27,6 +36,10 @@ func (t Talkgroup) Pack() int64 {
|
||||||
return int64((int64(t.System) << 32) | int64(t.Talkgroup))
|
return int64((int64(t.System) << 32) | int64(t.Talkgroup))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t Talkgroup) String() string {
|
||||||
|
return fmt.Sprintf("%d:%d", t.System, t.Talkgroup)
|
||||||
|
}
|
||||||
|
|
||||||
func PackedTGs(tg []Talkgroup) []int64 {
|
func PackedTGs(tg []Talkgroup) []int64 {
|
||||||
s := make([]int64, len(tg))
|
s := make([]int64, len(tg))
|
||||||
|
|
||||||
|
@ -38,41 +51,141 @@ func PackedTGs(tg []Talkgroup) []int64 {
|
||||||
}
|
}
|
||||||
|
|
||||||
type tgMap map[Talkgroup]database.GetTalkgroupWithLearnedByPackedIDsRow
|
type tgMap map[Talkgroup]database.GetTalkgroupWithLearnedByPackedIDsRow
|
||||||
type TalkgroupCache struct {
|
|
||||||
|
type TalkgroupCache interface {
|
||||||
|
TG(ctx context.Context, tg Talkgroup) (database.GetTalkgroupWithLearnedByPackedIDsRow, bool)
|
||||||
|
SystemName(ctx context.Context, id int) (string, bool)
|
||||||
|
ScaleScore(score trending.Score[Talkgroup], t time.Time, coversOpts ...ruletime.CoversOption) float64
|
||||||
|
Hint(ctx context.Context, tgs []Talkgroup) error
|
||||||
|
Load(ctx context.Context, tgs []int64) error
|
||||||
|
Invalidate()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *talkgroupCache) Invalidate() {
|
||||||
|
t.Lock()
|
||||||
|
defer t.Unlock()
|
||||||
|
clear(t.tgs)
|
||||||
|
clear(t.systems)
|
||||||
|
clear(t.AlertConfig)
|
||||||
|
}
|
||||||
|
|
||||||
|
type talkgroupCache struct {
|
||||||
|
sync.RWMutex
|
||||||
|
AlertConfig
|
||||||
tgs tgMap
|
tgs tgMap
|
||||||
systems map[int32]string
|
systems map[int32]string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTalkgroupCache(ctx context.Context, packedTgs []int64) (*TalkgroupCache, error) {
|
func NewTalkgroupCache() TalkgroupCache {
|
||||||
tgc := &TalkgroupCache{
|
tgc := &talkgroupCache{
|
||||||
tgs: make(tgMap),
|
tgs: make(tgMap),
|
||||||
systems: make(map[int32]string),
|
systems: make(map[int32]string),
|
||||||
|
AlertConfig: make(AlertConfig),
|
||||||
}
|
}
|
||||||
|
|
||||||
return tgc, tgc.LoadTGs(ctx, packedTgs)
|
return tgc
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TalkgroupCache) LoadTGs(ctx context.Context, packedTgs []int64) error {
|
func (t *talkgroupCache) Hint(ctx context.Context, tgs []Talkgroup) error {
|
||||||
tgRecords, err := database.FromCtx(ctx).GetTalkgroupWithLearnedByPackedIDs(ctx, packedTgs)
|
t.RLock()
|
||||||
|
var toLoad []int64
|
||||||
|
if len(t.tgs) > len(tgs)/2 { // TODO: instrument this
|
||||||
|
for _, tg := range tgs {
|
||||||
|
_, ok := t.tgs[tg]
|
||||||
|
if !ok {
|
||||||
|
toLoad = append(toLoad, tg.Pack())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
toLoad = make([]int64, 0, len(tgs))
|
||||||
|
for _, g := range tgs {
|
||||||
|
toLoad = append(toLoad, g.Pack())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(toLoad) > 0 {
|
||||||
|
t.RUnlock()
|
||||||
|
return t.Load(ctx, toLoad)
|
||||||
|
}
|
||||||
|
|
||||||
|
t.RUnlock()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *talkgroupCache) add(rec database.GetTalkgroupWithLearnedByPackedIDsRow) error {
|
||||||
|
tg := TG(rec.SystemID, rec.Tgid)
|
||||||
|
t.tgs[tg] = rec
|
||||||
|
t.systems[rec.SystemID] = rec.SystemName
|
||||||
|
|
||||||
|
return t.AlertConfig.AddAlertConfig(tg, rec.AlertConfig)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *talkgroupCache) Load(ctx context.Context, tgs []int64) error {
|
||||||
|
tgRecords, err := database.FromCtx(ctx).GetTalkgroupWithLearnedByPackedIDs(ctx, tgs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
t.Lock()
|
||||||
|
defer t.Unlock()
|
||||||
|
|
||||||
for _, rec := range tgRecords {
|
for _, rec := range tgRecords {
|
||||||
t.tgs[TG(rec.SystemID, rec.Tgid)] = rec
|
err := t.add(rec)
|
||||||
t.systems[rec.SystemID] = rec.SystemName
|
|
||||||
|
if err != nil {
|
||||||
|
log.Error().Err(err).Msg("add alert config fail")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TalkgroupCache) TG(tg Talkgroup) (database.GetTalkgroupWithLearnedByPackedIDsRow, bool) {
|
func (t *talkgroupCache) TG(ctx context.Context, tg Talkgroup) (database.GetTalkgroupWithLearnedByPackedIDsRow, bool) {
|
||||||
|
t.RLock()
|
||||||
rec, has := t.tgs[tg]
|
rec, has := t.tgs[tg]
|
||||||
|
t.RUnlock()
|
||||||
|
|
||||||
return rec, has
|
if has {
|
||||||
|
return rec, has
|
||||||
|
}
|
||||||
|
|
||||||
|
recs, err := database.FromCtx(ctx).GetTalkgroupWithLearnedByPackedIDs(ctx, []int64{tg.Pack()})
|
||||||
|
switch err {
|
||||||
|
case nil:
|
||||||
|
case pgx.ErrNoRows:
|
||||||
|
return database.GetTalkgroupWithLearnedByPackedIDsRow{}, false
|
||||||
|
default:
|
||||||
|
log.Error().Err(err).Msg("TG() cache add db get")
|
||||||
|
return database.GetTalkgroupWithLearnedByPackedIDsRow{}, false
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(recs) < 1 {
|
||||||
|
return database.GetTalkgroupWithLearnedByPackedIDsRow{}, false
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Lock()
|
||||||
|
defer t.Unlock()
|
||||||
|
err = t.add(recs[0])
|
||||||
|
if err != nil {
|
||||||
|
log.Error().Err(err).Msg("TG() cache add")
|
||||||
|
return recs[0], false
|
||||||
|
}
|
||||||
|
|
||||||
|
return recs[0], true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TalkgroupCache) SystemName(id int) (string, bool) {
|
func (t *talkgroupCache) SystemName(ctx context.Context, id int) (name string, has bool) {
|
||||||
n, has := t.systems[int32(id)]
|
n, has := t.systems[int32(id)]
|
||||||
|
|
||||||
|
if !has {
|
||||||
|
sys, err := database.FromCtx(ctx).GetSystemName(ctx, id)
|
||||||
|
if err != nil {
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
|
||||||
|
return sys, true
|
||||||
|
}
|
||||||
|
|
||||||
return n, has
|
return n, has
|
||||||
}
|
}
|
||||||
|
|
|
@ -53,6 +53,7 @@ type alerter struct {
|
||||||
alertCache map[cl.Talkgroup]Alert
|
alertCache map[cl.Talkgroup]Alert
|
||||||
renotify time.Duration
|
renotify time.Duration
|
||||||
notifier notify.Notifier
|
notifier notify.Notifier
|
||||||
|
tgCache cl.TalkgroupCache
|
||||||
}
|
}
|
||||||
|
|
||||||
type offsetClock time.Duration
|
type offsetClock time.Duration
|
||||||
|
@ -87,7 +88,7 @@ func WithNotifier(n notify.Notifier) AlertOption {
|
||||||
}
|
}
|
||||||
|
|
||||||
// New creates a new Alerter using the provided configuration.
|
// New creates a new Alerter using the provided configuration.
|
||||||
func New(cfg config.Alerting, opts ...AlertOption) Alerter {
|
func New(cfg config.Alerting, tgCache cl.TalkgroupCache, opts ...AlertOption) Alerter {
|
||||||
if !cfg.Enable {
|
if !cfg.Enable {
|
||||||
return &noopAlerter{}
|
return &noopAlerter{}
|
||||||
}
|
}
|
||||||
|
@ -97,6 +98,7 @@ func New(cfg config.Alerting, opts ...AlertOption) Alerter {
|
||||||
alertCache: make(map[cl.Talkgroup]Alert),
|
alertCache: make(map[cl.Talkgroup]Alert),
|
||||||
clock: timeseries.DefaultClock,
|
clock: timeseries.DefaultClock,
|
||||||
renotify: DefaultRenotify,
|
renotify: DefaultRenotify,
|
||||||
|
tgCache: tgCache,
|
||||||
}
|
}
|
||||||
|
|
||||||
if cfg.Renotify != nil {
|
if cfg.Renotify != nil {
|
||||||
|
@ -107,7 +109,7 @@ func New(cfg config.Alerting, opts ...AlertOption) Alerter {
|
||||||
opt(as)
|
opt(as)
|
||||||
}
|
}
|
||||||
|
|
||||||
as.scorer = trending.NewScorer[cl.Talkgroup](
|
as.scorer = trending.NewScorer(
|
||||||
trending.WithTimeSeries(as.newTimeSeries),
|
trending.WithTimeSeries(as.newTimeSeries),
|
||||||
trending.WithStorageDuration[cl.Talkgroup](time.Hour*24*time.Duration(cfg.LookbackDays)),
|
trending.WithStorageDuration[cl.Talkgroup](time.Hour*24*time.Duration(cfg.LookbackDays)),
|
||||||
trending.WithRecentDuration[cl.Talkgroup](time.Duration(cfg.Recent)),
|
trending.WithRecentDuration[cl.Talkgroup](time.Duration(cfg.Recent)),
|
||||||
|
@ -154,29 +156,64 @@ const notificationTemplStr = `{{ range . -}}
|
||||||
|
|
||||||
var notificationTemplate = template.Must(template.New("notification").Funcs(funcMap).Parse(notificationTemplStr))
|
var notificationTemplate = template.Must(template.New("notification").Funcs(funcMap).Parse(notificationTemplStr))
|
||||||
|
|
||||||
|
func (as *alerter) eval(ctx context.Context, now time.Time, testMode bool) ([]Alert, error) {
|
||||||
|
err := as.tgCache.Hint(ctx, as.scoredTGs())
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("prime TG cache: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
db := database.FromCtx(ctx)
|
||||||
|
|
||||||
|
var notifications []Alert
|
||||||
|
for _, s := range as.scores {
|
||||||
|
origScore := s.Score
|
||||||
|
tgr, has := as.tgCache.TG(ctx, s.ID)
|
||||||
|
if has {
|
||||||
|
if !tgr.Alert {
|
||||||
|
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 {
|
||||||
|
a, err := as.makeAlert(ctx, s, origScore)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("makeAlert: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
as.alertCache[s.ID] = a
|
||||||
|
|
||||||
|
if !testMode {
|
||||||
|
err = db.AddAlert(ctx, a.ToAddAlertParams())
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("addAlert: %w", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
notifications = append(notifications, a)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return notifications, nil
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func (as *alerter) testNotifyHandler(w http.ResponseWriter, r *http.Request) {
|
func (as *alerter) testNotifyHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
as.RLock()
|
as.RLock()
|
||||||
defer as.RUnlock()
|
defer as.RUnlock()
|
||||||
alerts := make([]Alert, 0, len(as.scores))
|
alerts := make([]Alert, 0, len(as.scores))
|
||||||
ctx := r.Context()
|
ctx := r.Context()
|
||||||
|
|
||||||
tgc, err := cl.NewTalkgroupCache(ctx, as.packedScoredTGs())
|
alerts, err := as.eval(ctx, time.Now(), true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Err(err).Msg("test notificaiton tg cache")
|
log.Error().Err(err).Msg("test notification eval")
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, s := range as.scores {
|
|
||||||
a, err := makeAlert(tgc, s)
|
|
||||||
if err != nil {
|
|
||||||
log.Error().Err(err).Msg("test notificaiton")
|
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
alerts = append(alerts, a)
|
|
||||||
}
|
|
||||||
|
|
||||||
err = as.sendNotification(ctx, alerts)
|
err = as.sendNotification(ctx, alerts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Err(err).Msg("test notification send")
|
log.Error().Err(err).Msg("test notification send")
|
||||||
|
@ -187,14 +224,24 @@ func (as *alerter) testNotifyHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
_, _ = w.Write([]byte("Sent"))
|
_, _ = w.Write([]byte("Sent"))
|
||||||
}
|
}
|
||||||
|
|
||||||
// packedScoredTGs gets a packed list of TG IDs for DB use.
|
// scoredTGs gets a list of TGs.
|
||||||
func (as *alerter) packedScoredTGs() []int64 {
|
func (as *alerter) scoredTGs() []cl.Talkgroup {
|
||||||
packedTGs := make([]int64, 0, len(as.scores))
|
tgs := make([]cl.Talkgroup, 0, len(as.scores))
|
||||||
for _, s := range as.scores {
|
for _, s := range as.scores {
|
||||||
packedTGs = append(packedTGs, s.ID.Pack())
|
tgs = append(tgs, s.ID)
|
||||||
}
|
}
|
||||||
|
|
||||||
return packedTGs
|
return tgs
|
||||||
|
}
|
||||||
|
|
||||||
|
// packedScoredTGs gets a list of packed TGIDs.
|
||||||
|
func (as *alerter) packedScoredTGs() []int64 {
|
||||||
|
tgs := make([]int64, 0, len(as.scores))
|
||||||
|
for _, s := range as.scores {
|
||||||
|
tgs = append(tgs, s.ID.Pack())
|
||||||
|
}
|
||||||
|
|
||||||
|
return tgs
|
||||||
}
|
}
|
||||||
|
|
||||||
// notify iterates the scores and sends out any necessary notifications
|
// notify iterates the scores and sends out any necessary notifications
|
||||||
|
@ -203,46 +250,14 @@ func (as *alerter) notify(ctx context.Context) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
now := time.Now()
|
|
||||||
|
|
||||||
as.Lock()
|
as.Lock()
|
||||||
defer as.Unlock()
|
defer as.Unlock()
|
||||||
|
|
||||||
tgc, err := cl.NewTalkgroupCache(ctx, as.packedScoredTGs())
|
notifications, err := as.eval(ctx, time.Now(), false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
db := database.FromCtx(ctx)
|
|
||||||
|
|
||||||
var notifications []Alert
|
|
||||||
for _, s := range as.scores {
|
|
||||||
tgr, has := tgc.TG(s.ID)
|
|
||||||
if has {
|
|
||||||
if !tgr.Alert {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
s.Score *= float64(tgr.Weight)
|
|
||||||
}
|
|
||||||
if s.Score > as.cfg.AlertThreshold {
|
|
||||||
if old, inCache := as.alertCache[s.ID]; !inCache || now.Sub(old.Timestamp) > as.renotify {
|
|
||||||
a, err := makeAlert(tgc, s)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
as.alertCache[s.ID] = a
|
|
||||||
|
|
||||||
err = db.AddAlert(ctx, a.ToAddAlertParams())
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
notifications = append(notifications, a)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(notifications) > 0 {
|
if len(notifications) > 0 {
|
||||||
return as.sendNotification(ctx, notifications)
|
return as.sendNotification(ctx, notifications)
|
||||||
}
|
}
|
||||||
|
@ -255,17 +270,20 @@ type Alert struct {
|
||||||
Timestamp time.Time
|
Timestamp time.Time
|
||||||
TGName string
|
TGName string
|
||||||
Score trending.Score[cl.Talkgroup]
|
Score trending.Score[cl.Talkgroup]
|
||||||
|
OrigScore float64
|
||||||
Weight float32
|
Weight float32
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *Alert) ToAddAlertParams() database.AddAlertParams {
|
func (a *Alert) ToAddAlertParams() database.AddAlertParams {
|
||||||
f32score := float32(a.Score.Score)
|
f32score := float32(a.Score.Score)
|
||||||
|
f32origscore := float32(a.OrigScore)
|
||||||
return database.AddAlertParams{
|
return database.AddAlertParams{
|
||||||
ID: a.ID,
|
ID: a.ID,
|
||||||
Time: pgtype.Timestamptz{Time: a.Timestamp, Valid: true},
|
Time: pgtype.Timestamptz{Time: a.Timestamp, Valid: true},
|
||||||
PackedTg: a.Score.ID.Pack(),
|
PackedTg: a.Score.ID.Pack(),
|
||||||
Weight: &a.Weight,
|
Weight: &a.Weight,
|
||||||
Score: &f32score,
|
Score: &f32score,
|
||||||
|
OrigScore: &f32origscore,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -285,15 +303,16 @@ func (as *alerter) sendNotification(ctx context.Context, n []Alert) error {
|
||||||
|
|
||||||
// makeAlert creates a notification for later rendering by the template.
|
// makeAlert creates a notification for later rendering by the template.
|
||||||
// It takes a talkgroup Score as input.
|
// It takes a talkgroup Score as input.
|
||||||
func makeAlert(tgs *cl.TalkgroupCache, score trending.Score[cl.Talkgroup]) (Alert, error) {
|
func (as *alerter) makeAlert(ctx context.Context, score trending.Score[cl.Talkgroup], origScore float64) (Alert, error) {
|
||||||
d := Alert{
|
d := Alert{
|
||||||
ID: uuid.New(),
|
ID: uuid.New(),
|
||||||
Score: score,
|
Score: score,
|
||||||
Timestamp: time.Now(),
|
Timestamp: time.Now(),
|
||||||
Weight: 1.0,
|
Weight: 1.0,
|
||||||
|
OrigScore: origScore,
|
||||||
}
|
}
|
||||||
|
|
||||||
tgRecord, has := tgs.TG(score.ID)
|
tgRecord, has := as.tgCache.TG(ctx, score.ID)
|
||||||
switch has {
|
switch has {
|
||||||
case true:
|
case true:
|
||||||
d.Weight = tgRecord.Weight
|
d.Weight = tgRecord.Weight
|
||||||
|
@ -307,7 +326,7 @@ func makeAlert(tgs *cl.TalkgroupCache, score trending.Score[cl.Talkgroup]) (Aler
|
||||||
d.TGName = fmt.Sprintf("%s:%d", tgRecord.SystemName, int(score.ID.Talkgroup))
|
d.TGName = fmt.Sprintf("%s:%d", tgRecord.SystemName, int(score.ID.Talkgroup))
|
||||||
}
|
}
|
||||||
case false:
|
case false:
|
||||||
system, has := tgs.SystemName(int(score.ID.System))
|
system, has := as.tgCache.SystemName(ctx, int(score.ID.System))
|
||||||
if has {
|
if has {
|
||||||
d.TGName = fmt.Sprintf("%s:%d", system, int(score.ID.Talkgroup))
|
d.TGName = fmt.Sprintf("%s:%d", system, int(score.ID.Talkgroup))
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -58,10 +58,12 @@ func (s *Simulation) stepClock(t time.Time) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Simulate begins the simulation using the DB handle from ctx. It returns final scores.
|
// Simulate begins the simulation using the DB handle from ctx. It returns final scores.
|
||||||
func (s *Simulation) Simulate(ctx context.Context) trending.Scores[cl.Talkgroup] {
|
func (s *Simulation) Simulate(ctx context.Context) (trending.Scores[cl.Talkgroup], error) {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
|
tgc := cl.NewTalkgroupCache()
|
||||||
|
|
||||||
s.Enable = true
|
s.Enable = true
|
||||||
s.alerter = New(s.Alerting, WithClock(&s.clock)).(*alerter)
|
s.alerter = New(s.Alerting, tgc, WithClock(&s.clock)).(*alerter)
|
||||||
if time.Time(s.ScoreEnd).IsZero() {
|
if time.Time(s.ScoreEnd).IsZero() {
|
||||||
s.ScoreEnd = jsontime.Time(now)
|
s.ScoreEnd = jsontime.Time(now)
|
||||||
}
|
}
|
||||||
|
@ -79,7 +81,7 @@ func (s *Simulation) Simulate(ctx context.Context) trending.Scores[cl.Talkgroup]
|
||||||
// backfill from lookback start until score start
|
// backfill from lookback start until score start
|
||||||
_, err := s.backfill(ctx, sinceLookback, time.Time(s.ScoreStart))
|
_, err := s.backfill(ctx, sinceLookback, time.Time(s.ScoreStart))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Err(err).Msg("simulate backfill")
|
return nil, fmt.Errorf("simulate backfill: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// initial score
|
// initial score
|
||||||
|
@ -99,13 +101,13 @@ func (s *Simulation) Simulate(ctx context.Context) trending.Scores[cl.Talkgroup]
|
||||||
// backfill from scorestart until now. sim is enabled, so scoring will be done by stepClock()
|
// backfill from scorestart until now. sim is enabled, so scoring will be done by stepClock()
|
||||||
_, err = s.backfill(ctx, time.Time(s.ScoreStart), scoreEnd)
|
_, err = s.backfill(ctx, time.Time(s.ScoreStart), scoreEnd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Err(err).Msg("simulate backfill final")
|
return nil, fmt.Errorf("simulate backfill final: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
s.lastScore = scoreEnd
|
s.lastScore = scoreEnd
|
||||||
sort.Sort(s.scores)
|
sort.Sort(s.scores)
|
||||||
|
|
||||||
return s.scores
|
return s.scores, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// simulateHandler is the POST endpoint handler.
|
// simulateHandler is the POST endpoint handler.
|
||||||
|
@ -136,6 +138,11 @@ func (as *alerter) simulateHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
s.Simulate(ctx)
|
_, err = s.Simulate(ctx)
|
||||||
|
if err != nil {
|
||||||
|
err = fmt.Errorf("simulate: %w", err)
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
s.tgStatsHandler(w, r)
|
s.tgStatsHandler(w, r)
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,7 +13,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const addAlert = `-- name: AddAlert :exec
|
const addAlert = `-- name: AddAlert :exec
|
||||||
INSERT INTO alerts (id, time, talkgroup, weight, score, metadata)
|
INSERT INTO alerts (id, time, talkgroup, weight, score, orig_score, metadata)
|
||||||
VALUES
|
VALUES
|
||||||
(
|
(
|
||||||
$1,
|
$1,
|
||||||
|
@ -21,17 +21,19 @@ VALUES
|
||||||
$3,
|
$3,
|
||||||
$4,
|
$4,
|
||||||
$5,
|
$5,
|
||||||
$6
|
$6,
|
||||||
|
$7
|
||||||
)
|
)
|
||||||
`
|
`
|
||||||
|
|
||||||
type AddAlertParams struct {
|
type AddAlertParams struct {
|
||||||
ID uuid.UUID `json:"id"`
|
ID uuid.UUID `json:"id"`
|
||||||
Time pgtype.Timestamptz `json:"time"`
|
Time pgtype.Timestamptz `json:"time"`
|
||||||
PackedTg int64 `json:"packed_tg"`
|
PackedTg int64 `json:"packed_tg"`
|
||||||
Weight *float32 `json:"weight"`
|
Weight *float32 `json:"weight"`
|
||||||
Score *float32 `json:"score"`
|
Score *float32 `json:"score"`
|
||||||
Metadata []byte `json:"metadata"`
|
OrigScore *float32 `json:"orig_score"`
|
||||||
|
Metadata []byte `json:"metadata"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *Queries) AddAlert(ctx context.Context, arg AddAlertParams) error {
|
func (q *Queries) AddAlert(ctx context.Context, arg AddAlertParams) error {
|
||||||
|
@ -41,6 +43,7 @@ func (q *Queries) AddAlert(ctx context.Context, arg AddAlertParams) error {
|
||||||
arg.PackedTg,
|
arg.PackedTg,
|
||||||
arg.Weight,
|
arg.Weight,
|
||||||
arg.Score,
|
arg.Score,
|
||||||
|
arg.OrigScore,
|
||||||
arg.Metadata,
|
arg.Metadata,
|
||||||
)
|
)
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -19,6 +19,7 @@ type Alert struct {
|
||||||
Tgid int32 `json:"tgid"`
|
Tgid int32 `json:"tgid"`
|
||||||
Weight *float32 `json:"weight"`
|
Weight *float32 `json:"weight"`
|
||||||
Score *float32 `json:"score"`
|
Score *float32 `json:"score"`
|
||||||
|
OrigScore *float32 `json:"orig_score"`
|
||||||
Metadata []byte `json:"metadata"`
|
Metadata []byte `json:"metadata"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"dynatron.me/x/stillbox/pkg/calls"
|
||||||
"dynatron.me/x/stillbox/pkg/gordio/alerting"
|
"dynatron.me/x/stillbox/pkg/gordio/alerting"
|
||||||
"dynatron.me/x/stillbox/pkg/gordio/auth"
|
"dynatron.me/x/stillbox/pkg/gordio/auth"
|
||||||
"dynatron.me/x/stillbox/pkg/gordio/config"
|
"dynatron.me/x/stillbox/pkg/gordio/config"
|
||||||
|
@ -34,6 +35,7 @@ type Server struct {
|
||||||
alerter alerting.Alerter
|
alerter alerting.Alerter
|
||||||
notifier notify.Notifier
|
notifier notify.Notifier
|
||||||
hup chan os.Signal
|
hup chan os.Signal
|
||||||
|
tgCache calls.TalkgroupCache
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(ctx context.Context, cfg *config.Config) (*Server, error) {
|
func New(ctx context.Context, cfg *config.Config) (*Server, error) {
|
||||||
|
@ -56,6 +58,8 @@ func New(ctx context.Context, cfg *config.Config) (*Server, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tgCache := calls.NewTalkgroupCache()
|
||||||
|
|
||||||
srv := &Server{
|
srv := &Server{
|
||||||
auth: authenticator,
|
auth: authenticator,
|
||||||
conf: cfg,
|
conf: cfg,
|
||||||
|
@ -63,8 +67,9 @@ func New(ctx context.Context, cfg *config.Config) (*Server, error) {
|
||||||
r: r,
|
r: r,
|
||||||
nex: nexus.New(),
|
nex: nexus.New(),
|
||||||
logger: logger,
|
logger: logger,
|
||||||
alerter: alerting.New(cfg.Alerting, alerting.WithNotifier(notifier)),
|
alerter: alerting.New(cfg.Alerting, tgCache, alerting.WithNotifier(notifier)),
|
||||||
notifier: notifier,
|
notifier: notifier,
|
||||||
|
tgCache: tgCache,
|
||||||
}
|
}
|
||||||
|
|
||||||
srv.sinks.Register("database", sinks.NewDatabaseSink(srv.db), true)
|
srv.sinks.Register("database", sinks.NewDatabaseSink(srv.db), true)
|
||||||
|
|
|
@ -79,6 +79,7 @@ CREATE TABLE IF NOT EXISTS alerts(
|
||||||
tgid INT4 NOT NULL GENERATED ALWAYS AS (talkgroup & x'ffffffff'::BIGINT) STORED,
|
tgid INT4 NOT NULL GENERATED ALWAYS AS (talkgroup & x'ffffffff'::BIGINT) STORED,
|
||||||
weight REAL,
|
weight REAL,
|
||||||
score REAL,
|
score REAL,
|
||||||
|
orig_score REAL,
|
||||||
metadata JSONB
|
metadata JSONB
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
@ -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, metadata)
|
INSERT INTO alerts (id, time, talkgroup, weight, score, orig_score, metadata)
|
||||||
VALUES
|
VALUES
|
||||||
(
|
(
|
||||||
sqlc.arg(id),
|
sqlc.arg(id),
|
||||||
|
@ -32,6 +32,7 @@ VALUES
|
||||||
sqlc.arg(packed_tg),
|
sqlc.arg(packed_tg),
|
||||||
sqlc.arg(weight),
|
sqlc.arg(weight),
|
||||||
sqlc.arg(score),
|
sqlc.arg(score),
|
||||||
|
sqlc.arg(orig_score),
|
||||||
sqlc.arg(metadata)
|
sqlc.arg(metadata)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue