import {z} from 'zod' export type GetApiV4AdminLakeMaintenanceIntegrationIdScheduleArgs = {integration_id: string | string, kind?: ('full' | 'daily') | undefined}; export type GetApiV4AdminLakeMaintenanceIntegrationIdScheduleOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const duckLakeMaintenanceStepName = z.enum(["flush_inlined_data", "thin_snapshots", "expire_snapshots", "merge_adjacent_files", "rewrite_data_files", "cleanup_old_files", "delete_orphaned_files", "vacuum_catalog"]); export const duckLakeMaintenanceRunThreshold = z.enum(["any", "warning", "critical"]); export const duckLakeMaintenanceRequest = z.object({steps: z.array(duckLakeMaintenanceStepName).nullable().optional(), deleteThreshold: z.number().gte(0).lte(1).nullable().optional(), expireSnapshotsOlderThan: z.string().nullable().optional(), cleanupAll: z.boolean().optional(), cpu: z.string().optional(), memory: z.string().optional(), timeoutSeconds: z.number().int().gte(60).lte(604800).optional(), thinKeepLast: z.number().int().gte(1).lte(1000).optional(), thinKeepDailyForDays: z.number().int().gte(1).lte(365).optional(), vacuumFull: z.boolean().optional(), mergeConcurrency: z.number().int().gte(1).lte(32).optional(), runThreshold: duckLakeMaintenanceRunThreshold.optional()}); export const duckLakeMaintenanceScheduleResponse = z.object({jobId: z.union([z.string(), z.string()]), integrationId: z.union([z.string(), z.string()]), kind: z.enum(["full", "daily"]), cron: z.string(), enabled: z.boolean(), schedule: duckLakeMaintenanceRequest}); export const responseMeta = z.object({requestId: z.string().optional(), timestamp: z.string().optional(), durationMs: z.number().int().nullable().optional()}); export const successResponseDuckLakeMaintenanceScheduleResponse = z.object({success: z.literal(true), data: duckLakeMaintenanceScheduleResponse, meta: responseMeta.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 }; /** * Fetch the scheduled DuckLake maintenance for an integration * * GET /v4/admin/lake/maintenance/{integration_id}/schedule */ export const getApiV4AdminLakeMaintenanceIntegrationIdSchedule = async (args: GetApiV4AdminLakeMaintenanceIntegrationIdScheduleArgs, options: GetApiV4AdminLakeMaintenanceIntegrationIdScheduleOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.kind != null) query.append("kind", String(args.kind)) const url = `${baseUrl.replace(/\/+$/, '')}/v4/admin/lake/maintenance/${encodeURIComponent(String(args.integration_id))}/schedule${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(successResponseDuckLakeMaintenanceScheduleResponse, await res.json()) };