File size: 7,362 Bytes
1c65f10 87a5fed 1c65f10 87a5fed 1c65f10 87a5fed 1c65f10 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | -- βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
-- 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;
|