import {z} from 'zod' export type GetApiV1AnalyticsCollectionsCollectionIdLatencyArgs = {collection_id: string, hours?: number | undefined, group_by?: string | undefined}; export type GetApiV1AnalyticsCollectionsCollectionIdLatencyOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, NamespaceHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const apiAnalyticsCollectionsModelsTimeRange = z.object({start: z.string(), end: z.string()}); export const latencyMetric = z.object({time_bucket: z.string(), document_count: z.number().int(), avg_latency_ms: z.number(), p95_latency_ms: z.number(), p99_latency_ms: z.number()}); export const latencyResponse = z.object({collection_id: z.string(), time_range: apiAnalyticsCollectionsModelsTimeRange, metrics: z.array(latencyMetric), slowest_documents: z.array(z.record(z.string(), z.unknown())).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 }; /** * Get Latency Metrics * * GET /v1/analytics/collections/{collection_id}/latency */ export const getApiV1AnalyticsCollectionsCollectionIdLatency = async (args: GetApiV1AnalyticsCollectionsCollectionIdLatencyArgs, options?: GetApiV1AnalyticsCollectionsCollectionIdLatencyOptions) => { const {baseUrl = "https://api.mixpeek.com", fetch: fetchFn = fetch, BearerAuth, NamespaceHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.hours != null) query.append("hours", String(args.hours)) if (args.group_by != null) query.append("group_by", String(args.group_by)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) if (NamespaceHeader !== undefined) headers.set("X-Namespace", NamespaceHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/analytics/collections/${encodeURIComponent(String(args.collection_id))}/latency${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(latencyResponse, await res.json()) };