import {z} from 'zod' export type MetricsParams = {replicaId?: (string | null) | undefined, start: string, stop: string}; export type CreateApiV2EndpointNamespaceNameMetricsInferenceRequestsArgs = {namespace: string, name: string, body: MetricsParams}; export type CreateApiV2EndpointNamespaceNameMetricsInferenceRequestsOptions = {baseUrl: string, token?: string | undefined, fetch?: typeof fetch | undefined}; export const dataPoint = z.object({x: z.string(), y: z.number()}); export const seriesHardware = z.object({data: z.array(dataPoint), deviceId: z.number().int().nullable().optional(), max: z.number().nullable().optional(), replicaId: z.string()}); export const graphHardware = z.object({series: z.array(seriesHardware)}); export const graphInferenceRequests = z.object({start: z.string(), stop: z.string(), timeWindowSeconds: z.number().int().gte(0), running: graphHardware, waiting: graphHardware}); 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 endpoint inference requests waiting and running [READ] * * POST /v2/endpoint/{namespace}/{name}/metrics/inference/requests */ export const createApiV2EndpointNamespaceNameMetricsInferenceRequests = async (args: CreateApiV2EndpointNamespaceNameMetricsInferenceRequestsArgs, options: CreateApiV2EndpointNamespaceNameMetricsInferenceRequestsOptions) => { const {baseUrl, fetch: fetchFn = fetch, token} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (token !== undefined) headers.set('authorization', `Bearer ${token}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/endpoint/${encodeURIComponent(String(args.namespace))}/${encodeURIComponent(String(args.name))}/metrics/inference/requests${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(graphInferenceRequests, await res.json()) };