import {z} from 'zod' export type GetApiV1AnalyticsCollectionsCollectionIdExtractorsArgs = {collection_id: string, hours?: number | undefined}; export type GetApiV1AnalyticsCollectionsCollectionIdExtractorsOptions = {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 extractorMetrics = z.object({extractor_name: z.string(), version: z.string(), execution_count: z.number().int(), success_count: z.number().int(), failure_count: z.number().int(), success_rate: z.number(), avg_latency_ms: z.number(), p95_latency_ms: z.number(), p99_latency_ms: z.number()}); export const extractorPerformanceResponse = z.object({collection_id: z.string(), time_range: apiAnalyticsCollectionsModelsTimeRange, extractors: z.array(extractorMetrics), summary: 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 Extractor Performance * * GET /v1/analytics/collections/{collection_id}/extractors */ export const getApiV1AnalyticsCollectionsCollectionIdExtractors = async (args: GetApiV1AnalyticsCollectionsCollectionIdExtractorsArgs, options?: GetApiV1AnalyticsCollectionsCollectionIdExtractorsOptions) => { 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 (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))}/extractors${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(extractorPerformanceResponse, await res.json()) };