stillbox/pkg/gordio/sinks/alerting/alerting.go

153 lines
3.8 KiB
Go
Raw Normal View History

2024-10-23 08:55:19 -04:00
package alerting
import (
"context"
"fmt"
"sync"
"time"
cl "dynatron.me/x/stillbox/pkg/calls"
"dynatron.me/x/stillbox/pkg/gordio/database"
"dynatron.me/x/stillbox/internal/timeseries"
"dynatron.me/x/stillbox/internal/trending"
"github.com/rs/zerolog/log"
)
const (
2024-10-24 07:19:46 -04:00
StorageLookbackDays = 7
2024-10-23 17:08:29 -04:00
HalfLife = 30 * time.Minute
2024-10-24 07:19:46 -04:00
RecentDuration = 2*time.Hour
2024-10-23 17:08:29 -04:00
ScoreThreshold = -1
CountThreshold = 1
2024-10-23 08:55:19 -04:00
)
type AlertSink struct {
sync.RWMutex
scorer trending.Scorer[cl.Talkgroup]
}
2024-10-23 17:08:29 -04:00
type myClock struct {
offset time.Duration
}
func (c *myClock) Now() time.Time {
return time.Now().Add(c.offset)
}
2024-10-23 08:55:19 -04:00
func NewSink(ctx context.Context) *AlertSink {
as := &AlertSink{
scorer: trending.NewScorer[cl.Talkgroup](
trending.WithTimeSeries(newTimeSeries),
trending.WithStorageDuration[cl.Talkgroup](StorageLookbackDays*24*time.Hour),
trending.WithRecentDuration[cl.Talkgroup](RecentDuration),
trending.WithHalfLife[cl.Talkgroup](HalfLife),
2024-10-23 17:08:29 -04:00
trending.WithScoreThreshold[cl.Talkgroup](ScoreThreshold),
trending.WithCountThreshold[cl.Talkgroup](CountThreshold),
2024-10-23 08:55:19 -04:00
),
}
go as.startBackfill(ctx)
return as
}
2024-10-23 17:08:29 -04:00
func newTimeSeries(id cl.Talkgroup) trending.TimeSeries {
ts, _ := timeseries.NewTimeSeries(timeseries.WithGranularities(
[]timeseries.Granularity{
{Granularity: time.Second, Count: 60},
2024-10-24 07:19:46 -04:00
{Granularity: time.Minute, Count: 10},
2024-10-23 17:08:29 -04:00
{Granularity: time.Hour, Count: 24},
{Granularity: time.Hour * 24, Count: StorageLookbackDays},
},
))
return ts
}
2024-10-23 08:55:19 -04:00
func (as *AlertSink) startBackfill(ctx context.Context) {
2024-10-23 17:08:29 -04:00
now := time.Now()
2024-10-24 07:19:46 -04:00
cl := &myClock{-24*StorageLookbackDays*time.Hour}
since := now.Add(StorageLookbackDays * -24 * time.Hour)
2024-10-23 08:55:19 -04:00
log.Debug().Time("since", since).Msg("starting stats backfill")
count, err := as.backfill(ctx, since)
if err != nil {
log.Error().Err(err).Msg("backfill failed")
return
}
2024-10-23 17:08:29 -04:00
log.Debug().Int("count", count).Str("in", time.Now().Sub(now).String()).Int("len", as.scorer.Score().Len()).Msg("backfill finished")
2024-10-24 07:19:46 -04:00
timeseries.DefaultClock = cl
2024-10-23 17:08:29 -04:00
for {
2024-10-24 07:19:46 -04:00
fmt.Printf("offs: %s (%s)\n", cl.offset.String(), cl.Now().String())
2024-10-23 17:08:29 -04:00
as.printScores(ctx)
cl.offset += time.Minute*5
if cl.offset == time.Minute*5 {
break
}
}
2024-10-23 08:55:19 -04:00
}
2024-10-23 17:08:29 -04:00
func (as *AlertSink) printScores(ctx context.Context) {
db := database.FromCtx(ctx)
as.Lock()
defer as.Unlock()
2024-10-23 08:55:19 -04:00
scores := as.scorer.Score()
2024-10-24 07:19:46 -04:00
//fmt.Printf("score len is %d\n", scores.Len())
2024-10-23 17:08:29 -04:00
//const scoreMult = 1000000000
const scoreMult = 1
2024-10-23 08:55:19 -04:00
for _, s := range scores {
2024-10-24 07:19:46 -04:00
if s.ID.Talkgroup != 1185 {
2024-10-23 17:08:29 -04:00
continue
}
tg, _ := db.GetTalkgroup(ctx, int(s.ID.System), int(s.ID.Talkgroup))
tgn := ""
if tg.Name != nil {
tgn = *tg.Name
}
fmt.Printf("%s\t\t\t%d:%d c %f\trc %f\tscore %f\tprob %f\texp %f\tmax %f\tkl %f\n", tgn, s.ID.System, s.ID.Talkgroup,
s.Count, s.RecentCount, s.Score*scoreMult, s.Probability, s.Expectation, s.Maximum, s.KLScore)
2024-10-23 08:55:19 -04:00
}
}
func (as *AlertSink) backfill(ctx context.Context, since time.Time) (count int, err error) {
db := database.FromCtx(ctx)
2024-10-24 07:19:46 -04:00
const backfillStatsQuery = `SELECT system, talkgroup, call_date FROM calls WHERE call_date > $1 AND call_date < $2 AND talkgroup = 1185 `
2024-10-23 08:55:19 -04:00
2024-10-23 17:08:29 -04:00
rows, err := db.Query(ctx, backfillStatsQuery, since, timeseries.DefaultClock.Now())
2024-10-23 08:55:19 -04:00
if err != nil {
return count, err
}
defer rows.Close()
as.Lock()
defer as.Unlock()
for rows.Next() {
var tg cl.Talkgroup
var callDate time.Time
if err := rows.Scan(&tg.System, &tg.Talkgroup, &callDate); err != nil {
return count, err
}
as.scorer.AddEvent(tg, callDate)
count++
}
if err := rows.Err(); err != nil {
return count, err
}
return count, nil
}
func (as *AlertSink) SinkType() string {
return "alerting"
}
2024-10-23 17:08:29 -04:00
func (as *AlertSink) Call(ctx context.Context, call *cl.Call) error {
as.Lock()
defer as.Unlock()
as.scorer.AddEvent(call.TalkgroupTuple(), call.DateTime)
2024-10-23 08:55:19 -04:00
return nil
}