Move tg tags into the tg table

This commit is contained in:
Daniel 2024-07-28 23:07:04 -04:00
parent b9491679b2
commit f05a717e2f
6 changed files with 55 additions and 60 deletions

1
.gitignore vendored
View file

@ -1,2 +1,3 @@
config.yaml config.yaml
mydb.sql
/gordio /gordio

View file

@ -68,12 +68,13 @@ type System struct {
} }
type Talkgroup struct { type Talkgroup struct {
SystemID int `json:"system_id"` SystemID int `json:"system_id"`
Tgid int `json:"tgid"` Tgid int `json:"tgid"`
Name *string `json:"name"` Name *string `json:"name"`
TgGroup *string `json:"tg_group"` TgGroup *string `json:"tg_group"`
Frequency *int32 `json:"frequency"` Frequency *int32 `json:"frequency"`
Metadata []byte `json:"metadata"` Metadata []byte `json:"metadata"`
Tags []string `json:"tags"`
} }
type TalkgroupsLearned struct { type TalkgroupsLearned struct {
@ -85,12 +86,6 @@ type TalkgroupsLearned struct {
Ignored *bool `json:"ignored"` Ignored *bool `json:"ignored"`
} }
type TalkgroupsTag struct {
SystemID int `json:"system_id"`
TalkgroupID int `json:"talkgroup_id"`
Tags []string `json:"tags"`
}
type User struct { type User struct {
ID int32 `json:"id"` ID int32 `json:"id"`
Username string `json:"username"` Username string `json:"username"`

View file

@ -18,15 +18,15 @@ type Querier interface {
DeleteAPIKey(ctx context.Context, apiKey uuid.UUID) error DeleteAPIKey(ctx context.Context, apiKey uuid.UUID) error
DeleteUser(ctx context.Context, username string) error DeleteUser(ctx context.Context, username string) error
GetAPIKey(ctx context.Context, apiKey uuid.UUID) (ApiKey, error) GetAPIKey(ctx context.Context, apiKey uuid.UUID) (ApiKey, error)
GetTalkgroupTags(ctx context.Context, talkgroupID int) ([]string, error) GetTalkgroupTags(ctx context.Context, systemID int, tgid int) ([]string, error)
GetTalkgroupsWithAllTags(ctx context.Context, tags []string) ([]GetTalkgroupsWithAllTagsRow, error) GetTalkgroupsWithAllTags(ctx context.Context, tags []string) ([]Talkgroup, error)
GetTalkgroupsWithAnyTags(ctx context.Context, tags []string) ([]GetTalkgroupsWithAnyTagsRow, error) GetTalkgroupsWithAnyTags(ctx context.Context, tags []string) ([]Talkgroup, error)
GetUserByID(ctx context.Context, id int32) (User, error) GetUserByID(ctx context.Context, id int32) (User, error)
GetUserByUID(ctx context.Context, id int32) (User, error) GetUserByUID(ctx context.Context, id int32) (User, error)
GetUserByUsername(ctx context.Context, username string) (User, error) GetUserByUsername(ctx context.Context, username string) (User, error)
GetUsers(ctx context.Context) ([]User, error) GetUsers(ctx context.Context) ([]User, error)
SetCallTranscript(ctx context.Context, iD uuid.UUID, transcript *string) error SetCallTranscript(ctx context.Context, iD uuid.UUID, transcript *string) error
SetTalkgroupTags(ctx context.Context, systemID int, talkgroupID int, tags []string) error SetTalkgroupTags(ctx context.Context, tags []string, tgid int) error
UpdatePassword(ctx context.Context, username string, password string) error UpdatePassword(ctx context.Context, username string, password string) error
} }

View file

@ -10,37 +10,40 @@ import (
) )
const getTalkgroupTags = `-- name: GetTalkgroupTags :one const getTalkgroupTags = `-- name: GetTalkgroupTags :one
SELECT tags FROM talkgroups_tags SELECT tags FROM talkgroups
WHERE talkgroup_id = $1 WHERE system_id = $1 AND tgid = $2
` `
func (q *Queries) GetTalkgroupTags(ctx context.Context, talkgroupID int) ([]string, error) { func (q *Queries) GetTalkgroupTags(ctx context.Context, systemID int, tgid int) ([]string, error) {
row := q.db.QueryRow(ctx, getTalkgroupTags, talkgroupID) row := q.db.QueryRow(ctx, getTalkgroupTags, systemID, tgid)
var tags []string var tags []string
err := row.Scan(&tags) err := row.Scan(&tags)
return tags, err return tags, err
} }
const getTalkgroupsWithAllTags = `-- name: GetTalkgroupsWithAllTags :many const getTalkgroupsWithAllTags = `-- name: GetTalkgroupsWithAllTags :many
SELECT system_id, talkgroup_id FROM talkgroups_tags SELECT system_id, tgid, name, tg_group, frequency, metadata, tags FROM talkgroups
WHERE tags && ARRAY[$1] WHERE tags && ARRAY[$1]
` `
type GetTalkgroupsWithAllTagsRow struct { func (q *Queries) GetTalkgroupsWithAllTags(ctx context.Context, tags []string) ([]Talkgroup, error) {
SystemID int `json:"system_id"`
TalkgroupID int `json:"talkgroup_id"`
}
func (q *Queries) GetTalkgroupsWithAllTags(ctx context.Context, tags []string) ([]GetTalkgroupsWithAllTagsRow, error) {
rows, err := q.db.Query(ctx, getTalkgroupsWithAllTags, tags) rows, err := q.db.Query(ctx, getTalkgroupsWithAllTags, tags)
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer rows.Close() defer rows.Close()
var items []GetTalkgroupsWithAllTagsRow var items []Talkgroup
for rows.Next() { for rows.Next() {
var i GetTalkgroupsWithAllTagsRow var i Talkgroup
if err := rows.Scan(&i.SystemID, &i.TalkgroupID); err != nil { if err := rows.Scan(
&i.SystemID,
&i.Tgid,
&i.Name,
&i.TgGroup,
&i.Frequency,
&i.Metadata,
&i.Tags,
); err != nil {
return nil, err return nil, err
} }
items = append(items, i) items = append(items, i)
@ -52,25 +55,28 @@ func (q *Queries) GetTalkgroupsWithAllTags(ctx context.Context, tags []string) (
} }
const getTalkgroupsWithAnyTags = `-- name: GetTalkgroupsWithAnyTags :many const getTalkgroupsWithAnyTags = `-- name: GetTalkgroupsWithAnyTags :many
SELECT system_id, talkgroup_id FROM talkgroups_tags SELECT system_id, tgid, name, tg_group, frequency, metadata, tags FROM talkgroups
WHERE tags @> ARRAY[$1] WHERE tags @> ARRAY[$1]
` `
type GetTalkgroupsWithAnyTagsRow struct { func (q *Queries) GetTalkgroupsWithAnyTags(ctx context.Context, tags []string) ([]Talkgroup, error) {
SystemID int `json:"system_id"`
TalkgroupID int `json:"talkgroup_id"`
}
func (q *Queries) GetTalkgroupsWithAnyTags(ctx context.Context, tags []string) ([]GetTalkgroupsWithAnyTagsRow, error) {
rows, err := q.db.Query(ctx, getTalkgroupsWithAnyTags, tags) rows, err := q.db.Query(ctx, getTalkgroupsWithAnyTags, tags)
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer rows.Close() defer rows.Close()
var items []GetTalkgroupsWithAnyTagsRow var items []Talkgroup
for rows.Next() { for rows.Next() {
var i GetTalkgroupsWithAnyTagsRow var i Talkgroup
if err := rows.Scan(&i.SystemID, &i.TalkgroupID); err != nil { if err := rows.Scan(
&i.SystemID,
&i.Tgid,
&i.Name,
&i.TgGroup,
&i.Frequency,
&i.Metadata,
&i.Tags,
); err != nil {
return nil, err return nil, err
} }
items = append(items, i) items = append(items, i)
@ -82,11 +88,11 @@ func (q *Queries) GetTalkgroupsWithAnyTags(ctx context.Context, tags []string) (
} }
const setTalkgroupTags = `-- name: SetTalkgroupTags :exec const setTalkgroupTags = `-- name: SetTalkgroupTags :exec
INSERT INTO talkgroups_tags(system_id, talkgroup_id, tags) VALUES($1, $2, $3) UPDATE talkgroups SET tags = $1
ON CONFLICT (system_id, talkgroup_id) DO UPDATE SET tags = $3 WHERE system_id = $1 AND tgid = $2
` `
func (q *Queries) SetTalkgroupTags(ctx context.Context, systemID int, talkgroupID int, tags []string) error { func (q *Queries) SetTalkgroupTags(ctx context.Context, tags []string, tgid int) error {
_, err := q.db.Exec(ctx, setTalkgroupTags, systemID, talkgroupID, tags) _, err := q.db.Exec(ctx, setTalkgroupTags, tags, tgid)
return err return err
} }

View file

@ -30,9 +30,12 @@ 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 '{}',
PRIMARY KEY (system_id, tgid) PRIMARY KEY (system_id, tgid)
); );
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,
@ -59,15 +62,6 @@ BEGIN
END END
$$ LANGUAGE plpgsql; $$ LANGUAGE plpgsql;
CREATE TABLE IF NOT EXISTS talkgroups_tags(
system_id INTEGER NOT NULL,
talkgroup_id INTEGER NOT NULL,
tags TEXT[] NOT NULL DEFAULT '{}',
FOREIGN KEY (system_id, talkgroup_id) REFERENCES talkgroups (system_id, tgid),
PRIMARY KEY (system_id, talkgroup_id)
);
CREATE INDEX IF NOT EXISTS talkgroup_tags_id_tags ON talkgroups_tags USING GIN (tags);
CREATE TABLE IF NOT EXISTS calls( CREATE TABLE IF NOT EXISTS calls(
id UUID PRIMARY KEY, id UUID PRIMARY KEY,
submitter INTEGER REFERENCES api_keys(id) ON DELETE SET NULL, submitter INTEGER REFERENCES api_keys(id) ON DELETE SET NULL,

View file

@ -1,16 +1,15 @@
-- name: GetTalkgroupsWithAnyTags :many -- name: GetTalkgroupsWithAnyTags :many
SELECT system_id, talkgroup_id FROM talkgroups_tags SELECT * FROM talkgroups
WHERE tags @> ARRAY[$1]; WHERE tags @> ARRAY[$1];
-- name: GetTalkgroupsWithAllTags :many -- name: GetTalkgroupsWithAllTags :many
SELECT system_id, talkgroup_id FROM talkgroups_tags SELECT * FROM talkgroups
WHERE tags && ARRAY[$1]; WHERE tags && ARRAY[$1];
-- name: GetTalkgroupTags :one -- name: GetTalkgroupTags :one
SELECT tags FROM talkgroups_tags SELECT tags FROM talkgroups
WHERE talkgroup_id = $1; WHERE system_id = $1 AND tgid = $2;
-- name: SetTalkgroupTags :exec -- name: SetTalkgroupTags :exec
INSERT INTO talkgroups_tags(system_id, talkgroup_id, tags) VALUES($1, $2, $3) UPDATE talkgroups SET tags = $1
ON CONFLICT (system_id, talkgroup_id) DO UPDATE SET tags = $3; WHERE system_id = $1 AND tgid = $2;