From 05eccf588ba87f3b48c422cb8fad5f3b9b951bfe Mon Sep 17 00:00:00 2001 From: Daniel Ponte Date: Fri, 15 Nov 2024 08:46:29 -0500 Subject: [PATCH] wip --- .gitignore | 2 +- pkg/alerting/stats.go | 4 +- pkg/database/database.go | 14 +++++++ pkg/database/extend.go | 6 +-- pkg/database/talkgroups.manual.sql.go | 59 +++++++++++++++++---------- pkg/talkgroups/cache.go | 2 +- pkg/talkgroups/talkgroup.go | 2 +- 7 files changed, 59 insertions(+), 30 deletions(-) diff --git a/.gitignore b/.gitignore index cb1321f..8e9a60e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,6 @@ config.yaml config.test.yaml -mydb.sql +/*.sql client/calls/ !client/calls/.gitkeep /gordio diff --git a/pkg/alerting/stats.go b/pkg/alerting/stats.go index 6ba16dd..5dea62e 100644 --- a/pkg/alerting/stats.go +++ b/pkg/alerting/stats.go @@ -47,13 +47,13 @@ func (as *alerter) tgStatsHandler(w http.ResponseWriter, r *http.Request) { return } - tgMap := make(map[talkgroups.ID]database.GetTalkgroupsWithLearnedByPackedIDsRow, len(tgs)) + tgMap := make(map[talkgroups.ID]database.GetTalkgroupsRow, len(tgs)) for _, t := range tgs { tgMap[talkgroups.ID{System: uint32(t.System.ID), Talkgroup: uint32(t.Talkgroup.Tgid)}] = t } renderData := struct { - TGs map[talkgroups.ID]database.GetTalkgroupsWithLearnedByPackedIDsRow + TGs map[talkgroups.ID]database.GetTalkgroupsRow Scores trending.Scores[talkgroups.ID] LastScore time.Time Simulation *Simulation diff --git a/pkg/database/database.go b/pkg/database/database.go index 9f1c69b..ebc5a6f 100644 --- a/pkg/database/database.go +++ b/pkg/database/database.go @@ -11,6 +11,8 @@ import ( _ "github.com/golang-migrate/migrate/v4/database/pgx/v5" "github.com/golang-migrate/migrate/v4/source/iofs" "github.com/jackc/pgx/v5/pgxpool" + "github.com/jackc/pgx/v5/tracelog" + "github.com/rs/zerolog/log" ) // DB is a database handle. @@ -19,6 +21,12 @@ type DB struct { *Queries } +type myLogger struct{} + +func (m myLogger) Log(ctx context.Context, level tracelog.LogLevel, msg string, data map[string]any) { + log.Debug().Fields(data).Msg(msg) +} + // NewClient creates a new DB using the provided config. func NewClient(ctx context.Context, conf config.DB) (*DB, error) { dir, err := iofs.New(sqlembed.Migrations, "postgres/migrations") @@ -43,6 +51,12 @@ func NewClient(ctx context.Context, conf config.DB) (*DB, error) { return nil, err } + logger := myLogger{} + tracer := &tracelog.TraceLog{ + Logger: logger, + LogLevel: tracelog.LogLevelTrace, + } + pgConf.ConnConfig.Tracer = tracer pool, err := pgxpool.NewWithConfig(ctx, pgConf) if err != nil { return nil, err diff --git a/pkg/database/extend.go b/pkg/database/extend.go index 5f165a0..61619d4 100644 --- a/pkg/database/extend.go +++ b/pkg/database/extend.go @@ -1,8 +1,8 @@ package database -func (d GetTalkgroupsWithLearnedByPackedIDsRow) GetTalkgroup() Talkgroup { return d.Talkgroup } -func (d GetTalkgroupsWithLearnedByPackedIDsRow) GetSystem() System { return d.System } -func (d GetTalkgroupsWithLearnedByPackedIDsRow) GetLearned() bool { return d.Learned } +func (d GetTalkgroupsRow) GetTalkgroup() Talkgroup { return d.Talkgroup } +func (d GetTalkgroupsRow) GetSystem() System { return d.System } +func (d GetTalkgroupsRow) GetLearned() bool { return d.Learned } func (g GetTalkgroupsWithLearnedRow) GetTalkgroup() Talkgroup { return g.Talkgroup } func (g GetTalkgroupsWithLearnedRow) GetSystem() System { return g.System } func (g GetTalkgroupsWithLearnedRow) GetLearned() bool { return g.Learned } diff --git a/pkg/database/talkgroups.manual.sql.go b/pkg/database/talkgroups.manual.sql.go index c141e23..debebe5 100644 --- a/pkg/database/talkgroups.manual.sql.go +++ b/pkg/database/talkgroups.manual.sql.go @@ -3,24 +3,45 @@ package database import ( "context" "database/sql/driver" + "fmt" + + "github.com/jackc/pgx/v5/pgtype" ) type TalkgroupT struct { - System uint32 `json:"sys"` - Talkgroup uint32 `json:"tg"` + System uint32 `json:"system_id"` + Talkgroup uint32 `json:"tgid"` +} + +type TalkgroupTs []TalkgroupT + +func (t TalkgroupTs) Nest() (sys []uint32, tg []uint32) { + sys = make([]uint32, len(t)) + tg = make([]uint32, len(t)) + + for i := range t { + sys[i] = t[i].System + tg[i] = t[i].Talkgroup + } + + return } func (t TalkgroupT) Value() (driver.Value, error) { return [2]uint32{t.System, t.Talkgroup}, nil } +func (t TalkgroupT) TextValue() (pgtype.Text, error) { + return pgtype.Text{String: fmt.Sprintf("%d:%d", t.System, t.Talkgroup)}, nil +} + const getTalkgroupsWithLearnedByPackedIDs = `-- name: GetTalkgroupsWithLearnedByPackedIDs :many SELECT tg.id, tg.system_id, tg.tgid, tg.name, tg.alpha_tag, tg.tg_group, tg.frequency, tg.metadata, tg.tags, tg.alert, tg.alert_config, tg.weight, sys.id, sys.name, FALSE learned FROM talkgroups tg JOIN systems sys ON tg.system_id = sys.id -WHERE (tg.system_id, tg.tgid) = ANY($1) +JOIN UNNEST($1::INT4[], $2::INT4[]) AS tgt(sys, tg) ON (tg.system_id = tgt.sys AND tg.tgid = tgt.tg) UNION SELECT NULL::UUID, tgl.system_id::INT4, tgl.tgid::INT4, tgl.name, @@ -30,24 +51,24 @@ TRUE, NULL::JSONB, 1.0, sys.id, sys.name, TRUE learned FROM talkgroups_learned tgl JOIN systems sys ON tgl.system_id = sys.id -WHERE (tgl.system_id, tgl.tgid) = ANY($1); -` +JOIN UNNEST($1::INT4[], $2::INT4[]) AS tgt(sys, tg) ON (tgl.system_id = tgt.sys AND tgl.tgid = tgt.tg);` -type GetTalkgroupsWithLearnedByPackedIDsRow struct { +type GetTalkgroupsRow struct { Talkgroup Talkgroup `json:"talkgroup"` System System `json:"system"` Learned bool `json:"learned"` } -func (q *Queries) GetTalkgroupsWithLearnedByPackedIDs(ctx context.Context, ids []TalkgroupT) ([]GetTalkgroupsWithLearnedByPackedIDsRow, error) { - rows, err := q.db.Query(ctx, getTalkgroupsWithLearnedByPackedIDs, ids) +func (q *Queries) GetTalkgroupsWithLearnedByPackedIDs(ctx context.Context, ids TalkgroupTs) ([]GetTalkgroupsRow, error) { + sysAr, tgAr := ids.Nest() + rows, err := q.db.Query(ctx, getTalkgroupsWithLearnedByPackedIDs, sysAr, tgAr) if err != nil { return nil, err } defer rows.Close() - var items []GetTalkgroupsWithLearnedByPackedIDsRow + var items []GetTalkgroupsRow for rows.Next() { - var i GetTalkgroupsWithLearnedByPackedIDsRow + var i GetTalkgroupsRow if err := rows.Scan( &i.Talkgroup.ID, &i.Talkgroup.SystemID, @@ -78,23 +99,19 @@ func (q *Queries) GetTalkgroupsWithLearnedByPackedIDs(ctx context.Context, ids [ const getTalkgroupsByPackedIDs = `-- name: GetTalkgroupsByPackedIDs :many SELECT tg.id, tg.system_id, tg.tgid, tg.name, tg.alpha_tag, tg.tg_group, tg.frequency, tg.metadata, tg.tags, tg.alert, tg.alert_config, tg.weight, sys.id, sys.name FROM talkgroups tg JOIN systems sys ON tg.system_id = sys.id -WHERE (tg.system_id, tg.tgid) = ANY($1) +JOIN UNNEST($1::INT4[], $2::INT4[]) AS tgt(sys, tg) ON (tg.system_id = tgt.sys AND tg.tgid = tgt.tg) ` -type GetTalkgroupsByPackedIDsRow struct { - Talkgroup Talkgroup `json:"talkgroup"` - System System `json:"system"` -} - -func (q *Queries) GetTalkgroupsByPackedIDs(ctx context.Context, idtuple []TalkgroupT) ([]GetTalkgroupsByPackedIDsRow, error) { - rows, err := q.db.Query(ctx, getTalkgroupsByPackedIDs, idtuple) +func (q *Queries) GetTalkgroupsByPackedIDs(ctx context.Context, ids TalkgroupTs) ([]GetTalkgroupsRow, error) { + sysAr, tgAr := ids.Nest() + rows, err := q.db.Query(ctx, getTalkgroupsByPackedIDs, sysAr, tgAr) if err != nil { return nil, err } defer rows.Close() - var items []GetTalkgroupsByPackedIDsRow + var items []GetTalkgroupsRow for rows.Next() { - var i GetTalkgroupsByPackedIDsRow + var i GetTalkgroupsRow if err := rows.Scan( &i.Talkgroup.ID, &i.Talkgroup.SystemID, @@ -120,5 +137,3 @@ func (q *Queries) GetTalkgroupsByPackedIDs(ctx context.Context, idtuple []Talkgr } return items, nil } - - diff --git a/pkg/talkgroups/cache.go b/pkg/talkgroups/cache.go index 18f30b6..0e86b2c 100644 --- a/pkg/talkgroups/cache.go +++ b/pkg/talkgroups/cache.go @@ -135,7 +135,7 @@ func (t *cache) add(rec *Talkgroup) error { } type row interface { - database.GetTalkgroupsWithLearnedByPackedIDsRow | database.GetTalkgroupsWithLearnedRow | + database.GetTalkgroupsRow | database.GetTalkgroupsWithLearnedRow | database.GetTalkgroupsWithLearnedBySystemRow GetTalkgroup() database.Talkgroup GetSystem() database.System diff --git a/pkg/talkgroups/talkgroup.go b/pkg/talkgroups/talkgroup.go index 2e5b269..5849ebb 100644 --- a/pkg/talkgroups/talkgroup.go +++ b/pkg/talkgroups/talkgroup.go @@ -48,7 +48,7 @@ func TG[T intId, U intId](sys T, tgid U) ID { func (t ID) Tuple() database.TalkgroupT { return database.TalkgroupT{ - System: t.System, + System: t.System, Talkgroup: t.Talkgroup, } }