import {z} from 'zod' export type GetApiV1TelemetryLatencyMetricNameArgs = {metric_name: string, time_range?: ('15m' | '30m' | '1h' | '2h' | '6h' | '1d' | '7d' | '30d') | undefined, time_window?: string | undefined, percentiles?: (string | null) | undefined, evaluation_id?: (string | null) | undefined, test_id?: (string | null) | undefined, probe_id?: (string | null) | undefined, agent_configuration_id?: (string | null) | undefined}; export type GetApiV1TelemetryLatencyMetricNameOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const timeSeriesPoint = z.object({timestamp: z.number().int(), value: z.number()}); export const latencyOverTimeResponse = z.object({metric_name: z.string(), time_window: z.string(), percentile: z.string(), values: z.array(timeSeriesPoint)}); export const getApiV1TelemetryLatencyMetricNameResponse = z.array(latencyOverTimeResponse); 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 }; /** * Get Latency Over Time * * GET /v1/telemetry/latency/{metric_name} */ export const getApiV1TelemetryLatencyMetricName = async (args: GetApiV1TelemetryLatencyMetricNameArgs, options: GetApiV1TelemetryLatencyMetricNameOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.time_range != null) query.append("time_range", String(args.time_range)) if (args.time_window != null) query.append("time_window", String(args.time_window)) if (args.percentiles != null) query.append("percentiles", String(args.percentiles)) if (args.evaluation_id != null) query.append("evaluation_id", String(args.evaluation_id)) if (args.test_id != null) query.append("test_id", String(args.test_id)) if (args.probe_id != null) query.append("probe_id", String(args.probe_id)) if (args.agent_configuration_id != null) query.append("agent_configuration_id", String(args.agent_configuration_id)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/telemetry/latency/${encodeURIComponent(String(args.metric_name))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiV1TelemetryLatencyMetricNameResponse, await res.json()) };