Update routes/logs.py
Browse files- routes/logs.py +30 -33
routes/logs.py
CHANGED
@@ -26,45 +26,42 @@ logger = logging.getLogger(__name__)
|
|
26 |
@router.get("/logs", response_model=List[Dict[str, Any]])
|
27 |
async def get_onboarding_logs():
|
28 |
"""
|
29 |
-
Retorna os últimos 10 logs
|
30 |
-
"""
|
31 |
-
# SQL Query para buscar logs que envolvam a tabela Onboarding e sejam métodos de modificação
|
32 |
-
sql_query = """
|
33 |
-
select
|
34 |
-
cast(timestamp as text) as timestamp,
|
35 |
-
m.method,
|
36 |
-
m.status_code,
|
37 |
-
r.headers ->> 'x-client-info' as client_info,
|
38 |
-
r.headers ->> 'authorization' as auth_token,
|
39 |
-
r.url
|
40 |
-
from edge_logs
|
41 |
-
cross join unnest(metadata) as m
|
42 |
-
cross join unnest(m.request) as r
|
43 |
-
where
|
44 |
-
path like '%rest/v1/Onboarding%'
|
45 |
-
and m.method in ('POST', 'PATCH', 'PUT', 'DELETE')
|
46 |
-
order by
|
47 |
-
timestamp desc
|
48 |
-
limit 10;
|
49 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
async with aiohttp.ClientSession() as session:
|
52 |
try:
|
53 |
-
async with session.
|
54 |
-
f"{SUPABASE_URL}/rest/v1/rpc/execute_sql",
|
55 |
-
json={"sql": sql_query},
|
56 |
-
headers=SUPABASE_HEADERS,
|
57 |
-
) as response:
|
58 |
if response.status != 200:
|
59 |
-
|
60 |
-
|
61 |
-
raise HTTPException(
|
62 |
-
status_code=response.status,
|
63 |
-
detail="Erro ao consultar os logs no Supabase",
|
64 |
-
)
|
65 |
|
66 |
-
|
67 |
-
return data
|
68 |
|
69 |
except Exception as e:
|
70 |
logger.exception("Erro inesperado ao consultar logs")
|
|
|
26 |
@router.get("/logs", response_model=List[Dict[str, Any]])
|
27 |
async def get_onboarding_logs():
|
28 |
"""
|
29 |
+
Retorna os últimos 10 logs de modificação na tabela Onboarding.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
"""
|
31 |
+
log_api_url = f"https://api.supabase.com/v1/projects/ussxqnifefkgkaumjann/logs"
|
32 |
+
|
33 |
+
params = {
|
34 |
+
"sql": """
|
35 |
+
select
|
36 |
+
cast(timestamp as text) as timestamp,
|
37 |
+
m.method,
|
38 |
+
m.status_code,
|
39 |
+
r.url
|
40 |
+
from edge_logs
|
41 |
+
cross join unnest(metadata) as m
|
42 |
+
cross join unnest(m.request) as r
|
43 |
+
where
|
44 |
+
path like '%rest/v1/Onboarding%'
|
45 |
+
and m.method in ('POST', 'PATCH', 'PUT', 'DELETE')
|
46 |
+
order by timestamp desc
|
47 |
+
limit 10
|
48 |
+
"""
|
49 |
+
}
|
50 |
+
|
51 |
+
headers = {
|
52 |
+
"apikey": SUPABASE_KEY,
|
53 |
+
"Authorization": f"Bearer {SUPABASE_KEY}",
|
54 |
+
"Content-Type": "application/json"
|
55 |
+
}
|
56 |
|
57 |
async with aiohttp.ClientSession() as session:
|
58 |
try:
|
59 |
+
async with session.get(log_api_url, headers=headers, params=params) as response:
|
|
|
|
|
|
|
|
|
60 |
if response.status != 200:
|
61 |
+
logger.error(f"Erro ao consultar logs: {await response.text()}")
|
62 |
+
raise HTTPException(status_code=500, detail="Erro ao buscar logs")
|
|
|
|
|
|
|
|
|
63 |
|
64 |
+
return await response.json()
|
|
|
65 |
|
66 |
except Exception as e:
|
67 |
logger.exception("Erro inesperado ao consultar logs")
|