import {z} from 'zod' export type GetApiApiV2ExperimentsMetricsMetricIdArgs = {include?: Array | undefined, metric_id: string}; export type GetApiApiV2ExperimentsMetricsMetricIdOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, appKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const experimentsMetricV2DTODataAttributesDataSourceType = z.enum(["DATADOG", "DATADOG_REFERENCE_TABLE", "CUSTOMER_WAREHOUSE", "IMPORTED", "UNKNOWN"]); export const experimentsMetricV2DTODataAttributesPercentileAggregationDatadogMetricMeasure = z.object({column_name: z.string().optional(), column_type: z.string().optional(), filters: z.unknown(), name: z.string().optional(), query: z.string().optional(), source_definition_filter: z.unknown(), source_subtype: z.string().optional(), source_type: z.string().optional()}); export const experimentsMetricPropertyFilter = z.object({measure_id: z.string().optional(), operation: z.string().optional(), property_id: z.string().optional(), values: z.array(z.string()).optional()}); export const experimentsMetricPropertyFilterGroup = z.array(experimentsMetricPropertyFilter); export const experimentsMetricV2DTODataAttributesPercentileAggregationWarehouseMetricMeasure = z.object({id: z.string().optional(), pipeline_column_suffix: z.string().optional()}); export const experimentsMetricV2DTODataAttributesNumeratorAggregation = z.object({aging_threshold_days: z.number().int().optional(), datadog_metric_measure: experimentsMetricV2DTODataAttributesPercentileAggregationDatadogMetricMeasure.optional(), enable_aging_subject_filter: z.boolean().optional(), operation: z.string().optional(), pipeline_column_suffix: z.string().optional(), property_filters: z.array(experimentsMetricPropertyFilterGroup).optional(), threshold_aggregation_type: z.string().optional(), threshold_breach_value: z.number().optional(), threshold_comparison_operator: z.string().optional(), threshold_timeframe_dimension: z.string().optional(), threshold_timeframe_value: z.number().optional(), timeframe_end_value: z.number().optional(), timeframe_start_value: z.number().optional(), timeframe_unit: z.string().optional(), warehouse_metric_measure: experimentsMetricV2DTODataAttributesPercentileAggregationWarehouseMetricMeasure.optional(), winsor_lower_fixed_value: z.number().optional(), winsor_lower_percentile: z.number().optional(), winsor_upper_fixed_value: z.number().optional(), winsor_upper_percentile: z.number().optional(), winsorization_strategy: z.string().optional()}).nullable(); export const experimentsMetricV2DTODataAttributesDesiredChange = z.enum(["METRIC_INCREASES", "METRIC_DECREASES", "UNKNOWN"]); export const experimentsMetricV2DTODataAttributesMetricType = z.enum(["SIMPLE", "RATIO", "PERCENTILE", "UNKNOWN"]); export const experimentsMetricV2DTODataAttributesPercentileAggregation = z.object({datadog_metric_measure: experimentsMetricV2DTODataAttributesPercentileAggregationDatadogMetricMeasure.optional(), percentile: z.number().optional(), pipeline_column_suffix: z.string().optional(), property_filters: z.array(experimentsMetricPropertyFilterGroup).optional(), warehouse_metric_measure: experimentsMetricV2DTODataAttributesPercentileAggregationWarehouseMetricMeasure.optional()}).nullable(); export const experimentsMetricV2DTODataAttributes = z.object({certified_at: z.string().nullable().optional(), created_at: z.string().optional(), data_source_type: experimentsMetricV2DTODataAttributesDataSourceType.optional(), denominator_aggregation: experimentsMetricV2DTODataAttributesNumeratorAggregation.optional(), description: z.string().optional(), desired_change: experimentsMetricV2DTODataAttributesDesiredChange.optional(), experiment_count: z.number().int().nullable().optional(), format_as_percent: z.boolean().optional(), guardrail_cutoff_threshold: z.number().nullable().optional(), metric_type: experimentsMetricV2DTODataAttributesMetricType.optional(), migration_metadata: z.unknown(), name: z.string().optional(), numerator_aggregation: experimentsMetricV2DTODataAttributesNumeratorAggregation.optional(), percentile_aggregation: experimentsMetricV2DTODataAttributesPercentileAggregation.optional(), reference_url: z.string().optional(), updated_at: z.string().optional()}); export const metricType = z.literal("metrics"); export const experimentsMetricV2DTOData = z.object({attributes: experimentsMetricV2DTODataAttributes.optional(), id: z.string(), type: metricType}); export const experimentsMetricV2DTO = z.object({data: experimentsMetricV2DTOData}); 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 metric * * GET /api/v2/experiments/metrics/{metric_id} * * Also accepts auth: AuthZ */ export const getApiApiV2ExperimentsMetricsMetricId = async (args: GetApiApiV2ExperimentsMetricsMetricIdArgs, options?: GetApiApiV2ExperimentsMetricsMetricIdOptions) => { 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") for (const item of args.include ?? []) query.append("include", String(item)) 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/metrics/${encodeURIComponent(String(args.metric_id))}${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(experimentsMetricV2DTO, await res.json()) };