caterium-app/ops/sql/SUPABASE-V17.6.1-CHAT-STORAGE-GUARD.sql
2026-09-07 17:59:58 +03:00

46 lines
3.2 KiB
PL/PgSQL

-- Caterium v17.6.1: server memory counters for Developer Console
create or replace function public.sun_platform_dashboard()
returns jsonb
language plpgsql
stable
security definer
set search_path='public','auth'
as $$
declare
v_companies bigint; v_accounts bigint; v_admins bigint; v_active bigint; v_trial bigint;
v_locked bigint; v_errors bigint; v_backups bigint; v_members bigint;
v_last_state timestamptz; v_last_backup timestamptz;
v_database_bytes bigint := 0; v_storage_bytes bigint := 0; v_storage_objects bigint := 0;
v_storage_buckets jsonb := '{}'::jsonb;
begin
perform public.sun_require_platform_admin_aal2();
select count(*) into v_companies from public.sun_workspaces;
select count(*) into v_accounts from auth.users;
select count(*) into v_admins from public.sun_platform_admins;
select count(*) into v_members from public.sun_workspace_members where is_active=true;
select count(*) into v_active from public.sun_workspace_subscriptions where status='active' and coalesce(current_period_end,'infinity'::timestamptz)>now();
select count(*) into v_trial from public.sun_workspace_subscriptions where status='trialing' and coalesce(trial_ends_at,'infinity'::timestamptz)>now();
select count(*) into v_locked from public.sun_workspaces w where public.sun_subscription_access_mode(w.id) in ('read_only','blocked');
select count(*) into v_errors from public.sun_v17_error_events where created_at>now()-interval '24 hours';
select count(*) into v_backups from public.sun_v17_backups where created_at>now()-interval '24 hours';
select max(updated_at) into v_last_state from public.sun_app_state;
select max(created_at) into v_last_backup from public.sun_v17_backups;
v_database_bytes := pg_database_size(current_database());
select count(*), coalesce(sum(case when coalesce(metadata->>'size','') ~ '^[0-9]+$' then (metadata->>'size')::bigint else 0 end),0)
into v_storage_objects, v_storage_bytes from storage.objects;
select coalesce(jsonb_object_agg(bucket_id,jsonb_build_object('objects',object_count,'bytes',bucket_bytes,'size',pg_size_pretty(bucket_bytes))),'{}'::jsonb)
into v_storage_buckets
from (select bucket_id,count(*)::bigint as object_count,coalesce(sum(case when coalesce(metadata->>'size','') ~ '^[0-9]+$' then (metadata->>'size')::bigint else 0 end),0)::bigint as bucket_bytes from storage.objects group by bucket_id) s;
return jsonb_build_object(
'companies',v_companies,'accounts',v_accounts,'platform_admins',v_admins,'memberships',v_members,
'active_subscriptions',v_active,'trials',v_trial,'restricted_companies',v_locked,
'errors_24h',v_errors,'backups_24h',v_backups,'last_state_at',v_last_state,'last_backup_at',v_last_backup,
'database_bytes',v_database_bytes,'database_size',pg_size_pretty(v_database_bytes),
'storage_bytes',v_storage_bytes,'storage_size',pg_size_pretty(v_storage_bytes),'storage_objects',v_storage_objects,'storage_buckets',v_storage_buckets,
'server_bytes',v_database_bytes+v_storage_bytes,'server_size',pg_size_pretty(v_database_bytes+v_storage_bytes),
'postgres_version',current_setting('server_version'));
end;
$$;
revoke all on function public.sun_platform_dashboard() from public, anon;
grant execute on function public.sun_platform_dashboard() to authenticated;