|
1 | 1 | import { z } from 'zod' |
2 | 2 |
|
| 3 | +import { nowISO, parseRelativeTime } from '../utils' |
| 4 | + |
3 | 5 | export const numericalOperations = ['eq', 'neq', 'gt', 'gte', 'lt', 'lte'] as const |
4 | 6 |
|
5 | 7 | export const queryOperations = [ |
@@ -150,27 +152,80 @@ export const zStatistics = z.object({ |
150 | 152 | bytes_read: z.number(), |
151 | 153 | }) |
152 | 154 |
|
153 | | -export const zTimeframe = z |
| 155 | +export const zTimeframeAbsolute = z |
154 | 156 | .object({ |
155 | 157 | to: z.string(), |
156 | 158 | from: z.string(), |
157 | 159 | }) |
158 | 160 | .describe( |
159 | | - `Timeframe for your query (ISO-8601 format). |
| 161 | + `An absolute timeframe for your query (ISO-8601 format). |
160 | 162 |
|
161 | | - • Current server time: ${new Date()} |
| 163 | + • Current server time: ${nowISO()} |
162 | 164 | • Default: Last hour from current time |
163 | 165 | • Maximum range: Last 7 days |
164 | 166 | • Format: "YYYY-MM-DDTHH:MM:SSZ" (e.g., "2025-04-29T14:30:00Z") |
165 | 167 |
|
166 | 168 | Examples: |
167 | | - - Last 30 minutes: from="2025-04-29T14:00:00Z", to="2025-04-29T14:30:00Z" |
168 | | - - Yesterday: from="2025-04-28T00:00:00Z", to="2025-04-29T00:00:00Z" |
| 169 | + - Between April 1st and 5th: from="2025-04-01T00:00:00Z", to="2025-04-05T23:59:59Z" |
169 | 170 |
|
170 | 171 | Note: Narrower timeframes provide faster responses and more specific results. |
171 | 172 | Omit this parameter entirely to use the default (last hour).` |
172 | 173 | ) |
173 | 174 |
|
| 175 | +export const zTimeframeRelative = z |
| 176 | + .object({ |
| 177 | + reference: z.string(), |
| 178 | + offset: z.string(), |
| 179 | + }) |
| 180 | + .describe( |
| 181 | + `Relative timeframe for your query, composed of a reference time and an offset. |
| 182 | +
|
| 183 | + • Current server time: ${nowISO()} |
| 184 | + • Default: Last hour from current time |
| 185 | + • Maximum range: Last 7 days |
| 186 | + • Reference time format: "YYYY-MM-DDTHH:MM:SSZ" (ISO-8601) (e.g., "2025-04-29T14:30:00Z") |
| 187 | + • Offset format: Must start with a '+' or '-' sign, which indicates whether the offset is in the past or future, followed by one or more time units (e.g., '+5d', '-2h', '+6h20m'). |
| 188 | + Units: s (seconds), m (minutes), h (hours), d (days), w (weeks). |
| 189 | + • You should not use a future looking offset in combination with the current server time as the reference time, as this will yield no results. (e.g. "the next 20 minutes") |
| 190 | +
|
| 191 | + Examples: |
| 192 | + - Last 30 minutes: reference="${nowISO()}", offset="-30m" |
| 193 | + - Yesterday: reference="${nowISO()}", offset="-1d" |
| 194 | +
|
| 195 | + Note: Narrower timeframes provide faster responses and more specific results. |
| 196 | + Omit this parameter entirely to use the default (last hour).` |
| 197 | + ) |
| 198 | + .transform((val) => { |
| 199 | + const referenceTime = new Date(val.reference).getTime() / 1000 |
| 200 | + |
| 201 | + if (isNaN(referenceTime)) { |
| 202 | + throw new Error(`Invalid reference time: ${val.reference}`) |
| 203 | + } |
| 204 | + |
| 205 | + const offsetSeconds = parseRelativeTime(val.offset) |
| 206 | + |
| 207 | + const from = new Date(Math.min(referenceTime + offsetSeconds, referenceTime) * 1000) |
| 208 | + const to = new Date(Math.max(referenceTime + offsetSeconds, referenceTime) * 1000) |
| 209 | + |
| 210 | + return { |
| 211 | + from: from.toISOString(), |
| 212 | + to: to.toISOString(), |
| 213 | + } |
| 214 | + }) |
| 215 | + |
| 216 | +export const zTimeframe = z.union([zTimeframeAbsolute, zTimeframeRelative]).describe( |
| 217 | + `Timeframe for your query, which can be either absolute or relative. |
| 218 | +
|
| 219 | + • Absolute timeframe: Specify exact start and end times in ISO-8601 format (e.g., "2025-04-29T14:30:00Z"). |
| 220 | + • Relative timeframe: Specify a reference time and an offset (e.g., reference="2025-04-29T14:30:00Z", offset="-30m"). |
| 221 | +
|
| 222 | + Examples: |
| 223 | + - Absolute: from="2025-04-01T00:00:00Z", to="2025-04-05T23:59:59Z" |
| 224 | + - Relative: reference="2025-04-29T14:30:00Z", offset="-30m" |
| 225 | +
|
| 226 | + Note: Narrower timeframes provide faster responses and more specific results.` |
| 227 | +) |
| 228 | + |
174 | 229 | const zCloudflareMiniEventDetailsRequest = z.object({ |
175 | 230 | url: z.string().optional(), |
176 | 231 | method: z.string().optional(), |
|
0 commit comments