From d592e20d3794d3c2b6f969243bfb3786258a70d2 Mon Sep 17 00:00:00 2001 From: Daniel Ponte Date: Tue, 12 Nov 2024 10:14:11 -0500 Subject: [PATCH] FKEY constraint prevents alerts on learned talkgroups --- pkg/alerting/alert/alert.go | 3 ++- pkg/database/calls.sql.go | 11 +++++++---- pkg/database/models.go | 5 ++--- sql/postgres/migrations/001_initial.up.sql | 5 ++--- sql/postgres/queries/calls.sql | 5 +++-- 5 files changed, 16 insertions(+), 13 deletions(-) diff --git a/pkg/alerting/alert/alert.go b/pkg/alerting/alert/alert.go index 6f3ad84..9b83006 100644 --- a/pkg/alerting/alert/alert.go +++ b/pkg/alerting/alert/alert.go @@ -36,7 +36,8 @@ func (a *Alert) ToAddAlertParams() database.AddAlertParams { return database.AddAlertParams{ ID: a.ID, Time: pgtype.Timestamptz{Time: a.Timestamp, Valid: true}, - PackedTg: a.Score.ID.Pack(), + SystemID: int(a.Score.ID.System), + Tgid: int(a.Score.ID.Talkgroup), Weight: &a.Weight, Score: &f32score, OrigScore: origScore, diff --git a/pkg/database/calls.sql.go b/pkg/database/calls.sql.go index cfed6e7..fc56bd0 100644 --- a/pkg/database/calls.sql.go +++ b/pkg/database/calls.sql.go @@ -13,7 +13,7 @@ import ( ) const addAlert = `-- name: AddAlert :exec -INSERT INTO alerts (id, time, talkgroup, weight, score, orig_score, notified, metadata) +INSERT INTO alerts (id, time, tgid, system_id, weight, score, orig_score, notified, metadata) VALUES ( $1, @@ -23,14 +23,16 @@ VALUES $5, $6, $7, - $8 + $8, + $9 ) ` type AddAlertParams struct { ID uuid.UUID `json:"id"` Time pgtype.Timestamptz `json:"time"` - PackedTg int64 `json:"packed_tg"` + Tgid int `json:"tgid"` + SystemID int `json:"system_id"` Weight *float32 `json:"weight"` Score *float32 `json:"score"` OrigScore *float32 `json:"orig_score"` @@ -42,7 +44,8 @@ func (q *Queries) AddAlert(ctx context.Context, arg AddAlertParams) error { _, err := q.db.Exec(ctx, addAlert, arg.ID, arg.Time, - arg.PackedTg, + arg.Tgid, + arg.SystemID, arg.Weight, arg.Score, arg.OrigScore, diff --git a/pkg/database/models.go b/pkg/database/models.go index 9e3eb2a..4d74489 100644 --- a/pkg/database/models.go +++ b/pkg/database/models.go @@ -15,9 +15,8 @@ import ( type Alert struct { ID uuid.UUID `json:"id"` Time pgtype.Timestamptz `json:"time"` - Talkgroup int64 `json:"talkgroup"` - SystemID int32 `json:"system_id"` - Tgid int32 `json:"tgid"` + Tgid int `json:"tgid"` + SystemID int `json:"system_id"` Weight *float32 `json:"weight"` Score *float32 `json:"score"` OrigScore *float32 `json:"orig_score"` diff --git a/sql/postgres/migrations/001_initial.up.sql b/sql/postgres/migrations/001_initial.up.sql index b9159e6..4706fe9 100644 --- a/sql/postgres/migrations/001_initial.up.sql +++ b/sql/postgres/migrations/001_initial.up.sql @@ -74,9 +74,8 @@ CREATE TABLE IF NOT EXISTS talkgroups_learned( CREATE TABLE IF NOT EXISTS alerts( id UUID PRIMARY KEY, time TIMESTAMPTZ NOT NULL, - talkgroup INT8 REFERENCES talkgroups(id) NOT NULL, - system_id INT4 REFERENCES systems(id) NOT NULL GENERATED ALWAYS AS (talkgroup >> 32) STORED, - tgid INT4 NOT NULL GENERATED ALWAYS AS (talkgroup & x'ffffffff'::BIGINT) STORED, + tgid INTEGER NOT NULL, + system_id INTEGER REFERENCES systems(id) NOT NULL, weight REAL, score REAL, orig_score REAL, diff --git a/sql/postgres/queries/calls.sql b/sql/postgres/queries/calls.sql index 7781ee3..1cb09fe 100644 --- a/sql/postgres/queries/calls.sql +++ b/sql/postgres/queries/calls.sql @@ -41,12 +41,13 @@ source UPDATE calls SET transcript = $2 WHERE id = $1; -- name: AddAlert :exec -INSERT INTO alerts (id, time, talkgroup, weight, score, orig_score, notified, metadata) +INSERT INTO alerts (id, time, tgid, system_id, weight, score, orig_score, notified, metadata) VALUES ( sqlc.arg(id), sqlc.arg(time), - sqlc.arg(packed_tg), + sqlc.arg(tgid), + sqlc.arg(system_id), sqlc.arg(weight), sqlc.arg(score), sqlc.arg(orig_score),