stillbox/pkg/database/calls.sql.go

376 lines
9.3 KiB
Go
Raw Normal View History

2024-07-16 19:31:30 -04:00
// Code generated by sqlc. DO NOT EDIT.
// versions:
2024-11-15 11:34:54 -05:00
// sqlc v1.27.0
2024-07-16 19:31:30 -04:00
// source: calls.sql
package database
import (
"context"
2024-07-24 22:42:30 -04:00
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgtype"
2024-07-16 19:31:30 -04:00
)
2024-11-01 09:15:39 -04:00
const addAlert = `-- name: AddAlert :exec
2024-11-17 21:46:10 -05:00
INSERT INTO alerts (time, tgid, system_id, weight, score, orig_score, notified, metadata)
2024-11-01 09:15:39 -04:00
VALUES
(
$1,
$2,
$3,
$4,
$5,
2024-11-02 09:41:48 -04:00
$6,
2024-11-02 14:26:58 -04:00
$7,
2024-11-17 21:46:10 -05:00
$8
2024-11-01 09:15:39 -04:00
)
`
type AddAlertParams struct {
2024-11-02 09:41:48 -04:00
Time pgtype.Timestamptz `json:"time"`
2024-11-15 11:34:54 -05:00
TGID int `json:"tgid"`
SystemID int `json:"system_id"`
2024-11-02 09:41:48 -04:00
Weight *float32 `json:"weight"`
Score *float32 `json:"score"`
OrigScore *float32 `json:"orig_score"`
2024-11-02 14:26:58 -04:00
Notified bool `json:"notified"`
2024-11-02 09:41:48 -04:00
Metadata []byte `json:"metadata"`
2024-11-01 09:15:39 -04:00
}
func (q *Queries) AddAlert(ctx context.Context, arg AddAlertParams) error {
_, err := q.db.Exec(ctx, addAlert,
arg.Time,
2024-11-15 11:34:54 -05:00
arg.TGID,
arg.SystemID,
2024-11-01 09:15:39 -04:00
arg.Weight,
arg.Score,
2024-11-02 09:41:48 -04:00
arg.OrigScore,
2024-11-02 14:26:58 -04:00
arg.Notified,
2024-11-01 09:15:39 -04:00
arg.Metadata,
)
return err
}
const addCall = `-- name: AddCall :exec
2024-07-16 19:31:30 -04:00
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
)
2024-07-16 19:31:30 -04:00
`
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"`
2024-11-17 21:46:10 -05:00
TGLabel *string `json:"tg_label"`
TGAlphaTag *string `json:"tg_alpha_tag"`
TGGroup *string `json:"tg_group"`
Source int `json:"source"`
2024-07-16 19:31:30 -04:00
}
func (q *Queries) AddCall(ctx context.Context, arg AddCallParams) error {
_, err := q.db.Exec(ctx, addCall,
arg.ID,
2024-07-16 19:31:30 -04:00
arg.Submitter,
arg.System,
arg.Talkgroup,
2024-07-16 19:35:16 -04:00
arg.CallDate,
2024-07-16 19:31:30 -04:00
arg.AudioName,
arg.AudioBlob,
arg.AudioType,
arg.AudioUrl,
2024-08-11 13:46:43 -04:00
arg.Duration,
2024-07-16 19:31:30 -04:00
arg.Frequency,
arg.Frequencies,
arg.Patches,
2024-11-17 21:46:10 -05:00
arg.TGLabel,
arg.TGAlphaTag,
arg.TGGroup,
2024-07-16 19:31:30 -04:00
arg.Source,
)
return err
2024-07-16 19:31:30 -04:00
}
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
2024-12-29 09:30:24 -05:00
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
}
2024-10-20 13:04:42 -04:00
const getDatabaseSize = `-- name: GetDatabaseSize :one
SELECT pg_size_pretty(pg_database_size(current_database()))
2024-10-20 12:26:32 -04:00
`
2024-10-20 13:04:42 -04:00
func (q *Queries) GetDatabaseSize(ctx context.Context) (string, error) {
row := q.db.QueryRow(ctx, getDatabaseSize)
2024-10-20 12:26:32 -04:00
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
2024-12-27 14:16:38 -05:00
tgs.tags && ARRAY[$3] ELSE TRUE END AND
CASE WHEN $4::TEXT[] IS NOT NULL THEN
2024-12-27 14:16:38 -05:00
(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
2024-12-27 14:16:38 -05:00
tgs.tags && ARRAY[$3] ELSE TRUE END AND
CASE WHEN $4::TEXT[] IS NOT NULL THEN
2024-12-27 14:16:38 -05:00
(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
}
2024-07-17 19:33:09 -04:00
const setCallTranscript = `-- name: SetCallTranscript :exec
2024-07-16 19:31:30 -04:00
UPDATE calls SET transcript = $2 WHERE id = $1
`
2024-07-25 09:37:27 -04:00
func (q *Queries) SetCallTranscript(ctx context.Context, iD uuid.UUID, transcript *string) error {
2024-07-22 08:39:33 -04:00
_, err := q.db.Exec(ctx, setCallTranscript, iD, transcript)
2024-07-16 19:31:30 -04:00
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
`
2025-01-04 10:38:52 -05:00
// 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
}