import {z} from 'zod' export type GetApiProjectsProjectIdMetricsTimeseriesApiV1QueryArgs = {project_id: string, query: string, time?: string | undefined, timeout?: string | undefined, limit?: string | undefined}; export type GetApiProjectsProjectIdMetricsTimeseriesApiV1QueryOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const observabilityMetrics = z.object({data: z.string(), error: z.string().optional(), errorType: z.string().optional(), infos: z.array(z.string()).optional(), status: z.string(), warnings: z.array(z.string()).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 }; /** * Query timeseries at an instant via PromQL. * * GET /projects/{project_id}/metrics/timeseries/api/v1/query */ export const getApiProjectsProjectIdMetricsTimeseriesApiV1Query = async (args: GetApiProjectsProjectIdMetricsTimeseriesApiV1QueryArgs, options?: GetApiProjectsProjectIdMetricsTimeseriesApiV1QueryOptions) => { const {baseUrl = "https://api.cloud.crusoe.ai/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.query != null) query.append("query", String(args.query)) if (args.time != null) query.append("time", String(args.time)) if (args.timeout != null) query.append("timeout", String(args.timeout)) if (args.limit != null) query.append("limit", String(args.limit)) const url = `${baseUrl.replace(/\/+$/, '')}/projects/${encodeURIComponent(String(args.project_id))}/metrics/timeseries/api/v1/query${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(observabilityMetrics, await res.json()) };