import {z} from 'zod' export type MetricsQuery = {check_id: number, data_type?: string | undefined, metrics?: Array | undefined, start_date?: string | undefined, end_date?: string | undefined, granularity?: ('auto' | '1min' | '15min' | '1hour' | '1day' | '1week' | '1month') | undefined, aggregation?: ('avg' | 'min' | 'max' | 'sum' | 'count' | 'median' | 'p75' | 'p90' | 'p95' | 'p99') | undefined, filters?: Record | undefined, group_by?: Array | undefined, limit?: (number | null) | undefined}; export type CreateApiApiV1MetricsChecksQueryArgs = {check_id: number, data_type?: string | undefined, metrics?: Array | undefined, start_date?: string | undefined, end_date?: string | undefined, granularity?: ('auto' | '1min' | '15min' | '1hour' | '1day' | '1week' | '1month') | undefined, aggregation?: ('avg' | 'min' | 'max' | 'sum' | 'count' | 'median' | 'p75' | 'p90' | 'p95' | 'p99') | undefined, filters?: Record | undefined, group_by?: Array | undefined, limit?: number | undefined, body: MetricsQuery}; export type CreateApiApiV1MetricsChecksQueryOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const metricsQuery = z.object({check_id: z.number().int(), data_type: z.string().optional(), metrics: z.array(z.string()).optional(), start_date: z.string().optional(), end_date: z.string().optional(), granularity: z.enum(["auto", "1min", "15min", "1hour", "1day", "1week", "1month"]).optional(), aggregation: z.enum(["avg", "min", "max", "sum", "count", "median", "p75", "p90", "p95", "p99"]).optional(), filters: z.object({}).optional(), group_by: z.array(z.string()).optional(), limit: z.number().int().nullable().optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Returns time-series datapoints with time bucketing and aggregation for one check. * * POST /api/v1/metrics/checks/query/ */ export const createApiApiV1MetricsChecksQuery = async (args: CreateApiApiV1MetricsChecksQueryArgs, options: CreateApiApiV1MetricsChecksQueryOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.check_id != null) query.append("check_id", String(args.check_id)) if (args.data_type != null) query.append("data_type", String(args.data_type)) for (const item of args.metrics ?? []) query.append("metrics", String(item)) if (args.start_date != null) query.append("start_date", String(args.start_date)) if (args.end_date != null) query.append("end_date", String(args.end_date)) if (args.granularity != null) query.append("granularity", String(args.granularity)) if (args.aggregation != null) query.append("aggregation", String(args.aggregation)) if (args.filters != null) query.append("filters", JSON.stringify(args.filters)) for (const item of args.group_by ?? []) query.append("group_by", String(item)) if (args.limit != null) query.append("limit", String(args.limit)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/metrics/checks/query/${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(metricsQuery, await res.json()) };