OverviewErrors.constants.ts 1.9 KB

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