FKEY constraint prevents alerts on learned talkgroups

This commit is contained in:
Daniel 2024-11-12 10:14:11 -05:00
parent f44ef2ec47
commit d592e20d37
5 changed files with 16 additions and 13 deletions

View file

@ -36,7 +36,8 @@ func (a *Alert) ToAddAlertParams() database.AddAlertParams {
return database.AddAlertParams{ return database.AddAlertParams{
ID: a.ID, ID: a.ID,
Time: pgtype.Timestamptz{Time: a.Timestamp, Valid: true}, 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, Weight: &a.Weight,
Score: &f32score, Score: &f32score,
OrigScore: origScore, OrigScore: origScore,

View file

@ -13,7 +13,7 @@ import (
) )
const addAlert = `-- name: AddAlert :exec 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 VALUES
( (
$1, $1,
@ -23,14 +23,16 @@ VALUES
$5, $5,
$6, $6,
$7, $7,
$8 $8,
$9
) )
` `
type AddAlertParams struct { type AddAlertParams struct {
ID uuid.UUID `json:"id"` ID uuid.UUID `json:"id"`
Time pgtype.Timestamptz `json:"time"` Time pgtype.Timestamptz `json:"time"`
PackedTg int64 `json:"packed_tg"` Tgid int `json:"tgid"`
SystemID int `json:"system_id"`
Weight *float32 `json:"weight"` Weight *float32 `json:"weight"`
Score *float32 `json:"score"` Score *float32 `json:"score"`
OrigScore *float32 `json:"orig_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, _, err := q.db.Exec(ctx, addAlert,
arg.ID, arg.ID,
arg.Time, arg.Time,
arg.PackedTg, arg.Tgid,
arg.SystemID,
arg.Weight, arg.Weight,
arg.Score, arg.Score,
arg.OrigScore, arg.OrigScore,

View file

@ -15,9 +15,8 @@ import (
type Alert struct { type Alert struct {
ID uuid.UUID `json:"id"` ID uuid.UUID `json:"id"`
Time pgtype.Timestamptz `json:"time"` Time pgtype.Timestamptz `json:"time"`
Talkgroup int64 `json:"talkgroup"` Tgid int `json:"tgid"`
SystemID int32 `json:"system_id"` SystemID int `json:"system_id"`
Tgid int32 `json:"tgid"`
Weight *float32 `json:"weight"` Weight *float32 `json:"weight"`
Score *float32 `json:"score"` Score *float32 `json:"score"`
OrigScore *float32 `json:"orig_score"` OrigScore *float32 `json:"orig_score"`

View file

@ -74,9 +74,8 @@ CREATE TABLE IF NOT EXISTS talkgroups_learned(
CREATE TABLE IF NOT EXISTS alerts( CREATE TABLE IF NOT EXISTS alerts(
id UUID PRIMARY KEY, id UUID PRIMARY KEY,
time TIMESTAMPTZ NOT NULL, time TIMESTAMPTZ NOT NULL,
talkgroup INT8 REFERENCES talkgroups(id) NOT NULL, tgid INTEGER NOT NULL,
system_id INT4 REFERENCES systems(id) NOT NULL GENERATED ALWAYS AS (talkgroup >> 32) STORED, system_id INTEGER REFERENCES systems(id) NOT NULL,
tgid INT4 NOT NULL GENERATED ALWAYS AS (talkgroup & x'ffffffff'::BIGINT) STORED,
weight REAL, weight REAL,
score REAL, score REAL,
orig_score REAL, orig_score REAL,

View file

@ -41,12 +41,13 @@ source
UPDATE calls SET transcript = $2 WHERE id = $1; UPDATE calls SET transcript = $2 WHERE id = $1;
-- name: AddAlert :exec -- 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 VALUES
( (
sqlc.arg(id), sqlc.arg(id),
sqlc.arg(time), sqlc.arg(time),
sqlc.arg(packed_tg), sqlc.arg(tgid),
sqlc.arg(system_id),
sqlc.arg(weight), sqlc.arg(weight),
sqlc.arg(score), sqlc.arg(score),
sqlc.arg(orig_score), sqlc.arg(orig_score),