Merge pull request 'Remove alert config double cache' (#33) from reid into trunk

Reviewed-on: #33
This commit is contained in:
Daniel 2024-11-12 09:59:00 -05:00
commit 5a82da2b16
4 changed files with 14 additions and 72 deletions

View file

@ -8,10 +8,14 @@ import (
type AlertRules []AlertRule type AlertRules []AlertRule
func (ars *AlertRules) Apply(t time.Time, coversOpts ...ruletime.CoversOption) float64 { func (ars AlertRules) Apply(t time.Time, coversOpts ...ruletime.CoversOption) float64 {
if ars == nil || len(ars) < 1 {
return 1.0
}
final := 1.0 final := 1.0
for _, ar := range *ars { for _, ar := range ars {
if ar.MatchTime(t, coversOpts...) { if ar.MatchTime(t, coversOpts...) {
final *= float64(ar.ScoreMultiplier) final *= float64(ar.ScoreMultiplier)
} }

View file

@ -17,7 +17,6 @@ import (
) )
func TestAlertConfig(t *testing.T) { func TestAlertConfig(t *testing.T) {
ac := talkgroups.NewAlertConfig()
parseTests := []struct { parseTests := []struct {
name string name string
tg talkgroups.ID tg talkgroups.ID
@ -57,6 +56,8 @@ func TestAlertConfig(t *testing.T) {
}, },
} }
tgc := make(map[talkgroups.ID]rules.AlertRules)
for _, tc := range parseTests { for _, tc := range parseTests {
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {
var ar rules.AlertRules var ar rules.AlertRules
@ -65,8 +66,8 @@ func TestAlertConfig(t *testing.T) {
require.Error(t, err) require.Error(t, err)
assert.Contains(t, err.Error(), tc.expectErr.Error()) assert.Contains(t, err.Error(), tc.expectErr.Error())
} else { } else {
ac.Add(tc.tg, ar) tgc[tc.tg] = ar
assert.Equal(t, tc.compare, ac.GetRules(tc.tg)) assert.Equal(t, tc.compare, ar)
} }
}) })
} }
@ -130,7 +131,7 @@ func TestAlertConfig(t *testing.T) {
ID: tc.tg, ID: tc.tg,
Score: tc.origScore, Score: tc.origScore,
} }
assert.Equal(t, tc.expectScore, toFixed(cs.Score*ac.ApplyAlertRules(cs.ID, tc.t), 5)) assert.Equal(t, tc.expectScore, toFixed(cs.Score*tgc[cs.ID].Apply(tc.t), 5))
}) })
} }
} }

View file

@ -1,53 +0,0 @@
package talkgroups
import (
"sync"
"time"
"dynatron.me/x/stillbox/internal/ruletime"
"dynatron.me/x/stillbox/pkg/alerting/rules"
)
type AlertConfig struct {
sync.RWMutex
m map[ID]rules.AlertRules
}
func NewAlertConfig() AlertConfig {
return AlertConfig{
m: make(map[ID]rules.AlertRules),
}
}
func (ac *AlertConfig) Add(tg ID, r rules.AlertRules) error {
ac.Lock()
defer ac.Unlock()
ac.m[tg] = r
return nil
}
func (ac *AlertConfig) GetRules(tg ID) rules.AlertRules {
ac.RLock()
defer ac.RUnlock()
return ac.m[tg]
}
func (ac *AlertConfig) ApplyAlertRules(id ID, t time.Time, coversOpts ...ruletime.CoversOption) float64 {
ac.RLock()
s, has := ac.m[id]
ac.RUnlock()
if !has {
return 1.0
}
return s.Apply(t, coversOpts...)
}
func (ac *AlertConfig) Invalidate() {
ac.Lock()
defer ac.Unlock()
clear(ac.m)
}

View file

@ -6,8 +6,6 @@ import (
"sync" "sync"
"time" "time"
"dynatron.me/x/stillbox/internal/ruletime"
"dynatron.me/x/stillbox/pkg/config" "dynatron.me/x/stillbox/pkg/config"
"dynatron.me/x/stillbox/pkg/database" "dynatron.me/x/stillbox/pkg/database"
@ -38,9 +36,6 @@ type Store interface {
// SystemName retrieves a system name from the store. It returns the record and whether one was found. // 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) SystemName(ctx context.Context, id int) (string, bool)
// ApplyAlertRules applies the score's talkgroup alert rules to the call occurring at t and returns the weighted score.
ApplyAlertRules(id ID, t time.Time, coversOpts ...ruletime.CoversOption) float64
// Hint hints the Store that the provided talkgroups will be asked for. // Hint hints the Store that the provided talkgroups will be asked for.
Hint(ctx context.Context, tgs []ID) error Hint(ctx context.Context, tgs []ID) error
@ -83,12 +78,10 @@ func (t *cache) Invalidate() {
defer t.Unlock() defer t.Unlock()
clear(t.tgs) clear(t.tgs)
clear(t.systems) clear(t.systems)
t.AlertConfig.Invalidate()
} }
type cache struct { type cache struct {
sync.RWMutex sync.RWMutex
AlertConfig
tgs tgMap tgs tgMap
systems map[int32]string systems map[int32]string
} }
@ -96,9 +89,8 @@ type cache struct {
// NewCache returns a new cache Store. // NewCache returns a new cache Store.
func NewCache() Store { func NewCache() Store {
tgc := &cache{ tgc := &cache{
tgs: make(tgMap), tgs: make(tgMap),
systems: make(map[int32]string), systems: make(map[int32]string),
AlertConfig: NewAlertConfig(),
} }
return tgc return tgc
@ -139,8 +131,6 @@ func (t *cache) add(rec *Talkgroup) error {
t.tgs[tg] = rec t.tgs[tg] = rec
t.systems[int32(rec.System.ID)] = rec.System.Name t.systems[int32(rec.System.ID)] = rec.System.Name
t.AlertConfig.Add(tg, rec.AlertConfig)
return nil return nil
} }
@ -231,7 +221,7 @@ func (t *cache) Weight(ctx context.Context, id ID, tm time.Time) float64 {
m := float64(tg.Weight) m := float64(tg.Weight)
m *= t.AlertConfig.ApplyAlertRules(id, tm) m *= tg.AlertConfig.Apply(tm)
return float64(m) return float64(m)
} }