import {z} from 'zod' export type RequestEncoding = 'base64' | 'Empty'; export type SearchEventType = 'ui' | 'dashboards' | 'reports' | 'alerts' | 'values' | 'other' | 'rum' | 'derivedstream' | 'searchjob' | 'download' | 'insights' | 'public_dashboard'; export type CreateApiApiOrgIdSearchPartitionArgs = {org_id: string, type?: string | undefined, body: {clusters?: Array | undefined, encoding?: RequestEncoding | undefined, end_time: number, histogram_interval?: number | undefined, query_fn?: (string | null) | undefined, regions?: Array | undefined, sampling_ratio?: (number | null) | undefined, search_type?: (SearchEventType | null) | undefined, sql: string, start_time: number, streaming_output?: boolean | undefined}}; export type CreateApiApiOrgIdSearchPartitionOptions = {baseUrl: string, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const orderBy = z.enum(["desc", "asc"]); export const searchPartitionResponse = z.object({compressed_size: z.number().int().gte(0), file_num: z.number().int().gte(0), histogram_interval: z.number().int().nullable().optional(), is_histogram_eligible: z.boolean().optional(), limit: z.number().int(), max_query_range: z.number().int(), order_by: orderBy, original_size: z.number().int().gte(0), partitions: z.array(z.array(z.number().int())), records: z.number().int().gte(0), streaming_aggs: z.boolean(), streaming_id: z.string().nullable().optional(), streaming_output: z.boolean(), trace_id: z.string()}); 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 }; /** * Search partition data * * POST /api/{org_id}/_search_partition */ export const createApiApiOrgIdSearchPartition = async (args: CreateApiApiOrgIdSearchPartitionArgs, options: CreateApiApiOrgIdSearchPartitionOptions) => { const {baseUrl, fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.type != null) query.append("type", String(args.type)) if (Authorization !== undefined) headers.set("Authorization", Authorization) const url = `${baseUrl.replace(/\/+$/, '')}/api/${encodeURIComponent(String(args.org_id))}/_search_partition${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(searchPartitionResponse, await res.json()) };