Add talkgroup activity alerting #17

Merged
amigan merged 11 commits from alerting into trunk 2024-10-31 00:20:48 -04:00
3 changed files with 41 additions and 7 deletions
Showing only changes of commit 673bddc4df - Show all commits

View file

@ -11,6 +11,17 @@ import (
"github.com/jackc/pgx/v5/pgtype" "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 { type ApiKey struct {
ID int32 `json:"id"` ID int32 `json:"id"`
Owner int `json:"owner"` Owner int `json:"owner"`
@ -78,6 +89,8 @@ type Talkgroup struct {
Frequency *int32 `json:"frequency"` Frequency *int32 `json:"frequency"`
Metadata []byte `json:"metadata"` Metadata []byte `json:"metadata"`
Tags []string `json:"tags"` Tags []string `json:"tags"`
Notify bool `json:"notify"`
Weight float32 `json:"weight"`
} }
type TalkgroupsLearned struct { type TalkgroupsLearned struct {

View file

@ -20,7 +20,7 @@ func (q *Queries) BulkSetTalkgroupTags(ctx context.Context, iD int64, tags []str
} }
const getTalkgroup = `-- name: GetTalkgroup :one 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) WHERE id = systg2id($1, $2)
` `
@ -37,6 +37,8 @@ func (q *Queries) GetTalkgroup(ctx context.Context, systemID int, tgid int) (Tal
&i.Frequency, &i.Frequency,
&i.Metadata, &i.Metadata,
&i.Tags, &i.Tags,
&i.Notify,
&i.Weight,
) )
return i, err return i, err
} }
@ -138,7 +140,7 @@ func (q *Queries) GetTalkgroupWithLearned(ctx context.Context, systemID int, tgi
} }
const getTalkgroupsByPackedIDs = `-- name: GetTalkgroupsByPackedIDs :many 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 JOIN systems sys ON tg.system_id = sys.id
WHERE tg.id = ANY($1::INT8[]) WHERE tg.id = ANY($1::INT8[])
` `
@ -153,6 +155,8 @@ type GetTalkgroupsByPackedIDsRow struct {
Frequency *int32 `json:"frequency"` Frequency *int32 `json:"frequency"`
Metadata []byte `json:"metadata"` Metadata []byte `json:"metadata"`
Tags []string `json:"tags"` Tags []string `json:"tags"`
Notify bool `json:"notify"`
Weight float32 `json:"weight"`
ID_2 int `json:"id_2"` ID_2 int `json:"id_2"`
Name_2 string `json:"name_2"` Name_2 string `json:"name_2"`
} }
@ -176,6 +180,8 @@ func (q *Queries) GetTalkgroupsByPackedIDs(ctx context.Context, dollar_1 []int64
&i.Frequency, &i.Frequency,
&i.Metadata, &i.Metadata,
&i.Tags, &i.Tags,
&i.Notify,
&i.Weight,
&i.ID_2, &i.ID_2,
&i.Name_2, &i.Name_2,
); err != nil { ); err != nil {
@ -190,7 +196,7 @@ func (q *Queries) GetTalkgroupsByPackedIDs(ctx context.Context, dollar_1 []int64
} }
const getTalkgroupsWithAllTags = `-- name: GetTalkgroupsWithAllTags :many 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] WHERE tags && ARRAY[$1]
` `
@ -213,6 +219,8 @@ func (q *Queries) GetTalkgroupsWithAllTags(ctx context.Context, tags []string) (
&i.Frequency, &i.Frequency,
&i.Metadata, &i.Metadata,
&i.Tags, &i.Tags,
&i.Notify,
&i.Weight,
); err != nil { ); err != nil {
return nil, err return nil, err
} }
@ -225,7 +233,7 @@ func (q *Queries) GetTalkgroupsWithAllTags(ctx context.Context, tags []string) (
} }
const getTalkgroupsWithAnyTags = `-- name: GetTalkgroupsWithAnyTags :many 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] WHERE tags @> ARRAY[$1]
` `
@ -248,6 +256,8 @@ func (q *Queries) GetTalkgroupsWithAnyTags(ctx context.Context, tags []string) (
&i.Frequency, &i.Frequency,
&i.Metadata, &i.Metadata,
&i.Tags, &i.Tags,
&i.Notify,
&i.Weight,
); err != nil { ); err != nil {
return nil, err return nil, err
} }

View file

@ -53,13 +53,13 @@ CREATE TABLE IF NOT EXISTS talkgroups(
tg_group TEXT, tg_group TEXT,
frequency INTEGER, frequency INTEGER,
metadata JSONB, 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 INDEX IF NOT EXISTS talkgroup_id_tags ON talkgroups USING GIN (tags);
CREATE TABLE IF NOT EXISTS talkgroups_learned( CREATE TABLE IF NOT EXISTS talkgroups_learned(
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,
system_id INTEGER REFERENCES systems(id) NOT NULL, system_id INTEGER REFERENCES systems(id) NOT NULL,
@ -70,6 +70,17 @@ CREATE TABLE IF NOT EXISTS talkgroups_learned(
UNIQUE (system_id, tgid, name) 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() CREATE OR REPLACE FUNCTION learn_talkgroup()
RETURNS TRIGGER AS $$ RETURNS TRIGGER AS $$
BEGIN BEGIN