new learn

This commit is contained in:
Daniel 2024-07-28 08:40:01 -04:00
parent 60c2bc0be2
commit 08ed9cf546
2 changed files with 14 additions and 23 deletions

View file

@ -80,7 +80,7 @@ type TalkgroupsLearned struct {
ID int32 `json:"id"` ID int32 `json:"id"`
SystemID int `json:"system_id"` SystemID int `json:"system_id"`
Tgid int `json:"tgid"` Tgid int `json:"tgid"`
GroupName string `json:"group_name"` Name string `json:"name"`
GroupTag *string `json:"group_tag"` GroupTag *string `json:"group_tag"`
Ignored *bool `json:"ignored"` Ignored *bool `json:"ignored"`
} }

View file

@ -37,32 +37,23 @@ 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,
tgid INTEGER NOT NULL, tgid INTEGER NOT NULL,
group_name TEXT NOT NULL, name TEXT NOT NULL,
group_tag TEXT, group_tag TEXT,
ignored BOOLEAN, ignored BOOLEAN,
UNIQUE (system_id, tgid, group_name) UNIQUE (system_id, tgid, name)
); );
CREATE OR REPLACE FUNCTION learn_talkgroup() CREATE OR REPLACE FUNCTION learn_talkgroup()
RETURNS TRIGGER AS $$ RETURNS TRIGGER AS $$
BEGIN BEGIN
IF NOT EXISTS (SELECT * IF NOT EXISTS (
FROM talkgroups SELECT tg.system_id, tg.tgid, tg.name FROM talkgroups tg WHERE tg.system_id = NEW.system AND tg.tgid = NEW.talkgroup
LEFT JOIN talkgroups_tags UNION
ON talkgroups_tags.system_id = talkgroups.system_id AND talkgroups_tags.talkgroup_id = talkgroups.tgid SELECT tgl.system_id, tgl.tgid, tgl.name FROM talkgroups_learned tgl WHERE tgl.system_id = NEW.system AND tgl.tgid = NEW.talkgroup
LEFT JOIN talkgroups_learned
ON talkgroups_learned.system_id = talkgroups.system_id AND talkgroups_learned.tgid = talkgroups.tgid
WHERE
talkgroups.system_id = NEW.system AND talkgroups.tgid = NEW.talkgroup AND
(
talkgroups.name = NEW.tg_label
AND (NEW.tg_tag IS NULL OR (talkgroups_tags.tags @> ARRAY[NEW.tg_tag]))
AND (NEW.tg_group IS NULL OR talkgroups.tg_group = NEW.tg_group)
)
) THEN ) THEN
INSERT INTO talkgroups_learned(system_id, tgid, group_name, group_tag) VALUES( INSERT INTO talkgroups_learned(system_id, tgid, name, group_tag) VALUES(
NEW.system, NEW.talkgroup, NEW.tg_label, NEW.tg_tag NEW.system, NEW.talkgroup, NEW.tg_label, NEW.tg_tag
) ON CONFLICT (system_id, tgid, group_name) DO UPDATE SET group_tag = EXCLUDED.group_tag; ) ON CONFLICT DO NOTHING;
END IF; END IF;
RETURN NEW; RETURN NEW;
END END