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
mydb.sql
/gordio

View file

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

View file

@ -18,15 +18,15 @@ type Querier interface {
DeleteAPIKey(ctx context.Context, apiKey uuid.UUID) error
DeleteUser(ctx context.Context, username string) error
GetAPIKey(ctx context.Context, apiKey uuid.UUID) (ApiKey, error)
GetTalkgroupTags(ctx context.Context, talkgroupID int) ([]string, error)
GetTalkgroupsWithAllTags(ctx context.Context, tags []string) ([]GetTalkgroupsWithAllTagsRow, error)
GetTalkgroupsWithAnyTags(ctx context.Context, tags []string) ([]GetTalkgroupsWithAnyTagsRow, error)
GetTalkgroupTags(ctx context.Context, systemID int, tgid int) ([]string, error)
GetTalkgroupsWithAllTags(ctx context.Context, tags []string) ([]Talkgroup, error)
GetTalkgroupsWithAnyTags(ctx context.Context, tags []string) ([]Talkgroup, error)
GetUserByID(ctx context.Context, id int32) (User, error)
GetUserByUID(ctx context.Context, id int32) (User, error)
GetUserByUsername(ctx context.Context, username string) (User, error)
GetUsers(ctx context.Context) ([]User, 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
}

View file

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

View file

@ -30,9 +30,12 @@ CREATE TABLE IF NOT EXISTS talkgroups(
tg_group TEXT,
frequency INTEGER,
metadata JSONB,
tags TEXT[] NOT NULL DEFAULT '{}',
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(
id SERIAL PRIMARY KEY,
system_id INTEGER REFERENCES systems(id) NOT NULL,
@ -59,15 +62,6 @@ BEGIN
END
$$ 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(
id UUID PRIMARY KEY,
submitter INTEGER REFERENCES api_keys(id) ON DELETE SET NULL,

View file

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