From c48d1eaf8d6071694ca1a44b35eed83a9f599e4a Mon Sep 17 00:00:00 2001 From: Daniel Ponte Date: Wed, 20 Nov 2024 20:13:18 -0500 Subject: [PATCH] Put LearnTG in the store where it belongs --- pkg/calls/call.go | 17 ----------------- pkg/server/server.go | 2 +- pkg/sinks/database.go | 8 +++++--- pkg/talkgroups/tgstore/store.go | 21 +++++++++++++++++++++ 4 files changed, 27 insertions(+), 21 deletions(-) diff --git a/pkg/calls/call.go b/pkg/calls/call.go index d76b530..6359940 100644 --- a/pkg/calls/call.go +++ b/pkg/calls/call.go @@ -1,13 +1,11 @@ package calls import ( - "context" "fmt" "time" "dynatron.me/x/stillbox/internal/audio" "dynatron.me/x/stillbox/pkg/auth" - "dynatron.me/x/stillbox/pkg/database" "dynatron.me/x/stillbox/pkg/pb" "dynatron.me/x/stillbox/pkg/talkgroups" @@ -113,21 +111,6 @@ func (c *Call) ToPB() *pb.Call { } } -func (c *Call) LearnTG(ctx context.Context, db database.Store) (learnedId int, err error) { - err = db.AddTalkgroupWithLearnedFlag(ctx, int32(c.System), int32(c.Talkgroup)) - if err != nil { - return 0, fmt.Errorf("addTalkgroupWithLearnedFlag: %w", err) - } - - return db.AddLearnedTalkgroup(ctx, database.AddLearnedTalkgroupParams{ - SystemID: c.System, - TGID: c.Talkgroup, - Name: c.TalkgroupLabel, - AlphaTag: c.TGAlphaTag, - TGGroup: c.TalkgroupGroup, - }) -} - func (c *Call) computeLength() (err error) { var td time.Duration diff --git a/pkg/server/server.go b/pkg/server/server.go index 3132e01..b14778f 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -79,7 +79,7 @@ func New(ctx context.Context, cfg *config.Config) (*Server, error) { rest: api, } - srv.sinks.Register("database", sinks.NewDatabaseSink(srv.db), true) + srv.sinks.Register("database", sinks.NewDatabaseSink(srv.db, tgCache), true) srv.sinks.Register("nexus", sinks.NewNexusSink(srv.nex), false) if srv.alerter.Enabled() { diff --git a/pkg/sinks/database.go b/pkg/sinks/database.go index 9ced102..190fa9d 100644 --- a/pkg/sinks/database.go +++ b/pkg/sinks/database.go @@ -7,6 +7,7 @@ import ( "dynatron.me/x/stillbox/internal/common" "dynatron.me/x/stillbox/pkg/calls" "dynatron.me/x/stillbox/pkg/database" + "dynatron.me/x/stillbox/pkg/talkgroups/tgstore" "github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5/pgtype" @@ -15,10 +16,11 @@ import ( type DatabaseSink struct { db database.Store + tgs tgstore.Store } -func NewDatabaseSink(store database.Store) *DatabaseSink { - return &DatabaseSink{store} +func NewDatabaseSink(store database.Store, tgs tgstore.Store) *DatabaseSink { + return &DatabaseSink{store, tgs} } func (s *DatabaseSink) Call(ctx context.Context, call *calls.Call) error { @@ -43,7 +45,7 @@ func (s *DatabaseSink) Call(ctx context.Context, call *calls.Call) error { if err != nil && database.IsTGConstraintViolation(err) { return s.db.InTx(ctx, func(tx database.Store) error { - _, err := call.LearnTG(ctx, tx) + _, err := s.tgs.LearnTG(ctx, call) if err != nil { return fmt.Errorf("add call: learn tg: %w", err) } diff --git a/pkg/talkgroups/tgstore/store.go b/pkg/talkgroups/tgstore/store.go index 0dc8d30..d8e6ad1 100644 --- a/pkg/talkgroups/tgstore/store.go +++ b/pkg/talkgroups/tgstore/store.go @@ -3,6 +3,7 @@ package tgstore import ( "context" "errors" + "fmt" "strings" "sync" "time" @@ -11,6 +12,7 @@ import ( "dynatron.me/x/stillbox/pkg/auth" "dynatron.me/x/stillbox/pkg/config" "dynatron.me/x/stillbox/pkg/database" + "dynatron.me/x/stillbox/pkg/calls" tgsp "dynatron.me/x/stillbox/pkg/talkgroups" "github.com/jackc/pgx/v5" @@ -37,6 +39,9 @@ type Store interface { // TGs retrieves many talkgroups from the Store. TGs(ctx context.Context, tgs tgsp.IDs) ([]*tgsp.Talkgroup, error) + // LearnTG learns the talkgroup from a Call. + LearnTG(ctx context.Context, call *calls.Call) (learnedId int, err error) + // SystemTGs retrieves all Talkgroups associated with a System. SystemTGs(ctx context.Context, systemID int32) ([]*tgsp.Talkgroup, error) @@ -300,6 +305,22 @@ func (t *cache) UpdateTG(ctx context.Context, input database.UpdateTalkgroupPara return record, nil } +func (t *cache) LearnTG(ctx context.Context, c *calls.Call) (learnedId int, err error) { + db := database.FromCtx(ctx) + err = db.AddTalkgroupWithLearnedFlag(ctx, int32(c.System), int32(c.Talkgroup)) + if err != nil { + return 0, fmt.Errorf("addTalkgroupWithLearnedFlag: %w", err) + } + + return db.AddLearnedTalkgroup(ctx, database.AddLearnedTalkgroupParams{ + SystemID: c.System, + TGID: c.Talkgroup, + Name: c.TalkgroupLabel, + AlphaTag: c.TGAlphaTag, + TGGroup: c.TalkgroupGroup, + }) +} + func (t *cache) UpsertTGs(ctx context.Context, system int, input []database.UpsertTalkgroupParams) ([]*tgsp.Talkgroup, error) { db := database.FromCtx(ctx) sysName, hasSys := t.SystemName(ctx, system)