import {z} from 'zod' export type DuckLakeMaintenanceStepName = 'flush_inlined_data' | 'thin_snapshots' | 'expire_snapshots' | 'merge_adjacent_files' | 'rewrite_data_files' | 'cleanup_old_files' | 'delete_orphaned_files' | 'vacuum_catalog'; export type DuckLakeMaintenanceRunThreshold = 'any' | 'warning' | 'critical'; export type DuckLakeMaintenanceSchedule = {steps?: (Array | null) | undefined, deleteThreshold?: (number | null) | undefined, expireSnapshotsOlderThan?: (string | null) | undefined, cleanupAll?: boolean | undefined, cpu?: string | undefined, memory?: string | undefined, timeoutSeconds?: number | undefined, thinKeepLast?: number | undefined, thinKeepDailyForDays?: number | undefined, vacuumFull?: boolean | undefined, mergeConcurrency?: number | undefined, runThreshold?: DuckLakeMaintenanceRunThreshold | undefined, cron: string, enabled?: boolean | undefined}; export type UpdateApiV4AdminLakeMaintenanceIntegrationIdScheduleArgs = {integration_id: string | string, kind?: ('full' | 'daily') | undefined, body: DuckLakeMaintenanceSchedule}; export type UpdateApiV4AdminLakeMaintenanceIntegrationIdScheduleOptions = {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 }; /** * Create or replace the scheduled DuckLake maintenance for an integration * * PUT /v4/admin/lake/maintenance/{integration_id}/schedule */ export const updateApiV4AdminLakeMaintenanceIntegrationIdSchedule = async (args: UpdateApiV4AdminLakeMaintenanceIntegrationIdScheduleArgs, options: UpdateApiV4AdminLakeMaintenanceIntegrationIdScheduleOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") 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: "PUT", 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(successResponseDuckLakeMaintenanceScheduleResponse, await res.json()) };