feat(reporting): fn_inc_filter_options for explorer dropdowns (migration 14)

Distinct engineers (owner, case-normalized), distinct clusters, and currently-open
ticket ids — feeds the ticket-explorer pulldowns. Backs GET /webhook/inc-filter-options.
Validated: 74 owners, 43 clusters, 489 open ids.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
david kiania 2026-06-19 12:34:43 +03:00
parent 5ea3f287d3
commit 73c5f31f97

View file

@ -0,0 +1,39 @@
-- 14_inc_filter_options.sql — fleettickets · ticket-explorer dropdown options
-- ─────────────────────────────────────────────────────────────────────────────
-- reporting.fn_inc_filter_options feeds the FleetOps ticket-explorer pulldowns:
-- distinct engineers (owner, case-normalized like migration 12/13), distinct
-- clusters, and the currently-open ticket ids (closed ids are ~22k → too many for a
-- pulldown; use the explorer's ticket-id substring search for historical lookups).
-- Backs GET /webhook/inc-filter-options. Idempotent (CREATE OR REPLACE).
-- ─────────────────────────────────────────────────────────────────────────────
SET search_path = tickets, public;
CREATE OR REPLACE FUNCTION reporting.fn_inc_filter_options()
RETURNS jsonb LANGUAGE sql STABLE AS $fn$
SELECT jsonb_build_object(
'owners', (SELECT COALESCE(jsonb_agg(o ORDER BY o), '[]'::jsonb)
FROM (SELECT DISTINCT initcap(lower(NULLIF(owner, ''))) AS o
FROM tickets.inc WHERE NULLIF(owner, '') IS NOT NULL) s),
'clusters', (SELECT COALESCE(jsonb_agg(c ORDER BY c), '[]'::jsonb)
FROM (SELECT DISTINCT cluster AS c
FROM tickets.inc WHERE NULLIF(cluster, '') IS NOT NULL) s),
'open_ticket_ids', (SELECT COALESCE(jsonb_agg(ticket_id ORDER BY ticket_id), '[]'::jsonb)
FROM tickets.inc WHERE COALESCE(is_actionable, false))
);
$fn$;
COMMENT ON FUNCTION reporting.fn_inc_filter_options() IS
'FleetOps ticket explorer dropdown options: distinct owners (normalized), clusters, '
'and open ticket ids. fleettickets 14.';
-- grants (guarded: roles may not exist on a fresh DB)
DO $grants$
BEGIN
IF EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'dashboard_ro') THEN
GRANT EXECUTE ON FUNCTION reporting.fn_inc_filter_options() TO dashboard_ro;
END IF;
IF EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'grafana_ro') THEN
GRANT EXECUTE ON FUNCTION reporting.fn_inc_filter_options() TO grafana_ro;
END IF;
END $grants$;