stillbox/pkg/talkgroups/cache.go

196 lines
4 KiB
Go
Raw Normal View History

2024-11-03 07:58:41 -05:00
package talkgroups
2024-10-31 16:14:38 -04:00
import (
"context"
2024-11-03 09:45:51 -05:00
"errors"
2024-11-02 11:39:02 -04:00
"sync"
"time"
2024-10-31 16:14:38 -04:00
2024-11-03 07:19:03 -05:00
"dynatron.me/x/stillbox/pkg/database"
2024-11-02 09:41:48 -04:00
2024-11-02 11:39:02 -04:00
"dynatron.me/x/stillbox/internal/ruletime"
"dynatron.me/x/stillbox/internal/trending"
"github.com/jackc/pgx/v5"
2024-11-02 09:41:48 -04:00
"github.com/rs/zerolog/log"
2024-10-31 16:14:38 -04:00
)
2024-11-03 09:45:51 -05:00
type tgMap map[ID]Talkgroup
2024-10-31 16:14:38 -04:00
2024-11-03 08:09:49 -05:00
type Store interface {
2024-11-03 09:45:51 -05:00
// TG retrieves a Talkgroup from the Store.
TG(ctx context.Context, tg ID) (Talkgroup, error)
2024-11-02 09:41:48 -04:00
2024-11-03 08:09:49 -05:00
// SystemName retrieves a system name from the store. It returns the record and whether one was found.
SystemName(ctx context.Context, id int) (string, bool)
2024-10-31 16:14:38 -04:00
2024-11-03 08:09:49 -05:00
// ApplyAlertRules applies the score's talkgroup alert rules to the call occurring at t and returns the weighted score.
ApplyAlertRules(score trending.Score[ID], t time.Time, coversOpts ...ruletime.CoversOption) float64
2024-10-31 16:14:38 -04:00
2024-11-03 08:09:49 -05:00
// Hint hints the Store that the provided talkgroups will be asked for.
Hint(ctx context.Context, tgs []ID) error
2024-11-02 11:39:02 -04:00
2024-11-03 08:09:49 -05:00
// Load loads the provided packed talkgroup IDs into the Store.
2024-11-02 11:39:02 -04:00
Load(ctx context.Context, tgs []int64) error
2024-11-03 08:09:49 -05:00
// Invalidate invalidates any caching in the Store.
2024-11-02 11:39:02 -04:00
Invalidate()
}
2024-11-03 09:45:51 -05:00
type CtxStoreKeyT string
const CtxStoreKey CtxStoreKeyT = "store"
func CtxWithStore(ctx context.Context, s Store) context.Context {
return context.WithValue(ctx, CtxStoreKey, s)
}
func StoreFrom(ctx context.Context) Store {
s, ok := ctx.Value(CtxStoreKey).(Store)
if !ok {
return NewCache()
}
return s
}
2024-11-03 08:09:49 -05:00
func (t *cache) Invalidate() {
2024-11-02 11:39:02 -04:00
t.Lock()
defer t.Unlock()
clear(t.tgs)
clear(t.systems)
clear(t.AlertConfig)
}
2024-11-03 08:09:49 -05:00
type cache struct {
2024-11-02 11:39:02 -04:00
sync.RWMutex
2024-11-02 09:41:48 -04:00
AlertConfig
2024-10-31 16:14:38 -04:00
tgs tgMap
systems map[int32]string
}
2024-11-03 08:09:49 -05:00
// NewCache returns a new cache Store.
func NewCache() Store {
tgc := &cache{
2024-11-02 09:41:48 -04:00
tgs: make(tgMap),
systems: make(map[int32]string),
AlertConfig: make(AlertConfig),
2024-10-31 16:14:38 -04:00
}
2024-11-02 11:39:02 -04:00
return tgc
}
2024-11-03 08:09:49 -05:00
func (t *cache) Hint(ctx context.Context, tgs []ID) error {
2024-11-02 11:39:02 -04:00
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
2024-10-31 16:14:38 -04:00
}
2024-11-03 09:45:51 -05:00
func (t *cache) add(rec Talkgroup) error {
2024-11-03 08:44:34 -05:00
tg := TG(rec.System.ID, int(rec.Talkgroup.Tgid))
2024-11-02 11:39:02 -04:00
t.tgs[tg] = rec
2024-11-03 08:44:34 -05:00
t.systems[int32(rec.System.ID)] = rec.System.Name
2024-11-02 11:39:02 -04:00
2024-11-03 08:44:34 -05:00
return t.AlertConfig.AddAlertConfig(tg, rec.Talkgroup.AlertConfig)
2024-11-02 11:39:02 -04:00
}
2024-11-03 09:45:51 -05:00
func rowToTalkgroup(r database.GetTalkgroupWithLearnedByPackedIDsRow) Talkgroup {
return Talkgroup{
Talkgroup: r.Talkgroup,
System: r.System,
Learned: r.Learned,
}
}
2024-11-03 08:09:49 -05:00
func (t *cache) Load(ctx context.Context, tgs []int64) error {
2024-11-02 11:39:02 -04:00
tgRecords, err := database.FromCtx(ctx).GetTalkgroupWithLearnedByPackedIDs(ctx, tgs)
2024-10-31 16:14:38 -04:00
if err != nil {
return err
}
2024-11-02 11:39:02 -04:00
t.Lock()
defer t.Unlock()
2024-10-31 16:14:38 -04:00
for _, rec := range tgRecords {
2024-11-03 09:45:51 -05:00
err := t.add(rowToTalkgroup(rec))
2024-11-02 09:41:48 -04:00
if err != nil {
log.Error().Err(err).Msg("add alert config fail")
}
2024-10-31 16:14:38 -04:00
}
return nil
}
2024-11-03 09:45:51 -05:00
var ErrNoTG = errors.New("talkgroup not found")
func (t *cache) TG(ctx context.Context, tg ID) (Talkgroup, error) {
2024-11-02 11:39:02 -04:00
t.RLock()
2024-10-31 16:14:38 -04:00
rec, has := t.tgs[tg]
2024-11-02 11:39:02 -04:00
t.RUnlock()
2024-10-31 16:14:38 -04:00
2024-11-02 11:39:02 -04:00
if has {
2024-11-03 09:45:51 -05:00
return rec, nil
2024-11-02 11:39:02 -04:00
}
recs, err := database.FromCtx(ctx).GetTalkgroupWithLearnedByPackedIDs(ctx, []int64{tg.Pack()})
switch err {
case nil:
case pgx.ErrNoRows:
2024-11-03 09:45:51 -05:00
return Talkgroup{}, ErrNoTG
2024-11-02 11:39:02 -04:00
default:
log.Error().Err(err).Msg("TG() cache add db get")
2024-11-03 09:45:51 -05:00
return Talkgroup{}, errors.Join(ErrNoTG, err)
2024-11-02 11:39:02 -04:00
}
if len(recs) < 1 {
2024-11-03 09:45:51 -05:00
return Talkgroup{}, ErrNoTG
2024-11-02 11:39:02 -04:00
}
t.Lock()
defer t.Unlock()
2024-11-03 09:45:51 -05:00
err = t.add(rowToTalkgroup(recs[0]))
2024-11-02 11:39:02 -04:00
if err != nil {
log.Error().Err(err).Msg("TG() cache add")
2024-11-03 09:45:51 -05:00
return rowToTalkgroup(recs[0]), errors.Join(ErrNoTG, err)
2024-11-02 11:39:02 -04:00
}
2024-11-03 09:45:51 -05:00
return rowToTalkgroup(recs[0]), nil
2024-10-31 16:14:38 -04:00
}
2024-11-03 08:09:49 -05:00
func (t *cache) SystemName(ctx context.Context, id int) (name string, has bool) {
2024-10-31 16:14:38 -04:00
n, has := t.systems[int32(id)]
2024-11-02 11:39:02 -04:00
if !has {
sys, err := database.FromCtx(ctx).GetSystemName(ctx, id)
if err != nil {
return "", false
}
return sys, true
}
2024-10-31 16:14:38 -04:00
return n, has
}