diff --git a/pkg/alerting/alerting.go b/pkg/alerting/alerting.go index cf7e56e..23f282f 100644 --- a/pkg/alerting/alerting.go +++ b/pkg/alerting/alerting.go @@ -170,10 +170,10 @@ func (as *alerter) eval(ctx context.Context, now time.Time, testMode bool) ([]Al origScore := s.Score tgr, has := as.tgCache.TG(ctx, s.ID) if has { - if !tgr.Alert { + if !tgr.Talkgroup.Alert { continue } - s.Score *= float64(tgr.Weight) + s.Score *= float64(tgr.Talkgroup.Weight) } if s.Score > as.cfg.AlertThreshold || testMode { @@ -330,15 +330,15 @@ func (as *alerter) makeAlert(ctx context.Context, score trending.Score[talkgroup tgRecord, has := as.tgCache.TG(ctx, score.ID) switch has { case true: - d.Weight = tgRecord.Weight - if tgRecord.SystemName == "" { - tgRecord.SystemName = strconv.Itoa(int(score.ID.System)) + d.Weight = tgRecord.Talkgroup.Weight + if tgRecord.System.Name == "" { + tgRecord.System.Name = strconv.Itoa(int(score.ID.System)) } - if tgRecord.Name != nil { - d.TGName = fmt.Sprintf("%s %s (%d)", tgRecord.SystemName, *tgRecord.Name, score.ID.Talkgroup) + if tgRecord.Talkgroup.Name != nil { + d.TGName = fmt.Sprintf("%s %s (%d)", tgRecord.System.Name, *tgRecord.Talkgroup.Name, score.ID.Talkgroup) } else { - d.TGName = fmt.Sprintf("%s:%d", tgRecord.SystemName, int(score.ID.Talkgroup)) + d.TGName = fmt.Sprintf("%s:%d", tgRecord.System.Name, int(score.ID.Talkgroup)) } case false: system, has := as.tgCache.SystemName(ctx, int(score.ID.System)) diff --git a/pkg/alerting/stats.go b/pkg/alerting/stats.go index c8835d9..e513edd 100644 --- a/pkg/alerting/stats.go +++ b/pkg/alerting/stats.go @@ -78,7 +78,7 @@ func (as *alerter) tgStatsHandler(w http.ResponseWriter, r *http.Request) { tgMap := make(map[talkgroups.ID]database.GetTalkgroupsByPackedIDsRow, len(tgs)) for _, t := range tgs { - tgMap[talkgroups.ID{System: uint32(t.SystemID), Talkgroup: uint32(t.ID)}] = t + tgMap[talkgroups.ID{System: uint32(t.System.ID), Talkgroup: uint32(t.Talkgroup.ID)}] = t } renderData := struct { diff --git a/pkg/database/querier.go b/pkg/database/querier.go index b911805..4bf2177 100644 --- a/pkg/database/querier.go +++ b/pkg/database/querier.go @@ -22,14 +22,14 @@ type Querier interface { GetAPIKey(ctx context.Context, apiKey string) (ApiKey, error) GetDatabaseSize(ctx context.Context) (string, error) GetSystemName(ctx context.Context, systemID int) (string, error) - GetTalkgroup(ctx context.Context, systemID int, tgid int) (Talkgroup, error) + GetTalkgroup(ctx context.Context, systemID int, tgid int) (GetTalkgroupRow, error) GetTalkgroupIDsByTags(ctx context.Context, anytags []string, alltags []string, nottags []string) ([]GetTalkgroupIDsByTagsRow, error) GetTalkgroupTags(ctx context.Context, sys int, tg int) ([]string, error) GetTalkgroupWithLearned(ctx context.Context, systemID int, tgid int) (GetTalkgroupWithLearnedRow, error) GetTalkgroupWithLearnedByPackedIDs(ctx context.Context, dollar_1 []int64) ([]GetTalkgroupWithLearnedByPackedIDsRow, error) GetTalkgroupsByPackedIDs(ctx context.Context, dollar_1 []int64) ([]GetTalkgroupsByPackedIDsRow, error) - GetTalkgroupsWithAllTags(ctx context.Context, tags []string) ([]Talkgroup, error) - GetTalkgroupsWithAnyTags(ctx context.Context, tags []string) ([]Talkgroup, error) + GetTalkgroupsWithAllTags(ctx context.Context, tags []string) ([]GetTalkgroupsWithAllTagsRow, error) + GetTalkgroupsWithAnyTags(ctx context.Context, tags []string) ([]GetTalkgroupsWithAnyTagsRow, 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) diff --git a/pkg/database/talkgroups.sql.go b/pkg/database/talkgroups.sql.go index 0b58f8a..e8c4204 100644 --- a/pkg/database/talkgroups.sql.go +++ b/pkg/database/talkgroups.sql.go @@ -31,26 +31,30 @@ func (q *Queries) GetSystemName(ctx context.Context, systemID int) (string, erro } const getTalkgroup = `-- name: GetTalkgroup :one -SELECT id, system_id, tgid, name, alpha_tag, tg_group, frequency, metadata, tags, alert, alert_config, weight FROM talkgroups +SELECT talkgroups.id, talkgroups.system_id, talkgroups.tgid, talkgroups.name, talkgroups.alpha_tag, talkgroups.tg_group, talkgroups.frequency, talkgroups.metadata, talkgroups.tags, talkgroups.alert, talkgroups.alert_config, talkgroups.weight FROM talkgroups WHERE id = systg2id($1, $2) ` -func (q *Queries) GetTalkgroup(ctx context.Context, systemID int, tgid int) (Talkgroup, error) { +type GetTalkgroupRow struct { + Talkgroup Talkgroup `json:"talkgroup"` +} + +func (q *Queries) GetTalkgroup(ctx context.Context, systemID int, tgid int) (GetTalkgroupRow, error) { row := q.db.QueryRow(ctx, getTalkgroup, systemID, tgid) - var i Talkgroup + var i GetTalkgroupRow err := row.Scan( - &i.ID, - &i.SystemID, - &i.Tgid, - &i.Name, - &i.AlphaTag, - &i.TgGroup, - &i.Frequency, - &i.Metadata, - &i.Tags, - &i.Alert, - &i.AlertConfig, - &i.Weight, + &i.Talkgroup.ID, + &i.Talkgroup.SystemID, + &i.Talkgroup.Tgid, + &i.Talkgroup.Name, + &i.Talkgroup.AlphaTag, + &i.Talkgroup.TgGroup, + &i.Talkgroup.Frequency, + &i.Talkgroup.Metadata, + &i.Talkgroup.Tags, + &i.Talkgroup.Alert, + &i.Talkgroup.AlertConfig, + &i.Talkgroup.Weight, ) return i, err } @@ -101,9 +105,7 @@ func (q *Queries) GetTalkgroupTags(ctx context.Context, sys int, tg int) ([]stri const getTalkgroupWithLearned = `-- name: GetTalkgroupWithLearned :one SELECT -tg.id, tg.system_id, sys.name system_name, tg.tgid, tg.name, -tg.tg_group, tg.frequency, tg.metadata, tg.tags, tg.alpha_tag, -tg.alert, tg.weight, tg.alert_config, +tg.id, tg.system_id, tg.tgid, tg.name, tg.alpha_tag, tg.tg_group, tg.frequency, tg.metadata, tg.tags, tg.alert, tg.alert_config, tg.weight, sys.id, sys.name, FALSE learned FROM talkgroups tg JOIN systems sys ON tg.system_id = sys.id @@ -121,39 +123,29 @@ WHERE tgl.system_id = $1 AND tgl.tgid = $2 AND ignored IS NOT TRUE ` type GetTalkgroupWithLearnedRow struct { - ID int64 `json:"id"` - SystemID int32 `json:"system_id"` - SystemName string `json:"system_name"` - Tgid int32 `json:"tgid"` - Name *string `json:"name"` - TgGroup *string `json:"tg_group"` - Frequency *int32 `json:"frequency"` - Metadata []byte `json:"metadata"` - Tags []string `json:"tags"` - AlphaTag *string `json:"alpha_tag"` - Alert bool `json:"alert"` - Weight float32 `json:"weight"` - AlertConfig []byte `json:"alert_config"` - Learned bool `json:"learned"` + Talkgroup Talkgroup `json:"talkgroup"` + System System `json:"system"` + Learned bool `json:"learned"` } func (q *Queries) GetTalkgroupWithLearned(ctx context.Context, systemID int, tgid int) (GetTalkgroupWithLearnedRow, error) { row := q.db.QueryRow(ctx, getTalkgroupWithLearned, systemID, tgid) var i GetTalkgroupWithLearnedRow err := row.Scan( - &i.ID, - &i.SystemID, - &i.SystemName, - &i.Tgid, - &i.Name, - &i.TgGroup, - &i.Frequency, - &i.Metadata, - &i.Tags, - &i.AlphaTag, - &i.Alert, - &i.Weight, - &i.AlertConfig, + &i.Talkgroup.ID, + &i.Talkgroup.SystemID, + &i.Talkgroup.Tgid, + &i.Talkgroup.Name, + &i.Talkgroup.AlphaTag, + &i.Talkgroup.TgGroup, + &i.Talkgroup.Frequency, + &i.Talkgroup.Metadata, + &i.Talkgroup.Tags, + &i.Talkgroup.Alert, + &i.Talkgroup.AlertConfig, + &i.Talkgroup.Weight, + &i.System.ID, + &i.System.Name, &i.Learned, ) return i, err @@ -161,9 +153,7 @@ func (q *Queries) GetTalkgroupWithLearned(ctx context.Context, systemID int, tgi const getTalkgroupWithLearnedByPackedIDs = `-- name: GetTalkgroupWithLearnedByPackedIDs :many SELECT -tg.id, tg.system_id, sys.name system_name, tg.tgid, tg.name, -tg.tg_group, tg.frequency, tg.metadata, tg.tags, tg.alpha_tag, -tg.alert, tg.weight, tg.alert_config, +tg.id, tg.system_id, tg.tgid, tg.name, tg.alpha_tag, tg.tg_group, tg.frequency, tg.metadata, tg.tags, tg.alert, tg.alert_config, tg.weight, sys.id, sys.name, FALSE learned FROM talkgroups tg JOIN systems sys ON tg.system_id = sys.id @@ -181,20 +171,9 @@ WHERE systg2id(tgl.system_id, tgl.tgid) = ANY($1::INT8[]) AND ignored IS NOT TRU ` type GetTalkgroupWithLearnedByPackedIDsRow struct { - ID int64 `json:"id"` - SystemID int32 `json:"system_id"` - SystemName string `json:"system_name"` - Tgid int32 `json:"tgid"` - Name *string `json:"name"` - TgGroup *string `json:"tg_group"` - Frequency *int32 `json:"frequency"` - Metadata []byte `json:"metadata"` - Tags []string `json:"tags"` - AlphaTag *string `json:"alpha_tag"` - Alert bool `json:"alert"` - Weight float32 `json:"weight"` - AlertConfig []byte `json:"alert_config"` - Learned bool `json:"learned"` + Talkgroup Talkgroup `json:"talkgroup"` + System System `json:"system"` + Learned bool `json:"learned"` } func (q *Queries) GetTalkgroupWithLearnedByPackedIDs(ctx context.Context, dollar_1 []int64) ([]GetTalkgroupWithLearnedByPackedIDsRow, error) { @@ -207,19 +186,20 @@ func (q *Queries) GetTalkgroupWithLearnedByPackedIDs(ctx context.Context, dollar for rows.Next() { var i GetTalkgroupWithLearnedByPackedIDsRow if err := rows.Scan( - &i.ID, - &i.SystemID, - &i.SystemName, - &i.Tgid, - &i.Name, - &i.TgGroup, - &i.Frequency, - &i.Metadata, - &i.Tags, - &i.AlphaTag, - &i.Alert, - &i.Weight, - &i.AlertConfig, + &i.Talkgroup.ID, + &i.Talkgroup.SystemID, + &i.Talkgroup.Tgid, + &i.Talkgroup.Name, + &i.Talkgroup.AlphaTag, + &i.Talkgroup.TgGroup, + &i.Talkgroup.Frequency, + &i.Talkgroup.Metadata, + &i.Talkgroup.Tags, + &i.Talkgroup.Alert, + &i.Talkgroup.AlertConfig, + &i.Talkgroup.Weight, + &i.System.ID, + &i.System.Name, &i.Learned, ); err != nil { return nil, err @@ -233,26 +213,14 @@ func (q *Queries) GetTalkgroupWithLearnedByPackedIDs(ctx context.Context, dollar } const getTalkgroupsByPackedIDs = `-- name: GetTalkgroupsByPackedIDs :many -SELECT tg.id, system_id, tgid, tg.name, alpha_tag, tg_group, frequency, metadata, tags, alert, alert_config, weight, sys.id, sys.name FROM talkgroups tg +SELECT tg.id, tg.system_id, tg.tgid, tg.name, tg.alpha_tag, tg.tg_group, tg.frequency, tg.metadata, tg.tags, tg.alert, tg.alert_config, tg.weight, sys.id, sys.name FROM talkgroups tg JOIN systems sys ON tg.system_id = sys.id WHERE tg.id = ANY($1::INT8[]) ` type GetTalkgroupsByPackedIDsRow struct { - ID int64 `json:"id"` - SystemID int32 `json:"system_id"` - Tgid int32 `json:"tgid"` - Name *string `json:"name"` - AlphaTag *string `json:"alpha_tag"` - TgGroup *string `json:"tg_group"` - Frequency *int32 `json:"frequency"` - Metadata []byte `json:"metadata"` - Tags []string `json:"tags"` - Alert bool `json:"alert"` - AlertConfig []byte `json:"alert_config"` - Weight float32 `json:"weight"` - ID_2 int `json:"id_2"` - Name_2 string `json:"name_2"` + Talkgroup Talkgroup `json:"talkgroup"` + System System `json:"system"` } func (q *Queries) GetTalkgroupsByPackedIDs(ctx context.Context, dollar_1 []int64) ([]GetTalkgroupsByPackedIDsRow, error) { @@ -265,20 +233,20 @@ func (q *Queries) GetTalkgroupsByPackedIDs(ctx context.Context, dollar_1 []int64 for rows.Next() { var i GetTalkgroupsByPackedIDsRow if err := rows.Scan( - &i.ID, - &i.SystemID, - &i.Tgid, - &i.Name, - &i.AlphaTag, - &i.TgGroup, - &i.Frequency, - &i.Metadata, - &i.Tags, - &i.Alert, - &i.AlertConfig, - &i.Weight, - &i.ID_2, - &i.Name_2, + &i.Talkgroup.ID, + &i.Talkgroup.SystemID, + &i.Talkgroup.Tgid, + &i.Talkgroup.Name, + &i.Talkgroup.AlphaTag, + &i.Talkgroup.TgGroup, + &i.Talkgroup.Frequency, + &i.Talkgroup.Metadata, + &i.Talkgroup.Tags, + &i.Talkgroup.Alert, + &i.Talkgroup.AlertConfig, + &i.Talkgroup.Weight, + &i.System.ID, + &i.System.Name, ); err != nil { return nil, err } @@ -291,32 +259,36 @@ 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, alert, alert_config, weight FROM talkgroups +SELECT talkgroups.id, talkgroups.system_id, talkgroups.tgid, talkgroups.name, talkgroups.alpha_tag, talkgroups.tg_group, talkgroups.frequency, talkgroups.metadata, talkgroups.tags, talkgroups.alert, talkgroups.alert_config, talkgroups.weight FROM talkgroups WHERE tags && ARRAY[$1] ` -func (q *Queries) GetTalkgroupsWithAllTags(ctx context.Context, tags []string) ([]Talkgroup, error) { +type GetTalkgroupsWithAllTagsRow struct { + Talkgroup Talkgroup `json:"talkgroup"` +} + +func (q *Queries) GetTalkgroupsWithAllTags(ctx context.Context, tags []string) ([]GetTalkgroupsWithAllTagsRow, error) { rows, err := q.db.Query(ctx, getTalkgroupsWithAllTags, tags) if err != nil { return nil, err } defer rows.Close() - var items []Talkgroup + var items []GetTalkgroupsWithAllTagsRow for rows.Next() { - var i Talkgroup + var i GetTalkgroupsWithAllTagsRow if err := rows.Scan( - &i.ID, - &i.SystemID, - &i.Tgid, - &i.Name, - &i.AlphaTag, - &i.TgGroup, - &i.Frequency, - &i.Metadata, - &i.Tags, - &i.Alert, - &i.AlertConfig, - &i.Weight, + &i.Talkgroup.ID, + &i.Talkgroup.SystemID, + &i.Talkgroup.Tgid, + &i.Talkgroup.Name, + &i.Talkgroup.AlphaTag, + &i.Talkgroup.TgGroup, + &i.Talkgroup.Frequency, + &i.Talkgroup.Metadata, + &i.Talkgroup.Tags, + &i.Talkgroup.Alert, + &i.Talkgroup.AlertConfig, + &i.Talkgroup.Weight, ); err != nil { return nil, err } @@ -329,32 +301,36 @@ 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, alert, alert_config, weight FROM talkgroups +SELECT talkgroups.id, talkgroups.system_id, talkgroups.tgid, talkgroups.name, talkgroups.alpha_tag, talkgroups.tg_group, talkgroups.frequency, talkgroups.metadata, talkgroups.tags, talkgroups.alert, talkgroups.alert_config, talkgroups.weight FROM talkgroups WHERE tags @> ARRAY[$1] ` -func (q *Queries) GetTalkgroupsWithAnyTags(ctx context.Context, tags []string) ([]Talkgroup, error) { +type GetTalkgroupsWithAnyTagsRow struct { + Talkgroup Talkgroup `json:"talkgroup"` +} + +func (q *Queries) GetTalkgroupsWithAnyTags(ctx context.Context, tags []string) ([]GetTalkgroupsWithAnyTagsRow, error) { rows, err := q.db.Query(ctx, getTalkgroupsWithAnyTags, tags) if err != nil { return nil, err } defer rows.Close() - var items []Talkgroup + var items []GetTalkgroupsWithAnyTagsRow for rows.Next() { - var i Talkgroup + var i GetTalkgroupsWithAnyTagsRow if err := rows.Scan( - &i.ID, - &i.SystemID, - &i.Tgid, - &i.Name, - &i.AlphaTag, - &i.TgGroup, - &i.Frequency, - &i.Metadata, - &i.Tags, - &i.Alert, - &i.AlertConfig, - &i.Weight, + &i.Talkgroup.ID, + &i.Talkgroup.SystemID, + &i.Talkgroup.Tgid, + &i.Talkgroup.Name, + &i.Talkgroup.AlphaTag, + &i.Talkgroup.TgGroup, + &i.Talkgroup.Frequency, + &i.Talkgroup.Metadata, + &i.Talkgroup.Tags, + &i.Talkgroup.Alert, + &i.Talkgroup.AlertConfig, + &i.Talkgroup.Weight, ); err != nil { return nil, err } diff --git a/pkg/nexus/commands.go b/pkg/nexus/commands.go index 6d5a55f..d9b9180 100644 --- a/pkg/nexus/commands.go +++ b/pkg/nexus/commands.go @@ -71,9 +71,9 @@ func (c *client) Talkgroup(ctx context.Context, tg *pb.Talkgroup) error { } var md *structpb.Struct - if len(tgi.Metadata) > 0 { + if len(tgi.Talkgroup.Metadata) > 0 { m := make(map[string]interface{}) - err := json.Unmarshal(tgi.Metadata, &m) + err := json.Unmarshal(tgi.Talkgroup.Metadata, &m) if err != nil { log.Error().Err(err).Int32("sys", tg.System).Int32("tg", tg.Talkgroup).Msg("unmarshal tg metadata") } @@ -85,14 +85,14 @@ func (c *client) Talkgroup(ctx context.Context, tg *pb.Talkgroup) error { resp := &pb.TalkgroupInfo{ Tg: tg, - Name: tgi.Name, - Group: tgi.TgGroup, - Frequency: tgi.Frequency, + Name: tgi.Talkgroup.Name, + Group: tgi.Talkgroup.TgGroup, + Frequency: tgi.Talkgroup.Frequency, Metadata: md, - Tags: tgi.Tags, + Tags: tgi.Talkgroup.Tags, Learned: tgi.Learned, - AlphaTag: tgi.AlphaTag, - SystemName: tgi.SystemName, + AlphaTag: tgi.Talkgroup.AlphaTag, + SystemName: tgi.System.Name, } _ = c.Send(&pb.Message{ diff --git a/pkg/talkgroups/cache.go b/pkg/talkgroups/cache.go index 1a3f89b..8c59da3 100644 --- a/pkg/talkgroups/cache.go +++ b/pkg/talkgroups/cache.go @@ -90,11 +90,11 @@ func (t *cache) Hint(ctx context.Context, tgs []ID) error { } func (t *cache) add(rec database.GetTalkgroupWithLearnedByPackedIDsRow) error { - tg := TG(rec.SystemID, rec.Tgid) + tg := TG(rec.System.ID, int(rec.Talkgroup.Tgid)) t.tgs[tg] = rec - t.systems[rec.SystemID] = rec.SystemName + t.systems[int32(rec.System.ID)] = rec.System.Name - return t.AlertConfig.AddAlertConfig(tg, rec.AlertConfig) + return t.AlertConfig.AddAlertConfig(tg, rec.Talkgroup.AlertConfig) } func (t *cache) Load(ctx context.Context, tgs []int64) error { diff --git a/sql/postgres/queries/talkgroups.sql b/sql/postgres/queries/talkgroups.sql index 099e983..49783d1 100644 --- a/sql/postgres/queries/talkgroups.sql +++ b/sql/postgres/queries/talkgroups.sql @@ -1,9 +1,9 @@ -- name: GetTalkgroupsWithAnyTags :many -SELECT * FROM talkgroups +SELECT sqlc.embed(talkgroups) FROM talkgroups WHERE tags @> ARRAY[$1]; -- name: GetTalkgroupsWithAllTags :many -SELECT * FROM talkgroups +SELECT sqlc.embed(talkgroups) FROM talkgroups WHERE tags && ARRAY[$1]; -- name: GetTalkgroupIDsByTags :many @@ -25,19 +25,17 @@ UPDATE talkgroups SET tags = $2 WHERE id = ANY($1); -- name: GetTalkgroup :one -SELECT * FROM talkgroups +SELECT sqlc.embed(talkgroups) FROM talkgroups WHERE id = systg2id(sqlc.arg(system_id), sqlc.arg(tgid)); -- name: GetTalkgroupsByPackedIDs :many -SELECT * FROM talkgroups tg +SELECT sqlc.embed(tg), sqlc.embed(sys) FROM talkgroups tg JOIN systems sys ON tg.system_id = sys.id WHERE tg.id = ANY($1::INT8[]); -- name: GetTalkgroupWithLearned :one SELECT -tg.id, tg.system_id, sys.name system_name, tg.tgid, tg.name, -tg.tg_group, tg.frequency, tg.metadata, tg.tags, tg.alpha_tag, -tg.alert, tg.weight, tg.alert_config, +sqlc.embed(tg), sqlc.embed(sys), FALSE learned FROM talkgroups tg JOIN systems sys ON tg.system_id = sys.id @@ -55,9 +53,7 @@ WHERE tgl.system_id = sqlc.arg(system_id) AND tgl.tgid = sqlc.arg(tgid) AND igno -- name: GetTalkgroupWithLearnedByPackedIDs :many SELECT -tg.id, tg.system_id, sys.name system_name, tg.tgid, tg.name, -tg.tg_group, tg.frequency, tg.metadata, tg.tags, tg.alpha_tag, -tg.alert, tg.weight, tg.alert_config, +sqlc.embed(tg), sqlc.embed(sys), FALSE learned FROM talkgroups tg JOIN systems sys ON tg.system_id = sys.id