Skip to content

Commit 203b770

Browse files
committed
add workers logs zod schemas
1 parent 76cef9b commit 203b770

File tree

1 file changed

+313
-0
lines changed

1 file changed

+313
-0
lines changed
Lines changed: 313 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,313 @@
1+
import { z } from "zod";
2+
3+
export const numericalOperations = ["eq", "neq", "gt", "gte", "lt", "lte"] as const;
4+
5+
export const queryOperations = [
6+
// applies only to strings
7+
"includes",
8+
"not_includes",
9+
10+
// string operations
11+
"starts_with",
12+
"regex",
13+
14+
// existence check
15+
"exists",
16+
"is_null",
17+
18+
// right hand side must be a string with comma separated values
19+
"in",
20+
"not_in",
21+
22+
// numerica
23+
...numericalOperations,
24+
] as const;
25+
26+
export const queryOperators = [
27+
"uniq",
28+
"count",
29+
"max",
30+
"min",
31+
"sum",
32+
"avg",
33+
"median",
34+
"p001",
35+
"p01",
36+
"p05",
37+
"p10",
38+
"p25",
39+
"p75",
40+
"p90",
41+
"p95",
42+
"p99",
43+
"p999",
44+
"stddev",
45+
"variance",
46+
] as const;
47+
48+
export const zQueryOperator = z.enum(queryOperators);
49+
export const zQueryOperation = z.enum(queryOperations);
50+
export const zQueryNumericalOperations = z.enum(numericalOperations);
51+
52+
export const zOffsetDirection = z.enum(["next", "prev"]);
53+
export const zFilterCombination = z.enum(["and", "or", "AND", "OR"]);
54+
55+
export const zPrimitiveUnion = z.union([z.string(), z.number(), z.boolean()]);
56+
57+
export const zQueryFilter = z.object({
58+
key: z.string(),
59+
operation: zQueryOperation,
60+
value: zPrimitiveUnion.optional(),
61+
type: z.enum(["string", "number", "boolean"]),
62+
});
63+
64+
export const zQueryCalculation = z.object({
65+
key: z.string().optional(),
66+
keyType: z.enum(["string", "number", "boolean"]).optional(),
67+
operator: zQueryOperator,
68+
alias: z.string().optional(),
69+
});
70+
export const zQueryGroupBy = z.object({
71+
type: z.enum(["string", "number", "boolean"]),
72+
value: z.string(),
73+
});
74+
75+
export const zSearchNeedle = z.object({
76+
value: zPrimitiveUnion,
77+
isRegex: z.boolean().optional(),
78+
matchCase: z.boolean().optional(),
79+
});
80+
81+
const zViews = z
82+
.enum(["traces", "events", "calculations", "invocations", "requests", "patterns"])
83+
.optional();
84+
85+
86+
87+
export const zAggregateResult = z.object({
88+
groups: z.array(z.object({ key: z.string(), value: zPrimitiveUnion })).optional(),
89+
value: z.number(),
90+
count: z.number(),
91+
interval: z.number(),
92+
sampleInterval: z.number(),
93+
});
94+
95+
export const zQueryRunCalculationsV2 = z.array(
96+
z.object({
97+
alias: z
98+
.string()
99+
.transform((val) => (val === "" ? undefined : val))
100+
.optional(),
101+
calculation: z.string(),
102+
aggregates: z.array(zAggregateResult),
103+
series: z.array(
104+
z.object({
105+
time: z.string(),
106+
data: z.array(
107+
zAggregateResult.merge(
108+
z.object({ firstSeen: z.string(), lastSeen: z.string() }),
109+
),
110+
),
111+
}),
112+
),
113+
}),
114+
);
115+
116+
export const zStatistics = z.object({
117+
elapsed: z.number(),
118+
rows_read: z.number(),
119+
bytes_read: z.number(),
120+
});
121+
122+
export const zCloudflareMiniEvent = z.object({
123+
event: z.record(z.string(), z.unknown()).optional(),
124+
scriptName: z.string(),
125+
outcome: z.string(),
126+
eventType: z.enum([
127+
"fetch",
128+
"scheduled",
129+
"alarm",
130+
"cron",
131+
"queue",
132+
"email",
133+
"tail",
134+
"rpc",
135+
"websocket",
136+
"unknown",
137+
]),
138+
entrypoint: z.string().optional(),
139+
scriptVersion: z
140+
.object({
141+
id: z.string().optional(),
142+
tag: z.string().optional(),
143+
message: z.string().optional(),
144+
})
145+
.optional(),
146+
truncated: z.boolean().optional(),
147+
executionModel: z.enum(["durableObject", "stateless"]).optional(),
148+
requestId: z.string(),
149+
});
150+
151+
export const zCloudflareEvent = zCloudflareMiniEvent.extend({
152+
diagnosticsChannelEvents: z
153+
.array(
154+
z.object({
155+
timestamp: z.number(),
156+
channel: z.string(),
157+
message: z.string(),
158+
}),
159+
)
160+
.optional(),
161+
dispatchNamespace: z.string().optional(),
162+
wallTimeMs: z.number(),
163+
cpuTimeMs: z.number(),
164+
});
165+
166+
export const zReturnedTelemetryEvent = z.object({
167+
dataset: z.string(),
168+
timestamp: z.number().int().positive(),
169+
source: z.union([z.string(), z.object({})]),
170+
$workers: z.union([zCloudflareMiniEvent, zCloudflareEvent]).optional(),
171+
$metadata: z.object({
172+
id: z.string(),
173+
requestId: z.string().optional(),
174+
traceId: z.string().optional(),
175+
spanId: z.string().optional(),
176+
trigger: z.string().optional(),
177+
parentSpanId: z.string().optional(),
178+
service: z.string().optional(),
179+
level: z.string().optional(),
180+
duration: z.number().positive().int().optional(),
181+
statusCode: z.number().positive().int().optional(),
182+
traceDuration: z.number().positive().int().optional(),
183+
error: z.string().optional(),
184+
message: z.string().optional(),
185+
spanName: z.string().optional(),
186+
url: z.string().optional(),
187+
region: z.string().optional(),
188+
account: z.string().optional(),
189+
provider: z.string().optional(),
190+
type: z.string().optional(),
191+
fingerprint: z.string().optional(),
192+
origin: z.string().optional(),
193+
metricName: z.string().optional(),
194+
stackId: z.string().optional(),
195+
coldStart: z.number().positive().int().optional(),
196+
cost: z.number().positive().int().optional(),
197+
cloudService: z.string().optional(),
198+
messageTemplate: z.string().optional(),
199+
errorTemplate: z.string().optional(),
200+
}),
201+
});
202+
203+
export const zReturnedQueryRunEvents = z.object({
204+
events: z.array(zReturnedTelemetryEvent).optional(),
205+
fields: z
206+
.array(
207+
z.object({
208+
key: z.string(),
209+
type: z.string(),
210+
}),
211+
)
212+
.optional(),
213+
count: z.number().optional(),
214+
});
215+
216+
/**
217+
* The request to run a query
218+
*/
219+
export const zQueryRunRequest = z.object({
220+
queryId: z.string(),
221+
parameters: z.object({
222+
datasets: z.array(z.string()).optional(),
223+
filters: z.array(zQueryFilter).optional(),
224+
filterCombination: zFilterCombination.optional(),
225+
calculations: z.array(zQueryCalculation).optional(),
226+
groupBys: z.array(zQueryGroupBy).optional(),
227+
orderBy: z
228+
.object({
229+
value: z.string(),
230+
order: z.enum(["asc", "desc"]).optional(),
231+
})
232+
.optional(),
233+
limit: z.number().int().nonnegative().max(100).optional(),
234+
needle: zSearchNeedle.optional(),
235+
}),
236+
timeframe: z.object({
237+
to: z.number(),
238+
from: z.number(),
239+
}),
240+
granularity: z.number().optional(),
241+
limit: z.number().max(100).optional().default(50),
242+
view: zViews.optional().default("calculations"),
243+
dry: z.boolean().optional().default(false),
244+
offset: z.string().optional(),
245+
offsetBy: z.number().optional(),
246+
offsetDirection: z.string().optional(),
247+
});
248+
249+
/**
250+
* The response from the API
251+
*/
252+
export const zReturnedQueryRunResult = z.object({
253+
run: zQueryRunRequest,
254+
calculations: zQueryRunCalculationsV2.optional(),
255+
compare: zQueryRunCalculationsV2.optional(),
256+
events: zReturnedQueryRunEvents.optional(),
257+
invocations: z.record(z.string(), z.array(zReturnedTelemetryEvent)).optional(),
258+
statistics: zStatistics,
259+
});
260+
261+
/**
262+
* Keys Request
263+
*/
264+
export const zKeysRequest = z.object({
265+
timeframe: z
266+
.object({
267+
to: z.number(),
268+
from: z.number(),
269+
})
270+
.optional(),
271+
datasets: z.array(z.string()).default([]),
272+
filters: z.array(zQueryFilter).default([]),
273+
limit: z.number().optional(),
274+
needle: zSearchNeedle.optional(),
275+
keyNeedle: zSearchNeedle.optional(),
276+
});
277+
278+
/**
279+
* Keys Response
280+
*/
281+
export const zKeysResponse = z.array(
282+
z.object({
283+
key: z.string(),
284+
type: z.enum(["string", "boolean", "number"]),
285+
lastSeenAt: z.number(),
286+
}),
287+
);
288+
289+
/**
290+
* Values Request
291+
*/
292+
export const zValuesRequest = z.object({
293+
timeframe: z.object({
294+
to: z.number(),
295+
from: z.number(),
296+
}),
297+
key: z.string(),
298+
type: z.enum(["string", "boolean", "number"]),
299+
datasets: z.array(z.string()),
300+
filters: z.array(zQueryFilter).default([]),
301+
limit: z.number().default(50),
302+
needle: zSearchNeedle.optional(),
303+
});
304+
305+
/** Values Response */
306+
export const zValuesResponse = z.array(
307+
z.object({
308+
key: z.string(),
309+
type: z.enum(["string", "boolean", "number"]),
310+
value: z.union([z.string(), z.number(), z.boolean()]),
311+
dataset: z.string(),
312+
}),
313+
);

0 commit comments

Comments
 (0)