diff --git a/pkg/alerting/rules/alertrules.go b/pkg/alerting/rules/alertrules.go index 22cd0f1..18ed3ce 100644 --- a/pkg/alerting/rules/alertrules.go +++ b/pkg/alerting/rules/alertrules.go @@ -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) } diff --git a/pkg/alerting/rules/alertrules_test.go b/pkg/alerting/rules/alertrules_test.go index 22c1564..8f4e5f7 100644 --- a/pkg/alerting/rules/alertrules_test.go +++ b/pkg/alerting/rules/alertrules_test.go @@ -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)) }) } } diff --git a/pkg/talkgroups/alertconfig.go b/pkg/talkgroups/alertconfig.go deleted file mode 100644 index eaa7294..0000000 --- a/pkg/talkgroups/alertconfig.go +++ /dev/null @@ -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) -} diff --git a/pkg/talkgroups/cache.go b/pkg/talkgroups/cache.go index 9e14588..e5463c7 100644 --- a/pkg/talkgroups/cache.go +++ b/pkg/talkgroups/cache.go @@ -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 } @@ -96,9 +89,8 @@ type cache struct { // NewCache returns a new cache Store. func NewCache() Store { tgc := &cache{ - tgs: make(tgMap), - systems: make(map[int32]string), - AlertConfig: NewAlertConfig(), + tgs: make(tgMap), + systems: make(map[int32]string), } 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) }