import {z} from 'zod' export type GetApiQueryV2NotebooksRuntimeUsersArgs = {start_date: string, end_date: string, instance_id?: (number | null) | undefined, account?: (string | null) | undefined, page?: number | undefined, size?: number | undefined, sortAttribute?: ('cost' | 'credits' | 'user_name' | 'source_rows') | undefined, sortOrder?: ('asc' | 'desc') | undefined, search?: (string | null) | undefined, instance_ids?: (Array | null) | undefined, navigationSource?: (string | null) | undefined, 'x-tenant': string}; export type GetApiQueryV2NotebooksRuntimeUsersOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const notebookRuntimeUserCost = z.object({instance_id: z.number().int(), account: z.string(), user_id: z.number().int().nullable(), user_name: z.string().nullable(), source_rows: z.number().int(), unknown_credit_rows: z.number().int(), credits: z.number().nullable(), cost: z.number().nullable(), cost_currency: z.string().nullable().optional()}); export const notebookRuntimeUserCosts = z.object({status: z.enum(["available", "no_data", "restricted", "unavailable"]), items: z.array(notebookRuntimeUserCost), total: z.number().int(), page: z.number().int(), size: z.number().int(), pages: z.number().int()}); 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 Notebook Runtime User Costs * * GET /query/v2/notebooks/runtime/users * * Also accepts auth: HTTPBearer */ export const getApiQueryV2NotebooksRuntimeUsers = async (args: GetApiQueryV2NotebooksRuntimeUsersArgs, options: GetApiQueryV2NotebooksRuntimeUsersOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.start_date != null) query.append("start_date", String(args.start_date)) if (args.end_date != null) query.append("end_date", String(args.end_date)) if (args.instance_id != null) query.append("instance_id", String(args.instance_id)) if (args.account != null) query.append("account", String(args.account)) if (args.page != null) query.append("page", String(args.page)) if (args.size != null) query.append("size", String(args.size)) if (args.sortAttribute != null) query.append("sortAttribute", String(args.sortAttribute)) if (args.sortOrder != null) query.append("sortOrder", String(args.sortOrder)) if (args.search != null) query.append("search", String(args.search)) for (const item of args.instance_ids ?? []) query.append("instance_ids", String(item)) if (args.navigationSource != null) query.append("navigationSource", String(args.navigationSource)) if (args['x-tenant'] != null) headers.set("x-tenant", String(args['x-tenant'])) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/query/v2/notebooks/runtime/users${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(notebookRuntimeUserCosts, await res.json()) };