Show opt-in counts by feature

This commit is contained in:
Jamie Brunton committed 2026-01-02 11:53:38 +00:00
1 parent e1b1800f03
commit e1ff87dc03
2 files changed
+90

No files matched your search

@@ -0,0 +1,29 @@
-- Expose per-feature opt-in counts for admins
CREATE OR REPLACE FUNCTION public.get_feature_interest_counts()
RETURNS TABLE (
feature_name TEXT,
opt_in_count BIGINT,
opt_in_count_last_7_days BIGINT
)
LANGUAGE plpgsql
SECURITY DEFINER
SET search_path = 'public'
AS $$
BEGIN
IF NOT has_permission('view_users') THEN
RAISE EXCEPTION 'Access denied';
END IF;
RETURN QUERY
SELECT
fi.feature_name,
COUNT(*)::BIGINT AS opt_in_count,
COUNT(*) FILTER (WHERE fi.created_at >= now() - interval '7 days')::BIGINT AS opt_in_count_last_7_days
FROM public.feature_interests fi
GROUP BY fi.feature_name
ORDER BY opt_in_count DESC, fi.feature_name ASC;
END;
$$;
GRANT EXECUTE ON FUNCTION public.get_feature_interest_counts() TO authenticated;