375 lines
9.3 KiB
Go
375 lines
9.3 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.27.0
|
|
// source: calls.sql
|
|
|
|
package database
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
const addAlert = `-- name: AddAlert :exec
|
|
INSERT INTO alerts (time, tgid, system_id, weight, score, orig_score, notified, metadata)
|
|
VALUES
|
|
(
|
|
$1,
|
|
$2,
|
|
$3,
|
|
$4,
|
|
$5,
|
|
$6,
|
|
$7,
|
|
$8
|
|
)
|
|
`
|
|
|
|
type AddAlertParams struct {
|
|
Time pgtype.Timestamptz `json:"time"`
|
|
TGID int `json:"tgid"`
|
|
SystemID int `json:"system_id"`
|
|
Weight *float32 `json:"weight"`
|
|
Score *float32 `json:"score"`
|
|
OrigScore *float32 `json:"orig_score"`
|
|
Notified bool `json:"notified"`
|
|
Metadata []byte `json:"metadata"`
|
|
}
|
|
|
|
func (q *Queries) AddAlert(ctx context.Context, arg AddAlertParams) error {
|
|
_, err := q.db.Exec(ctx, addAlert,
|
|
arg.Time,
|
|
arg.TGID,
|
|
arg.SystemID,
|
|
arg.Weight,
|
|
arg.Score,
|
|
arg.OrigScore,
|
|
arg.Notified,
|
|
arg.Metadata,
|
|
)
|
|
return err
|
|
}
|
|
|
|
const addCall = `-- name: AddCall :exec
|
|
INSERT INTO calls (
|
|
id,
|
|
submitter,
|
|
system,
|
|
talkgroup,
|
|
call_date,
|
|
audio_name,
|
|
audio_blob,
|
|
audio_type,
|
|
audio_url,
|
|
duration,
|
|
frequency,
|
|
frequencies,
|
|
patches,
|
|
tg_label,
|
|
tg_alpha_tag,
|
|
tg_group,
|
|
source
|
|
) VALUES (
|
|
$1,
|
|
$2,
|
|
$3,
|
|
$4,
|
|
$5,
|
|
$6,
|
|
$7,
|
|
$8,
|
|
$9,
|
|
$10,
|
|
$11,
|
|
$12,
|
|
$13,
|
|
$14,
|
|
$15,
|
|
$16,
|
|
$17
|
|
)
|
|
`
|
|
|
|
type AddCallParams struct {
|
|
ID uuid.UUID `json:"id"`
|
|
Submitter *int32 `json:"submitter"`
|
|
System int `json:"system"`
|
|
Talkgroup int `json:"talkgroup"`
|
|
CallDate pgtype.Timestamptz `json:"call_date"`
|
|
AudioName *string `json:"audio_name"`
|
|
AudioBlob []byte `json:"audio_blob"`
|
|
AudioType *string `json:"audio_type"`
|
|
AudioUrl *string `json:"audio_url"`
|
|
Duration *int32 `json:"duration"`
|
|
Frequency int `json:"frequency"`
|
|
Frequencies []int `json:"frequencies"`
|
|
Patches []int `json:"patches"`
|
|
TGLabel *string `json:"tg_label"`
|
|
TGAlphaTag *string `json:"tg_alpha_tag"`
|
|
TGGroup *string `json:"tg_group"`
|
|
Source int `json:"source"`
|
|
}
|
|
|
|
func (q *Queries) AddCall(ctx context.Context, arg AddCallParams) error {
|
|
_, err := q.db.Exec(ctx, addCall,
|
|
arg.ID,
|
|
arg.Submitter,
|
|
arg.System,
|
|
arg.Talkgroup,
|
|
arg.CallDate,
|
|
arg.AudioName,
|
|
arg.AudioBlob,
|
|
arg.AudioType,
|
|
arg.AudioUrl,
|
|
arg.Duration,
|
|
arg.Frequency,
|
|
arg.Frequencies,
|
|
arg.Patches,
|
|
arg.TGLabel,
|
|
arg.TGAlphaTag,
|
|
arg.TGGroup,
|
|
arg.Source,
|
|
)
|
|
return err
|
|
}
|
|
|
|
const cleanupSweptCalls = `-- name: CleanupSweptCalls :execrows
|
|
WITH to_sweep AS (
|
|
SELECT id FROM calls
|
|
JOIN incidents_calls ic ON ic.call_id = calls.id
|
|
WHERE calls.call_date >= $1 AND calls.call_date < $2
|
|
) UPDATE incidents_calls
|
|
SET
|
|
swept_call_id = call_id,
|
|
calls_tbl_id = NULL
|
|
WHERE call_id IN (SELECT id FROM to_sweep)
|
|
`
|
|
|
|
func (q *Queries) CleanupSweptCalls(ctx context.Context, rangeStart pgtype.Timestamptz, rangeEnd pgtype.Timestamptz) (int64, error) {
|
|
result, err := q.db.Exec(ctx, cleanupSweptCalls, rangeStart, rangeEnd)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
const getCallAudioByID = `-- name: GetCallAudioByID :one
|
|
SELECT
|
|
c.call_date,
|
|
c.audio_name,
|
|
c.audio_type,
|
|
c.audio_blob
|
|
FROM calls c
|
|
WHERE c.id = $1
|
|
UNION
|
|
SELECT
|
|
sc.call_date,
|
|
sc.audio_name,
|
|
sc.audio_type,
|
|
sc.audio_blob
|
|
FROM swept_calls sc
|
|
WHERE sc.id = $1
|
|
`
|
|
|
|
type GetCallAudioByIDRow struct {
|
|
CallDate pgtype.Timestamptz `json:"call_date"`
|
|
AudioName *string `json:"audio_name"`
|
|
AudioType *string `json:"audio_type"`
|
|
AudioBlob []byte `json:"audio_blob"`
|
|
}
|
|
|
|
func (q *Queries) GetCallAudioByID(ctx context.Context, id uuid.UUID) (GetCallAudioByIDRow, error) {
|
|
row := q.db.QueryRow(ctx, getCallAudioByID, id)
|
|
var i GetCallAudioByIDRow
|
|
err := row.Scan(
|
|
&i.CallDate,
|
|
&i.AudioName,
|
|
&i.AudioType,
|
|
&i.AudioBlob,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getDatabaseSize = `-- name: GetDatabaseSize :one
|
|
SELECT pg_size_pretty(pg_database_size(current_database()))
|
|
`
|
|
|
|
func (q *Queries) GetDatabaseSize(ctx context.Context) (string, error) {
|
|
row := q.db.QueryRow(ctx, getDatabaseSize)
|
|
var pg_size_pretty string
|
|
err := row.Scan(&pg_size_pretty)
|
|
return pg_size_pretty, err
|
|
}
|
|
|
|
const listCallsCount = `-- name: ListCallsCount :one
|
|
SELECT
|
|
COUNT(*)
|
|
FROM calls c
|
|
JOIN talkgroups tgs ON c.talkgroup = tgs.tgid AND c.system = tgs.system_id
|
|
WHERE
|
|
CASE WHEN $1::TIMESTAMPTZ IS NOT NULL THEN
|
|
c.call_date >= $1 ELSE TRUE END AND
|
|
CASE WHEN $2::TIMESTAMPTZ IS NOT NULL THEN
|
|
c.call_date <= $2 ELSE TRUE END AND
|
|
CASE WHEN $3::TEXT[] IS NOT NULL THEN
|
|
tgs.tags && ARRAY[$3] ELSE TRUE END AND
|
|
CASE WHEN $4::TEXT[] IS NOT NULL THEN
|
|
(NOT (tgs.tags && ARRAY[$4])) ELSE TRUE END AND
|
|
(CASE WHEN $5::TEXT IS NOT NULL THEN (
|
|
tgs.tg_group ILIKE '%' || $5 || '%' OR
|
|
tgs.name ILIKE '%' || $5 || '%' OR
|
|
tgs.alpha_tag ILIKE '%' || $5 || '%'
|
|
) ELSE TRUE END) AND
|
|
(CASE WHEN $6::NUMERIC IS NOT NULL THEN (
|
|
c.duration > $6
|
|
) ELSE TRUE END)
|
|
`
|
|
|
|
type ListCallsCountParams struct {
|
|
Start pgtype.Timestamptz `json:"start"`
|
|
End pgtype.Timestamptz `json:"end"`
|
|
TagsAny []string `json:"tags_any"`
|
|
TagsNot []string `json:"tags_not"`
|
|
TGFilter *string `json:"tg_filter"`
|
|
LongerThan pgtype.Numeric `json:"longer_than"`
|
|
}
|
|
|
|
func (q *Queries) ListCallsCount(ctx context.Context, arg ListCallsCountParams) (int64, error) {
|
|
row := q.db.QueryRow(ctx, listCallsCount,
|
|
arg.Start,
|
|
arg.End,
|
|
arg.TagsAny,
|
|
arg.TagsNot,
|
|
arg.TGFilter,
|
|
arg.LongerThan,
|
|
)
|
|
var count int64
|
|
err := row.Scan(&count)
|
|
return count, err
|
|
}
|
|
|
|
const listCallsP = `-- name: ListCallsP :many
|
|
SELECT
|
|
c.id,
|
|
c.call_date,
|
|
c.duration,
|
|
tgs.system_id,
|
|
tgs.tgid,
|
|
sys.name system_name,
|
|
tgs.name tg_name
|
|
FROM calls c
|
|
JOIN talkgroups tgs ON c.talkgroup = tgs.tgid AND c.system = tgs.system_id
|
|
JOIN systems sys ON sys.id = tgs.system_id
|
|
WHERE
|
|
CASE WHEN $1::TIMESTAMPTZ IS NOT NULL THEN
|
|
c.call_date >= $1 ELSE TRUE END AND
|
|
CASE WHEN $2::TIMESTAMPTZ IS NOT NULL THEN
|
|
c.call_date <= $2 ELSE TRUE END AND
|
|
CASE WHEN $3::TEXT[] IS NOT NULL THEN
|
|
tgs.tags && ARRAY[$3] ELSE TRUE END AND
|
|
CASE WHEN $4::TEXT[] IS NOT NULL THEN
|
|
(NOT (tgs.tags && ARRAY[$4])) ELSE TRUE END AND
|
|
(CASE WHEN $5::TEXT IS NOT NULL THEN (
|
|
tgs.tg_group ILIKE '%' || $5 || '%' OR
|
|
tgs.name ILIKE '%' || $5 || '%' OR
|
|
tgs.alpha_tag ILIKE '%' || $5 || '%'
|
|
) ELSE TRUE END) AND
|
|
(CASE WHEN $6::NUMERIC IS NOT NULL THEN (
|
|
c.duration > $6
|
|
) ELSE TRUE END)
|
|
ORDER BY
|
|
CASE WHEN $7::TEXT = 'asc' THEN c.call_date END ASC,
|
|
CASE WHEN $7 = 'desc' THEN c.call_date END DESC
|
|
OFFSET $8 ROWS
|
|
FETCH NEXT $9 ROWS ONLY
|
|
`
|
|
|
|
type ListCallsPParams struct {
|
|
Start pgtype.Timestamptz `json:"start"`
|
|
End pgtype.Timestamptz `json:"end"`
|
|
TagsAny []string `json:"tags_any"`
|
|
TagsNot []string `json:"tags_not"`
|
|
TGFilter *string `json:"tg_filter"`
|
|
LongerThan pgtype.Numeric `json:"longer_than"`
|
|
Direction string `json:"direction"`
|
|
Offset int32 `json:"offset"`
|
|
PerPage int32 `json:"per_page"`
|
|
}
|
|
|
|
type ListCallsPRow struct {
|
|
ID uuid.UUID `json:"id"`
|
|
CallDate pgtype.Timestamptz `json:"call_date"`
|
|
Duration *int32 `json:"duration"`
|
|
SystemID int32 `json:"system_id"`
|
|
TGID int32 `json:"tgid"`
|
|
SystemName string `json:"system_name"`
|
|
TGName *string `json:"tg_name"`
|
|
}
|
|
|
|
func (q *Queries) ListCallsP(ctx context.Context, arg ListCallsPParams) ([]ListCallsPRow, error) {
|
|
rows, err := q.db.Query(ctx, listCallsP,
|
|
arg.Start,
|
|
arg.End,
|
|
arg.TagsAny,
|
|
arg.TagsNot,
|
|
arg.TGFilter,
|
|
arg.LongerThan,
|
|
arg.Direction,
|
|
arg.Offset,
|
|
arg.PerPage,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []ListCallsPRow
|
|
for rows.Next() {
|
|
var i ListCallsPRow
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.CallDate,
|
|
&i.Duration,
|
|
&i.SystemID,
|
|
&i.TGID,
|
|
&i.SystemName,
|
|
&i.TGName,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const setCallTranscript = `-- name: SetCallTranscript :exec
|
|
UPDATE calls SET transcript = $2 WHERE id = $1
|
|
`
|
|
|
|
func (q *Queries) SetCallTranscript(ctx context.Context, iD uuid.UUID, transcript *string) error {
|
|
_, err := q.db.Exec(ctx, setCallTranscript, iD, transcript)
|
|
return err
|
|
}
|
|
|
|
const sweepCalls = `-- name: SweepCalls :execrows
|
|
WITH to_sweep AS (
|
|
SELECT id, submitter, system, talkgroup, calls.call_date, audio_name, audio_blob, duration, audio_type,
|
|
audio_url, frequency, frequencies, patches, tg_label, tg_alpha_tag, tg_group, source, transcript
|
|
FROM calls
|
|
JOIN incidents_calls ic ON ic.call_id = calls.id
|
|
WHERE calls.call_date >= $1 AND calls.call_date < $2
|
|
) INSERT INTO swept_calls SELECT id, submitter, system, talkgroup, call_date, audio_name, audio_blob, duration, audio_type, audio_url, frequency, frequencies, patches, tg_label, tg_alpha_tag, tg_group, source, transcript FROM to_sweep
|
|
`
|
|
|
|
// This is used to sweep calls that are part of an incident prior to pruning a partition.
|
|
func (q *Queries) SweepCalls(ctx context.Context, rangeStart pgtype.Timestamptz, rangeEnd pgtype.Timestamptz) (int64, error) {
|
|
result, err := q.db.Exec(ctx, sweepCalls, rangeStart, rangeEnd)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|