From c3233f1bc8bcbb5275feda984879bd5dbe478a55 Mon Sep 17 00:00:00 2001 From: Daniel Ponte Date: Sun, 3 Nov 2024 07:58:41 -0500 Subject: [PATCH] Split out talkgroups --- pkg/calls/call.go | 6 ++++ pkg/calls/filter.go | 22 +++++++------- pkg/{calls => talkgroups}/alertconfig.go | 2 +- pkg/{calls => talkgroups}/alertconfig_test.go | 30 +++++++++---------- pkg/{calls => talkgroups}/talkgroups.go | 6 +--- 5 files changed, 35 insertions(+), 31 deletions(-) rename pkg/{calls => talkgroups}/alertconfig.go (98%) rename pkg/{calls => talkgroups}/alertconfig_test.go (83%) rename pkg/{calls => talkgroups}/talkgroups.go (96%) diff --git a/pkg/calls/call.go b/pkg/calls/call.go index ea9c3a2..ea5402f 100644 --- a/pkg/calls/call.go +++ b/pkg/calls/call.go @@ -7,6 +7,7 @@ import ( "dynatron.me/x/stillbox/internal/audio" "dynatron.me/x/stillbox/pkg/auth" "dynatron.me/x/stillbox/pkg/pb" + "dynatron.me/x/stillbox/pkg/talkgroups" "google.golang.org/protobuf/types/known/timestamppb" ) @@ -128,3 +129,8 @@ func (c *Call) computeLength() (err error) { return nil } + +func (c *Call) TalkgroupTuple() talkgroups.Talkgroup { + return talkgroups.TG(c.System, c.Talkgroup) +} + diff --git a/pkg/calls/filter.go b/pkg/calls/filter.go index fe45cd4..a3cf253 100644 --- a/pkg/calls/filter.go +++ b/pkg/calls/filter.go @@ -5,16 +5,18 @@ import ( "dynatron.me/x/stillbox/pkg/database" "dynatron.me/x/stillbox/pkg/pb" + + tgs "dynatron.me/x/stillbox/pkg/talkgroups" ) type TalkgroupFilter struct { - Talkgroups []Talkgroup `json:"talkgroups,omitempty"` - TalkgroupsNot []Talkgroup `json:"talkgroupsNot,omitempty"` + Talkgroups []tgs.Talkgroup `json:"talkgroups,omitempty"` + TalkgroupsNot []tgs.Talkgroup `json:"talkgroupsNot,omitempty"` TalkgroupTagsAll []string `json:"talkgroupTagsAll,omitempty"` TalkgroupTagsAny []string `json:"talkgroupTagsAny,omitempty"` TalkgroupTagsNot []string `json:"talkgroupTagsNot,omitempty"` - talkgroups map[Talkgroup]bool + talkgroups map[tgs.Talkgroup]bool } func TalkgroupFilterFromPB(ctx context.Context, p *pb.Filter) (*TalkgroupFilter, error) { @@ -25,9 +27,9 @@ func TalkgroupFilterFromPB(ctx context.Context, p *pb.Filter) (*TalkgroupFilter, } if l := len(p.Talkgroups); l > 0 { - tgf.Talkgroups = make([]Talkgroup, l) + tgf.Talkgroups = make([]tgs.Talkgroup, l) for i, t := range p.Talkgroups { - tgf.Talkgroups[i] = Talkgroup{ + tgf.Talkgroups[i] = tgs.Talkgroup{ System: uint32(t.System), Talkgroup: uint32(t.Talkgroup), } @@ -35,9 +37,9 @@ func TalkgroupFilterFromPB(ctx context.Context, p *pb.Filter) (*TalkgroupFilter, } if l := len(p.TalkgroupsNot); l > 0 { - tgf.TalkgroupsNot = make([]Talkgroup, l) + tgf.TalkgroupsNot = make([]tgs.Talkgroup, l) for i, t := range p.TalkgroupsNot { - tgf.TalkgroupsNot[i] = Talkgroup{ + tgf.TalkgroupsNot[i] = tgs.Talkgroup{ System: uint32(t.System), Talkgroup: uint32(t.Talkgroup), } @@ -51,12 +53,12 @@ func (f *TalkgroupFilter) hasTags() bool { return len(f.TalkgroupTagsAny) > 0 || len(f.TalkgroupTagsAll) > 0 || len(f.TalkgroupTagsNot) > 0 } -func (f *TalkgroupFilter) GetFinalTalkgroups() map[Talkgroup]bool { +func (f *TalkgroupFilter) GetFinalTalkgroups() map[tgs.Talkgroup]bool { return f.talkgroups } func (f *TalkgroupFilter) compile(ctx context.Context) error { - f.talkgroups = make(map[Talkgroup]bool) + f.talkgroups = make(map[tgs.Talkgroup]bool) for _, tg := range f.Talkgroups { f.talkgroups[tg] = true } @@ -69,7 +71,7 @@ func (f *TalkgroupFilter) compile(ctx context.Context) error { } for _, tg := range tagTGs { - f.talkgroups[Talkgroup{System: uint32(tg.SystemID), Talkgroup: uint32(tg.Tgid)}] = true + f.talkgroups[tgs.Talkgroup{System: uint32(tg.SystemID), Talkgroup: uint32(tg.Tgid)}] = true } } diff --git a/pkg/calls/alertconfig.go b/pkg/talkgroups/alertconfig.go similarity index 98% rename from pkg/calls/alertconfig.go rename to pkg/talkgroups/alertconfig.go index 0a8b492..73365d6 100644 --- a/pkg/calls/alertconfig.go +++ b/pkg/talkgroups/alertconfig.go @@ -1,4 +1,4 @@ -package calls +package talkgroups import ( "encoding/json" diff --git a/pkg/calls/alertconfig_test.go b/pkg/talkgroups/alertconfig_test.go similarity index 83% rename from pkg/calls/alertconfig_test.go rename to pkg/talkgroups/alertconfig_test.go index fe141d9..785895b 100644 --- a/pkg/calls/alertconfig_test.go +++ b/pkg/talkgroups/alertconfig_test.go @@ -1,4 +1,4 @@ -package calls_test +package talkgroups_test import ( "errors" @@ -8,26 +8,26 @@ import ( "dynatron.me/x/stillbox/internal/ruletime" "dynatron.me/x/stillbox/internal/trending" - "dynatron.me/x/stillbox/pkg/calls" + "dynatron.me/x/stillbox/pkg/talkgroups" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func TestAlertConfig(t *testing.T) { - ac := make(calls.AlertConfig) + ac := make(talkgroups.AlertConfig) parseTests := []struct { name string - tg calls.Talkgroup + tg talkgroups.Talkgroup conf string - compare []calls.AlertRule + compare []talkgroups.AlertRule expectErr error }{ { name: "base case", - tg: calls.TG(197, 3), + tg: talkgroups.TG(197, 3), conf: `[{"times":["7:00+2h","01:00+1h","16:00+1h","19:00+4h"],"mult":0.2},{"times":["11:00+1h","15:00+30m","16:03+20m"],"mult":2.0}]`, - compare: []calls.AlertRule{ + compare: []talkgroups.AlertRule{ { Times: []ruletime.RuleTime{ ruletime.Must(ruletime.New("7:00+2h")), @@ -49,7 +49,7 @@ func TestAlertConfig(t *testing.T) { }, { name: "bad spec", - tg: calls.TG(197, 3), + tg: talkgroups.TG(197, 3), conf: `[{"times":["26:00+2h","01:00+1h","19:00+4h"],"mult":0.2},{"times":["11:00+1h","15:00+30m"],"mult":2.0}]`, expectErr: errors.New("'26:00+2h': invalid hours"), }, @@ -78,42 +78,42 @@ func TestAlertConfig(t *testing.T) { evalTests := []struct { name string - tg calls.Talkgroup + tg talkgroups.Talkgroup t time.Time origScore float64 expectScore float64 }{ { name: "base eval", - tg: calls.TG(197, 3), + tg: talkgroups.TG(197, 3), t: tMust("1:20"), origScore: 3, expectScore: 0.6, }, { name: "base eval", - tg: calls.TG(197, 3), + tg: talkgroups.TG(197, 3), t: tMust("23:03"), origScore: 3, expectScore: 3, }, { name: "base eval", - tg: calls.TG(197, 3), + tg: talkgroups.TG(197, 3), t: tMust("8:03"), origScore: 1.0, expectScore: 0.2, }, { name: "base eval", - tg: calls.TG(197, 3), + tg: talkgroups.TG(197, 3), t: tMust("15:15"), origScore: 3.0, expectScore: 6.0, }, { name: "overlapping eval", - tg: calls.TG(197, 3), + tg: talkgroups.TG(197, 3), t: tMust("16:10"), origScore: 1.0, expectScore: 0.4, @@ -122,7 +122,7 @@ func TestAlertConfig(t *testing.T) { for _, tc := range evalTests { t.Run(tc.name, func(t *testing.T) { - cs := trending.Score[calls.Talkgroup]{ + cs := trending.Score[talkgroups.Talkgroup]{ ID: tc.tg, Score: tc.origScore, } diff --git a/pkg/calls/talkgroups.go b/pkg/talkgroups/talkgroups.go similarity index 96% rename from pkg/calls/talkgroups.go rename to pkg/talkgroups/talkgroups.go index 6d554b7..a537ce9 100644 --- a/pkg/calls/talkgroups.go +++ b/pkg/talkgroups/talkgroups.go @@ -1,4 +1,4 @@ -package calls +package talkgroups import ( "context" @@ -20,10 +20,6 @@ type Talkgroup struct { Talkgroup uint32 } -func (c *Call) TalkgroupTuple() Talkgroup { - return Talkgroup{System: uint32(c.System), Talkgroup: uint32(c.Talkgroup)} -} - func TG[T int | uint | int64 | uint64 | int32 | uint32](sys, tgid T) Talkgroup { return Talkgroup{ System: uint32(sys),