diff --git a/migrations/14_inc_filter_options.sql b/migrations/14_inc_filter_options.sql new file mode 100644 index 0000000..3bbbfc9 --- /dev/null +++ b/migrations/14_inc_filter_options.sql @@ -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$;