UnifiedLogs.schema.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { z } from 'zod'
  2. import { LOG_TYPES, METHODS, REGIONS } from './UnifiedLogs.constants'
  3. import {
  4. ARRAY_DELIMITER,
  5. LEVELS,
  6. RANGE_DELIMITER,
  7. } from '@/components/ui/DataTable/DataTable.constants'
  8. export const columnSchema = z.object({
  9. id: z.string(),
  10. log_type: z.enum(LOG_TYPES),
  11. method: z.enum(METHODS),
  12. pathname: z.string(),
  13. level: z.enum(LEVELS),
  14. status: z.number(),
  15. date: z.date(),
  16. timestamp: z.number(),
  17. event_message: z.string().optional(),
  18. log_count: z.number().optional(), // used to count function logs for a given execution_id
  19. logs: z.array(z.any()).optional(), // array of function logs
  20. auth_user: z.string().optional(),
  21. })
  22. export type ColumnSchema = z.infer<typeof columnSchema>
  23. export const columnFilterSchema = z.object({
  24. level: z
  25. .string()
  26. .transform((val) => val.split(ARRAY_DELIMITER))
  27. .pipe(z.enum(LEVELS).array())
  28. .optional(),
  29. method: z
  30. .string()
  31. .transform((val) => val.split(ARRAY_DELIMITER))
  32. .pipe(z.enum(METHODS).array())
  33. .optional(),
  34. pathname: z.string().optional(),
  35. status: z
  36. .string()
  37. .transform((val) => val.split(ARRAY_DELIMITER))
  38. .pipe(z.string().array())
  39. .optional(),
  40. regions: z
  41. .string()
  42. .transform((val) => val.split(ARRAY_DELIMITER))
  43. .pipe(z.enum(REGIONS).array())
  44. .optional(),
  45. date: z
  46. .string()
  47. .transform((val) => val.split(RANGE_DELIMITER).map(Number))
  48. .pipe(z.coerce.date().array())
  49. .optional(),
  50. auth_user: z.string().optional(),
  51. })
  52. export type ColumnFilterSchema = z.infer<typeof columnFilterSchema>
  53. export const facetMetadataSchema = z.object({
  54. rows: z.array(z.object({ value: z.any(), total: z.number() })),
  55. total: z.number(),
  56. min: z.number().optional(),
  57. max: z.number().optional(),
  58. })
  59. export type FacetMetadataSchema = z.infer<typeof facetMetadataSchema>
  60. export type BaseChartSchema = { timestamp: number; [key: string]: number }
  61. export const timelineChartSchema = z.object({
  62. timestamp: z.number(), // UNIX
  63. ...LEVELS.reduce(
  64. (acc, level) => ({
  65. ...acc,
  66. [level]: z.number().default(0),
  67. }),
  68. {} as Record<(typeof LEVELS)[number], z.ZodNumber>
  69. ),
  70. // REMINDER: make sure to have the `timestamp` field in the object
  71. }) satisfies z.ZodType<BaseChartSchema>
  72. export type TimelineChartSchema = z.infer<typeof timelineChartSchema>