Remove alert config double cache #33
4 changed files with 14 additions and 72 deletions
|
@ -8,10 +8,14 @@ import (
|
|||
|
||||
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
|
||||
|
||||
for _, ar := range *ars {
|
||||
for _, ar := range ars {
|
||||
if ar.MatchTime(t, coversOpts...) {
|
||||
final *= float64(ar.ScoreMultiplier)
|
||||
}
|
||||
|
|
|
@ -17,7 +17,6 @@ import (
|
|||
)
|
||||
|
||||
func TestAlertConfig(t *testing.T) {
|
||||
ac := talkgroups.NewAlertConfig()
|
||||
parseTests := []struct {
|
||||
name string
|
||||
tg talkgroups.ID
|
||||
|
@ -57,6 +56,8 @@ func TestAlertConfig(t *testing.T) {
|
|||
},
|
||||
}
|
||||
|
||||
tgc := make(map[talkgroups.ID]rules.AlertRules)
|
||||
|
||||
for _, tc := range parseTests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
var ar rules.AlertRules
|
||||
|
@ -65,8 +66,8 @@ func TestAlertConfig(t *testing.T) {
|
|||
require.Error(t, err)
|
||||
assert.Contains(t, err.Error(), tc.expectErr.Error())
|
||||
} else {
|
||||
ac.Add(tc.tg, ar)
|
||||
assert.Equal(t, tc.compare, ac.GetRules(tc.tg))
|
||||
tgc[tc.tg] = ar
|
||||
assert.Equal(t, tc.compare, ar)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -130,7 +131,7 @@ func TestAlertConfig(t *testing.T) {
|
|||
ID: tc.tg,
|
||||
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))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
|
@ -6,8 +6,6 @@ import (
|
|||
"sync"
|
||||
"time"
|
||||
|
||||
"dynatron.me/x/stillbox/internal/ruletime"
|
||||
|
||||
"dynatron.me/x/stillbox/pkg/config"
|
||||
"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(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(ctx context.Context, tgs []ID) error
|
||||
|
||||
|
@ -83,12 +78,10 @@ func (t *cache) Invalidate() {
|
|||
defer t.Unlock()
|
||||
clear(t.tgs)
|
||||
clear(t.systems)
|
||||
t.AlertConfig.Invalidate()
|
||||
}
|
||||
|
||||
type cache struct {
|
||||
sync.RWMutex
|
||||
AlertConfig
|
||||
tgs tgMap
|
||||
systems map[int32]string
|
||||
}
|
||||
|
@ -98,7 +91,6 @@ func NewCache() Store {
|
|||
tgc := &cache{
|
||||
tgs: make(tgMap),
|
||||
systems: make(map[int32]string),
|
||||
AlertConfig: NewAlertConfig(),
|
||||
}
|
||||
|
||||
return tgc
|
||||
|
@ -139,8 +131,6 @@ func (t *cache) add(rec *Talkgroup) error {
|
|||
t.tgs[tg] = rec
|
||||
t.systems[int32(rec.System.ID)] = rec.System.Name
|
||||
|
||||
t.AlertConfig.Add(tg, rec.AlertConfig)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -231,7 +221,7 @@ func (t *cache) Weight(ctx context.Context, id ID, tm time.Time) float64 {
|
|||
|
||||
m := float64(tg.Weight)
|
||||
|
||||
m *= t.AlertConfig.ApplyAlertRules(id, tm)
|
||||
m *= tg.AlertConfig.Apply(tm)
|
||||
|
||||
return float64(m)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue