Spaces:
Sleeping

drop-edit / supabase /schema.sql
anititititi's picture
feat: tabela de erros e logging automatico na edge function
87a5fed
Raw History Blame Contribute Delete
7.36 kB
-- ─────────────────────────────────────────────────────────────
-- DROP EDIT β€” schema de mΓ©tricas e custos
-- Cole tudo isso no SQL Editor do Supabase e execute.
-- ─────────────────────────────────────────────────────────────
-- ── TABELAS ──────────────────────────────────────────────────
-- Uma linha por editorial gerado (preenchida automaticamente pelo app)
create table if not exists analyses (
id uuid primary key default gen_random_uuid(),
created_at timestamptz not null default now(),
session_id text,
magazine text not null, -- vogue | numero | another | id
estetica text,
nome_estetica text
);
-- Cliques de conversΓ£o pΓ³s-editorial (preenchida automaticamente pelo app)
create table if not exists events (
id uuid primary key default gen_random_uuid(),
created_at timestamptz not null default now(),
analysis_id uuid references analyses(id) on delete set null,
session_id text,
event_type text not null
-- valores esperados: choose_magazine | see_all_versions | save_editorial | search_piece
);
-- Custos operacionais mensais (vocΓͺ preenche manualmente no Table Editor)
create table if not exists operational_costs (
id uuid primary key default gen_random_uuid(),
created_at timestamptz not null default now(),
month date not null, -- primeiro dia do mΓͺs: 2025-04-01
category text not null, -- anthropic | vercel | supabase | domain | outro
amount_brl numeric(10,2) not null,
description text
);
-- ── ÍNDICES ──────────────────────────────────────────────────
create index if not exists idx_analyses_created_at on analyses(created_at);
create index if not exists idx_analyses_session_id on analyses(session_id);
create index if not exists idx_events_analysis_id on events(analysis_id);
create index if not exists idx_events_created_at on events(created_at);
create index if not exists idx_events_type on events(event_type);
create index if not exists idx_costs_month on operational_costs(month);
-- ── RLS (Row Level Security) ──────────────────────────────────
alter table analyses enable row level security;
alter table events enable row level security;
alter table operational_costs enable row level security;
-- O app insere anΓ‘lises e eventos com a chave anon (cliente)
create policy "anon_insert_analyses"
on analyses for insert to anon with check (true);
create policy "anon_insert_events"
on events for insert to anon with check (true);
-- Leitura bloqueada para anon β€” sΓ³ service_role (painel admin) pode ler
-- (service_role bypassa RLS automaticamente, nenhuma policy necessΓ‘ria)
-- Erros da Edge Function (inseridos pela prΓ³pria funΓ§Γ£o via service_role)
create table if not exists errors (
id uuid primary key default gen_random_uuid(),
created_at timestamptz not null default now(),
error_type text,
error_message text,
ip text,
revista text,
context jsonb
);
-- ── VIEWS ────────────────────────────────────────────────────
-- MΓ©tricas mensais consolidadas
create or replace view monthly_metrics with (security_invoker = true) as
with analysis_counts as (
select
date_trunc('month', created_at)::date as month,
count(*) as total_analyses,
count(distinct session_id) as unique_users
from analyses
group by 1
),
event_counts as (
select
date_trunc('month', created_at)::date as month,
count(*) filter (where event_type = 'choose_magazine') as choose_magazine_clicks,
count(*) filter (where event_type = 'see_all_versions') as see_all_versions_clicks,
count(*) filter (where event_type = 'save_editorial') as save_editorial_clicks,
count(*) filter (where event_type = 'search_piece') as search_piece_clicks
from events
group by 1
),
cost_totals as (
select
date_trunc('month', month)::date as month,
sum(amount_brl) as total_cost_brl
from operational_costs
group by 1
)
select
coalesce(ac.month, ec.month, ct.month) as month,
coalesce(ac.total_analyses, 0) as total_analyses,
coalesce(ac.unique_users, 0) as unique_users,
coalesce(ec.choose_magazine_clicks, 0) as choose_magazine_clicks,
coalesce(ec.see_all_versions_clicks, 0) as see_all_versions_clicks,
coalesce(ec.save_editorial_clicks, 0) as save_editorial_clicks,
coalesce(ec.search_piece_clicks, 0) as search_piece_clicks,
coalesce(ct.total_cost_brl, 0) as total_cost_brl,
case
when coalesce(ac.total_analyses, 0) > 0
then round(coalesce(ct.total_cost_brl, 0) / ac.total_analyses, 4)
else 0
end as cost_per_analysis_brl
from analysis_counts ac
full join event_counts ec on ac.month = ec.month
full join cost_totals ct on coalesce(ac.month, ec.month) = ct.month
order by month desc;
-- Unit economics + crescimento mΓͺs a mΓͺs (para deck de investidores)
create or replace view unit_economics with (security_invoker = true) as
select
month,
total_analyses,
unique_users,
total_cost_brl,
cost_per_analysis_brl,
-- taxas de conversΓ£o
case when total_analyses > 0
then round((choose_magazine_clicks::numeric / total_analyses) * 100, 1)
else 0 end as choose_magazine_rate_pct,
case when total_analyses > 0
then round((see_all_versions_clicks::numeric / total_analyses) * 100, 1)
else 0 end as see_all_versions_rate_pct,
case when total_analyses > 0
then round((search_piece_clicks::numeric / total_analyses) * 100, 1)
else 0 end as search_piece_rate_pct,
-- crescimento mΓͺs a mΓͺs
case
when lag(total_analyses) over (order by month) > 0
then round(
((total_analyses - lag(total_analyses) over (order by month))::numeric
/ lag(total_analyses) over (order by month)) * 100, 1)
else null
end as analyses_growth_pct,
case
when lag(unique_users) over (order by month) > 0
then round(
((unique_users - lag(unique_users) over (order by month))::numeric
/ lag(unique_users) over (order by month)) * 100, 1)
else null
end as users_growth_pct
from monthly_metrics
order by month desc;