2024-11-06 20:47:10 -05:00
|
|
|
-- name: AddCall :exec
|
2024-07-15 22:30:32 -04:00
|
|
|
INSERT INTO calls (
|
2024-11-06 20:47:10 -05:00
|
|
|
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 (
|
|
|
|
@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
|
|
|
|
);
|
2024-07-16 19:31:30 -04:00
|
|
|
|
2024-12-02 17:53:43 -05:00
|
|
|
-- name: GetCallAudioByID :one
|
|
|
|
SELECT call_date, audio_name, audio_type, audio_blob FROM calls WHERE id = @id;
|
|
|
|
|
2024-07-17 19:33:09 -04:00
|
|
|
-- name: SetCallTranscript :exec
|
2024-07-16 19:31:30 -04:00
|
|
|
UPDATE calls SET transcript = $2 WHERE id = $1;
|
2024-10-20 12:26:32 -04:00
|
|
|
|
2024-11-01 09:15:39 -04:00
|
|
|
-- 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
|
|
|
|
(
|
|
|
|
sqlc.arg(time),
|
2024-11-12 10:14:11 -05:00
|
|
|
sqlc.arg(tgid),
|
|
|
|
sqlc.arg(system_id),
|
2024-11-01 09:15:39 -04:00
|
|
|
sqlc.arg(weight),
|
|
|
|
sqlc.arg(score),
|
2024-11-02 09:41:48 -04:00
|
|
|
sqlc.arg(orig_score),
|
2024-11-02 14:26:58 -04:00
|
|
|
sqlc.arg(notified),
|
2024-11-01 09:15:39 -04:00
|
|
|
sqlc.arg(metadata)
|
|
|
|
);
|
|
|
|
|
2024-10-20 13:04:42 -04:00
|
|
|
-- name: GetDatabaseSize :one
|
|
|
|
SELECT pg_size_pretty(pg_database_size(current_database()));
|
2024-12-01 03:01:09 -05:00
|
|
|
|
|
|
|
-- 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 >= @range_start AND calls.call_date < @range_end
|
|
|
|
) INSERT INTO swept_calls SELECT * FROM to_sweep;
|
|
|
|
|
|
|
|
-- name: CleanupSweptCalls :execrows
|
2024-12-19 16:14:41 -05:00
|
|
|
-- This is used to sweep calls that are part of an incident prior to pruning a partition.
|
2024-12-01 03:01:09 -05:00
|
|
|
WITH to_sweep AS (
|
|
|
|
SELECT id FROM calls
|
|
|
|
JOIN incidents_calls ic ON ic.call_id = calls.id
|
|
|
|
WHERE calls.call_date >= @range_start AND calls.call_date < @range_end
|
|
|
|
) UPDATE incidents_calls
|
|
|
|
SET
|
|
|
|
swept_call_id = call_id,
|
|
|
|
calls_tbl_id = NULL
|
|
|
|
WHERE call_id IN (SELECT id FROM to_sweep);
|
2024-12-19 16:14:41 -05:00
|
|
|
|
|
|
|
-- 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 sqlc.narg('start')::TIMESTAMPTZ IS NOT NULL THEN
|
|
|
|
c.call_date >= @start ELSE TRUE END AND
|
|
|
|
CASE WHEN sqlc.narg('end')::TIMESTAMPTZ IS NOT NULL THEN
|
|
|
|
c.call_date <= sqlc.narg('end') ELSE TRUE END AND
|
|
|
|
CASE WHEN sqlc.narg('tags_any')::TEXT[] IS NOT NULL THEN
|
2024-12-27 14:16:38 -05:00
|
|
|
tgs.tags && ARRAY[@tags_any] ELSE TRUE END AND
|
2024-12-19 16:14:41 -05:00
|
|
|
CASE WHEN sqlc.narg('tags_not')::TEXT[] IS NOT NULL THEN
|
2024-12-27 14:16:38 -05:00
|
|
|
(NOT (tgs.tags && ARRAY[@tags_not])) ELSE TRUE END AND
|
2024-12-27 13:14:45 -05:00
|
|
|
(CASE WHEN sqlc.narg('tg_filter')::TEXT IS NOT NULL THEN (
|
|
|
|
tgs.tg_group ILIKE '%' || @tg_filter || '%' OR
|
|
|
|
tgs.name ILIKE '%' || @tg_filter || '%' OR
|
|
|
|
tgs.alpha_tag ILIKE '%' || @tg_filter || '%'
|
|
|
|
) ELSE TRUE END) AND
|
|
|
|
(CASE WHEN sqlc.narg('longer_than')::NUMERIC IS NOT NULL THEN (
|
|
|
|
c.duration > @longer_than
|
|
|
|
) ELSE TRUE END)
|
2024-12-19 16:14:41 -05:00
|
|
|
ORDER BY
|
|
|
|
CASE WHEN @direction::TEXT = 'asc' THEN c.call_date END ASC,
|
|
|
|
CASE WHEN @direction = 'desc' THEN c.call_date END DESC
|
|
|
|
OFFSET sqlc.arg('offset') ROWS
|
|
|
|
FETCH NEXT sqlc.arg('per_page') ROWS ONLY
|
|
|
|
;
|
|
|
|
|
|
|
|
-- 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 sqlc.narg('start')::TIMESTAMPTZ IS NOT NULL THEN
|
|
|
|
c.call_date >= @start ELSE TRUE END AND
|
|
|
|
CASE WHEN sqlc.narg('end')::TIMESTAMPTZ IS NOT NULL THEN
|
|
|
|
c.call_date <= sqlc.narg('end') ELSE TRUE END AND
|
|
|
|
CASE WHEN sqlc.narg('tags_any')::TEXT[] IS NOT NULL THEN
|
2024-12-27 14:16:38 -05:00
|
|
|
tgs.tags && ARRAY[@tags_any] ELSE TRUE END AND
|
2024-12-19 16:14:41 -05:00
|
|
|
CASE WHEN sqlc.narg('tags_not')::TEXT[] IS NOT NULL THEN
|
2024-12-27 14:16:38 -05:00
|
|
|
(NOT (tgs.tags && ARRAY[@tags_not])) ELSE TRUE END AND
|
2024-12-27 13:14:45 -05:00
|
|
|
(CASE WHEN sqlc.narg('tg_filter')::TEXT IS NOT NULL THEN (
|
|
|
|
tgs.tg_group ILIKE '%' || @tg_filter || '%' OR
|
|
|
|
tgs.name ILIKE '%' || @tg_filter || '%' OR
|
|
|
|
tgs.alpha_tag ILIKE '%' || @tg_filter || '%'
|
|
|
|
) ELSE TRUE END) AND
|
|
|
|
(CASE WHEN sqlc.narg('longer_than')::NUMERIC IS NOT NULL THEN (
|
|
|
|
c.duration > @longer_than
|
|
|
|
) ELSE TRUE END)
|
2024-12-19 16:14:41 -05:00
|
|
|
;
|