OverviewErrors.constants.ts69 lines · main
1// @ts-nocheck
2import dayjs from 'dayjs'
3
4import { fetchLogs } from '@/data/reports/report.utils'
5
6export type ResponseErrorRow = {
7 method: string
8 path: string
9 status_code: number
10 count: number
11}
12
13export type AuthErrorCodeRow = {
14 error_code: string
15 count: number
16}
17
18export const getDateRange = () => {
19 return {
20 start: dayjs().subtract(24, 'hour').toISOString(),
21 end: dayjs().toISOString(),
22 }
23}
24
25// Top API response errors for /auth/v1 endpoints (path/method/status)
26export const AUTH_TOP_RESPONSE_ERRORS_SQL = `
27 select
28 request.method as method,
29 request.path as path,
30 response.status_code as status_code,
31 count(*) as count
32 from edge_logs
33 cross join unnest(metadata) as m
34 cross join unnest(m.request) as request
35 cross join unnest(m.response) as response
36 where path like '%auth/v1%'
37 and response.status_code between 400 and 599
38 group by method, path, status_code
39 order by count desc
40 limit 10
41`
42
43// Top Auth service error codes from x_sb_error_code header for /auth/v1 endpoints
44export const AUTH_TOP_ERROR_CODES_SQL = `
45 select
46 h.x_sb_error_code as error_code,
47 count(*) as count
48 from edge_logs
49 cross join unnest(metadata) as m
50 cross join unnest(m.request) as request
51 cross join unnest(m.response) as response
52 cross join unnest(response.headers) as h
53 where path like '%auth/v1%'
54 and response.status_code between 400 and 599
55 and h.x_sb_error_code is not null
56 group by error_code
57 order by count desc
58 limit 10
59`
60
61export const fetchTopResponseErrors = async (projectRef: string) => {
62 const { start, end } = getDateRange()
63 return await fetchLogs(projectRef, AUTH_TOP_RESPONSE_ERRORS_SQL, start, end)
64}
65
66export const fetchTopAuthErrorCodes = async (projectRef: string) => {
67 const { start, end } = getDateRange()
68 return await fetchLogs(projectRef, AUTH_TOP_ERROR_CODES_SQL, start, end)
69}