From 673bddc4df6763a21d84fd810c18e1835107de6f Mon Sep 17 00:00:00 2001 From: Daniel Ponte Date: Fri, 25 Oct 2024 16:02:08 -0400 Subject: [PATCH] Model changes --- pkg/gordio/database/models.go | 13 +++++++++++++ pkg/gordio/database/talkgroups.sql.go | 18 ++++++++++++++---- sql/postgres/migrations/001_initial.up.sql | 17 ++++++++++++++--- 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/pkg/gordio/database/models.go b/pkg/gordio/database/models.go index 25df383..9ebe7b7 100644 --- a/pkg/gordio/database/models.go +++ b/pkg/gordio/database/models.go @@ -11,6 +11,17 @@ import ( "github.com/jackc/pgx/v5/pgtype" ) +type Alert struct { + ID int32 `json:"id"` + Time pgtype.Timestamptz `json:"time"` + Talkgroup int64 `json:"talkgroup"` + SystemID int32 `json:"system_id"` + Tgid int32 `json:"tgid"` + Weight *float32 `json:"weight"` + Score *float32 `json:"score"` + Metadata []byte `json:"metadata"` +} + type ApiKey struct { ID int32 `json:"id"` Owner int `json:"owner"` @@ -78,6 +89,8 @@ type Talkgroup struct { Frequency *int32 `json:"frequency"` Metadata []byte `json:"metadata"` Tags []string `json:"tags"` + Notify bool `json:"notify"` + Weight float32 `json:"weight"` } type TalkgroupsLearned struct { diff --git a/pkg/gordio/database/talkgroups.sql.go b/pkg/gordio/database/talkgroups.sql.go index 6ff6b1e..c5b3fa4 100644 --- a/pkg/gordio/database/talkgroups.sql.go +++ b/pkg/gordio/database/talkgroups.sql.go @@ -20,7 +20,7 @@ func (q *Queries) BulkSetTalkgroupTags(ctx context.Context, iD int64, tags []str } const getTalkgroup = `-- name: GetTalkgroup :one -SELECT id, system_id, tgid, name, alpha_tag, tg_group, frequency, metadata, tags FROM talkgroups +SELECT id, system_id, tgid, name, alpha_tag, tg_group, frequency, metadata, tags, notify, weight FROM talkgroups WHERE id = systg2id($1, $2) ` @@ -37,6 +37,8 @@ func (q *Queries) GetTalkgroup(ctx context.Context, systemID int, tgid int) (Tal &i.Frequency, &i.Metadata, &i.Tags, + &i.Notify, + &i.Weight, ) return i, err } @@ -138,7 +140,7 @@ func (q *Queries) GetTalkgroupWithLearned(ctx context.Context, systemID int, tgi } const getTalkgroupsByPackedIDs = `-- name: GetTalkgroupsByPackedIDs :many -SELECT tg.id, system_id, tgid, tg.name, alpha_tag, tg_group, frequency, metadata, tags, sys.id, sys.name FROM talkgroups tg +SELECT tg.id, system_id, tgid, tg.name, alpha_tag, tg_group, frequency, metadata, tags, notify, weight, sys.id, sys.name FROM talkgroups tg JOIN systems sys ON tg.system_id = sys.id WHERE tg.id = ANY($1::INT8[]) ` @@ -153,6 +155,8 @@ type GetTalkgroupsByPackedIDsRow struct { Frequency *int32 `json:"frequency"` Metadata []byte `json:"metadata"` Tags []string `json:"tags"` + Notify bool `json:"notify"` + Weight float32 `json:"weight"` ID_2 int `json:"id_2"` Name_2 string `json:"name_2"` } @@ -176,6 +180,8 @@ func (q *Queries) GetTalkgroupsByPackedIDs(ctx context.Context, dollar_1 []int64 &i.Frequency, &i.Metadata, &i.Tags, + &i.Notify, + &i.Weight, &i.ID_2, &i.Name_2, ); err != nil { @@ -190,7 +196,7 @@ func (q *Queries) GetTalkgroupsByPackedIDs(ctx context.Context, dollar_1 []int64 } const getTalkgroupsWithAllTags = `-- name: GetTalkgroupsWithAllTags :many -SELECT id, system_id, tgid, name, alpha_tag, tg_group, frequency, metadata, tags FROM talkgroups +SELECT id, system_id, tgid, name, alpha_tag, tg_group, frequency, metadata, tags, notify, weight FROM talkgroups WHERE tags && ARRAY[$1] ` @@ -213,6 +219,8 @@ func (q *Queries) GetTalkgroupsWithAllTags(ctx context.Context, tags []string) ( &i.Frequency, &i.Metadata, &i.Tags, + &i.Notify, + &i.Weight, ); err != nil { return nil, err } @@ -225,7 +233,7 @@ func (q *Queries) GetTalkgroupsWithAllTags(ctx context.Context, tags []string) ( } const getTalkgroupsWithAnyTags = `-- name: GetTalkgroupsWithAnyTags :many -SELECT id, system_id, tgid, name, alpha_tag, tg_group, frequency, metadata, tags FROM talkgroups +SELECT id, system_id, tgid, name, alpha_tag, tg_group, frequency, metadata, tags, notify, weight FROM talkgroups WHERE tags @> ARRAY[$1] ` @@ -248,6 +256,8 @@ func (q *Queries) GetTalkgroupsWithAnyTags(ctx context.Context, tags []string) ( &i.Frequency, &i.Metadata, &i.Tags, + &i.Notify, + &i.Weight, ); err != nil { return nil, err } diff --git a/sql/postgres/migrations/001_initial.up.sql b/sql/postgres/migrations/001_initial.up.sql index 6fb7874..efbc463 100644 --- a/sql/postgres/migrations/001_initial.up.sql +++ b/sql/postgres/migrations/001_initial.up.sql @@ -53,13 +53,13 @@ CREATE TABLE IF NOT EXISTS talkgroups( tg_group TEXT, frequency INTEGER, metadata JSONB, - tags TEXT[] NOT NULL DEFAULT '{}' + tags TEXT[] NOT NULL DEFAULT '{}', + notify BOOLEAN NOT NULL DEFAULT 'true', + weight REAL NOT NULL DEFAULT 1.0 ); CREATE INDEX IF NOT EXISTS talkgroup_id_tags ON talkgroups USING GIN (tags); - - CREATE TABLE IF NOT EXISTS talkgroups_learned( id SERIAL PRIMARY KEY, system_id INTEGER REFERENCES systems(id) NOT NULL, @@ -70,6 +70,17 @@ CREATE TABLE IF NOT EXISTS talkgroups_learned( UNIQUE (system_id, tgid, name) ); +CREATE TABLE IF NOT EXISTS alerts( + id SERIAL 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, + weight REAL, + score REAL, + metadata JSONB +); + CREATE OR REPLACE FUNCTION learn_talkgroup() RETURNS TRIGGER AS $$ BEGIN