import {z} from 'zod' export type GetApiApiV2ExperimentsExperimentIdAnalysisPlanArgs = {experiment_id: string}; export type GetApiApiV2ExperimentsExperimentIdAnalysisPlanOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, appKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const experimentsAnalysisPlanV2MutationResponseDataAttributesBayesianPrior = z.object({degrees_of_freedom: z.number().nullable().optional(), standard_deviation: z.number().optional()}); export const experimentsAnalysisPlanV2DTODataAttributesConfidenceIntervalMethod = z.enum(["Sequential", "FixedSample", "Bayesian", "SequentialFixedHybrid"]); export const experimentsAnalysisPlanV2MutationResponseDataAttributes = z.object({bayesian_prior: experimentsAnalysisPlanV2MutationResponseDataAttributesBayesianPrior.optional(), confidence_interval_method: experimentsAnalysisPlanV2DTODataAttributesConfidenceIntervalMethod.optional(), confidence_level: z.number().optional(), cuped_lookback_period_days: z.number().int().optional(), experiment_auto_end_days: z.number().int().optional(), experiment_min_duration: z.number().int().optional(), experiment_min_sample_size: z.number().int().optional(), has_custom_analysis_settings: z.boolean().optional(), is_cuped_enabled: z.boolean().optional(), is_multiple_testing_correction_enabled: z.boolean().optional(), preferential_bonferroni_primary_metric_weight: z.number().optional(), target_duration_days: z.number().int().nullable().optional()}); export const experimentsAnalysisPlanWriteV2RequestDataType = z.literal("analysis-plans"); export const experimentsAnalysisPlanV2DTOData = z.object({attributes: experimentsAnalysisPlanV2MutationResponseDataAttributes.optional(), id: z.string(), type: experimentsAnalysisPlanWriteV2RequestDataType}); export const experimentsAnalysisPlanV2DTO = z.object({data: experimentsAnalysisPlanV2DTOData}); 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 experiment analysis plan * * GET /api/v2/experiments/{experiment_id}/analysis-plan * * Also accepts auth: AuthZ */ export const getApiApiV2ExperimentsExperimentIdAnalysisPlan = async (args: GetApiApiV2ExperimentsExperimentIdAnalysisPlanArgs, options?: GetApiApiV2ExperimentsExperimentIdAnalysisPlanOptions) => { const {baseUrl = "https://api.datadoghq.com", fetch: fetchFn = fetch, apiKeyAuth, appKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (apiKeyAuth !== undefined) headers.set("DD-API-KEY", apiKeyAuth) if (appKeyAuth !== undefined) headers.set("DD-APPLICATION-KEY", appKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/experiments/${encodeURIComponent(String(args.experiment_id))}/analysis-plan${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(experimentsAnalysisPlanV2DTO, await res.json()) };