import {z} from 'zod' export type GetApiApiV1OperationsIndexAdvisorSuggestedIndexesArgs = {clusterName?: string | undefined}; export type GetApiApiV1OperationsIndexAdvisorSuggestedIndexesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const indexKeyDto = z.object({field: z.string().optional(), direction: z.number().int().optional()}); export const queryShapeMetricsDto = z.object({shapeId: z.string().optional(), namespace: z.string().optional(), avgExecutionTimeMillis: z.number().optional(), executionCount: z.number().int().optional(), avgQueryTargeting: z.number().optional(), avgDocsScanned: z.number().optional(), avgDocsReturned: z.number().optional(), sampledOperationCount: z.number().int().optional()}); export const indexRecommendationMetricsDto = z.object({avgExecutionTimeMillis: z.number().optional(), executionCount: z.number().int().optional(), executionsPerHour: z.number().optional(), windowHours: z.number().optional(), avgQueryTargeting: z.number().optional(), avgDocsScanned: z.number().optional(), avgDocsReturned: z.number().optional(), avgObjectSizeBytes: z.number().optional(), estimatedDiskReadBytesPerExecution: z.number().optional(), estimatedDiskReadBytesPerHour: z.number().optional(), sampledOperationCount: z.number().int().optional(), shapes: z.array(queryShapeMetricsDto).optional()}); export const suggestedIndexDto = z.object({recommendationKey: z.string().optional(), clusterName: z.string().optional(), namespace: z.string().optional(), database: z.string().optional(), collection: z.string().optional(), keys: z.array(indexKeyDto).optional(), weight: z.number().optional(), impactedQueryShapeIds: z.array(z.string()).optional(), metrics: indexRecommendationMetricsDto.optional(), status: z.enum(["PENDING", "CREATED", "REJECTED"]).optional(), decisionId: z.string().optional(), decidedBy: z.string().optional(), decidedAt: z.string().optional()}); export const getApiApiV1OperationsIndexAdvisorSuggestedIndexesResponse = z.array(suggestedIndexDto); 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 /api/v1/operations/index-advisor/suggested-indexes */ export const getApiApiV1OperationsIndexAdvisorSuggestedIndexes = async (args: GetApiApiV1OperationsIndexAdvisorSuggestedIndexesArgs, options?: GetApiApiV1OperationsIndexAdvisorSuggestedIndexesOptions) => { const {baseUrl = "https://api-docs.gosure.ai", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/vnd.hal+json") if (args.clusterName != null) query.append("clusterName", String(args.clusterName)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/operations/index-advisor/suggested-indexes${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(getApiApiV1OperationsIndexAdvisorSuggestedIndexesResponse, await res.json()) };