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