import {z} from 'zod' export type ExperimentsCreateMetricV2RequestDataAttributesDataSourceType = 'DATADOG' | 'DATADOG_REFERENCE_TABLE' | 'CUSTOMER_WAREHOUSE'; export type ExperimentsNullableDatadogMetricMeasureInput = {column_name?: string | undefined, column_type: string, filters: unknown, name: string, query?: string | undefined, source_definition_filter: unknown, source_subtype: string, source_type: string} | null; export type ExperimentsWarehouseEntryPointFilterOperation = 'IS' | 'IS_NOT'; export type ExperimentsPropertyFilterInput = {measure_id?: (string | null) | undefined, operation: ExperimentsWarehouseEntryPointFilterOperation, property_id: string, values: Array}; export type ExperimentsPropertyNullFilterInputOperation = 'IS_NULL' | 'IS_NOT_NULL'; export type ExperimentsPropertyNullFilterInput = {measure_id?: (string | null) | undefined, operation: ExperimentsPropertyNullFilterInputOperation, property_id: string, values?: (Array | null) | undefined}; export type ExperimentsMeasureComparisonFilterInputOperation = '=' | '!=' | '>' | '>=' | '<' | '<='; export type ExperimentsMeasureComparisonFilterInput = {measure_id: string, operation: ExperimentsMeasureComparisonFilterInputOperation, property_id?: (string | null) | undefined, values: Array}; export type ExperimentsMeasureRangeFilterInputOperation = 'BETWEEN'; export type ExperimentsMeasureRangeFilterInput = {measure_id: string, operation: ExperimentsMeasureRangeFilterInputOperation, property_id?: (string | null) | undefined, values: Array}; export type ExperimentsMeasureNullFilterInput = {measure_id: string, operation: ExperimentsPropertyNullFilterInputOperation, property_id?: (string | null) | undefined, values?: (Array | null) | undefined}; export type ExperimentsWarehouseFilterInput = ExperimentsPropertyFilterInput | ExperimentsPropertyNullFilterInput | ExperimentsMeasureComparisonFilterInput | ExperimentsMeasureRangeFilterInput | ExperimentsMeasureNullFilterInput; export type ExperimentsWarehouseFilterInputGroup = Array; export type ExperimentsWarehouseMetricMeasure = {id: string}; export type ExperimentsWarehouseMetricAggregationInput = {aging_threshold_days?: number | undefined, datadog_metric_measure?: ExperimentsNullableDatadogMetricMeasureInput | undefined, enable_aging_subject_filter?: boolean | undefined, operation: string, property_filters?: (Array | null) | undefined, threshold_aggregation_type?: string | undefined, threshold_breach_value?: number | undefined, threshold_comparison_operator?: string | undefined, threshold_timeframe_dimension?: string | undefined, threshold_timeframe_value?: number | undefined, timeframe_end_value?: number | undefined, timeframe_start_value?: number | undefined, timeframe_unit?: string | undefined, warehouse_metric_measure: ExperimentsWarehouseMetricMeasure, winsor_lower_fixed_value?: number | undefined, winsor_lower_percentile?: number | undefined, winsor_upper_fixed_value?: number | undefined, winsor_upper_percentile?: number | undefined, winsorization_strategy?: string | undefined}; export type ExperimentsDatadogMetricMeasureInput = {column_name?: string | undefined, column_type: string, filters: unknown, name: string, query?: string | undefined, source_definition_filter: unknown, source_subtype: string, source_type: string}; export type ExperimentsPropertyFilterInputGroup = Array; export type ExperimentsNullableWarehouseMetricMeasureInput = {id: string} | null; export type ExperimentsDatadogMetricAggregationInput = {aging_threshold_days?: number | undefined, datadog_metric_measure: ExperimentsDatadogMetricMeasureInput, enable_aging_subject_filter?: boolean | undefined, operation: string, property_filters?: (Array | null) | undefined, threshold_aggregation_type?: string | undefined, threshold_breach_value?: number | undefined, threshold_comparison_operator?: string | undefined, threshold_timeframe_dimension?: string | undefined, threshold_timeframe_value?: number | undefined, timeframe_end_value?: number | undefined, timeframe_start_value?: number | undefined, timeframe_unit?: string | undefined, warehouse_metric_measure?: ExperimentsNullableWarehouseMetricMeasureInput | undefined, winsor_lower_fixed_value?: number | undefined, winsor_lower_percentile?: number | undefined, winsor_upper_fixed_value?: number | undefined, winsor_upper_percentile?: number | undefined, winsorization_strategy?: string | undefined}; export type ExperimentsCreateMetricV2RequestDataAttributesNumeratorAggregation = ExperimentsWarehouseMetricAggregationInput | ExperimentsDatadogMetricAggregationInput; export type ExperimentsCreateMetricV2RequestDataAttributesDesiredChange = 'METRIC_INCREASES' | 'METRIC_DECREASES'; export type ExperimentsCreateMetricNumeratorAttributes = {data_source_type: ExperimentsCreateMetricV2RequestDataAttributesDataSourceType, denominator_aggregation?: ExperimentsCreateMetricV2RequestDataAttributesNumeratorAggregation | undefined, description?: (string | null) | undefined, desired_change: ExperimentsCreateMetricV2RequestDataAttributesDesiredChange, format_as_percent?: boolean | undefined, guardrail_cutoff_threshold?: (number | null) | undefined, migration_metadata: unknown, name: string, numerator_aggregation: ExperimentsCreateMetricV2RequestDataAttributesNumeratorAggregation}; export type ExperimentsNullableDatadogPercentileMeasureInput = {column_name: string, column_type: string, filters: unknown, name: string, query?: string | undefined, source_definition_filter: unknown, source_subtype: string, source_type: string} | null; export type ExperimentsWarehousePercentileAggregationInput = {datadog_metric_measure?: ExperimentsNullableDatadogPercentileMeasureInput | undefined, percentile: number, property_filters?: (Array | null) | undefined, warehouse_metric_measure: ExperimentsWarehouseMetricMeasure}; export type ExperimentsDatadogPercentileMeasureInput = {column_name: string, column_type: string, filters: unknown, name: string, query?: string | undefined, source_definition_filter: unknown, source_subtype: string, source_type: string}; export type ExperimentsDatadogPercentileAggregationInput = {datadog_metric_measure: ExperimentsDatadogPercentileMeasureInput, percentile: number, property_filters?: (Array | null) | undefined, warehouse_metric_measure?: ExperimentsNullableWarehouseMetricMeasureInput | undefined}; export type ExperimentsCreateMetricV2RequestDataAttributesPercentileAggregation = ExperimentsWarehousePercentileAggregationInput | ExperimentsDatadogPercentileAggregationInput; export type ExperimentsCreateMetricPercentileAttributes = {data_source_type: ExperimentsCreateMetricV2RequestDataAttributesDataSourceType, description?: (string | null) | undefined, desired_change: ExperimentsCreateMetricV2RequestDataAttributesDesiredChange, format_as_percent?: boolean | undefined, guardrail_cutoff_threshold?: (number | null) | undefined, migration_metadata: unknown, name: string, percentile_aggregation: ExperimentsCreateMetricV2RequestDataAttributesPercentileAggregation}; export type ExperimentsCreateMetricV2RequestDataAttributes = ExperimentsCreateMetricNumeratorAttributes | ExperimentsCreateMetricPercentileAttributes; export type MetricType = 'metrics'; export type ExperimentsCreateMetricV2RequestData = {attributes: ExperimentsCreateMetricV2RequestDataAttributes, type: MetricType}; export type ExperimentsCreateMetricV2Request = {data: ExperimentsCreateMetricV2RequestData}; export type CreateApiApiV2ExperimentsMetricsArgs = {body: ExperimentsCreateMetricV2Request}; export type CreateApiApiV2ExperimentsMetricsOptions = {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 }; /** * Create metric * * POST /api/v2/experiments/metrics * * Also accepts auth: AuthZ */ export const createApiApiV2ExperimentsMetrics = async (args: CreateApiApiV2ExperimentsMetricsArgs, options?: CreateApiApiV2ExperimentsMetricsOptions) => { const {baseUrl = "https://api.datadoghq.com", fetch: fetchFn = fetch, apiKeyAuth, appKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") 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/metrics${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(experimentsMetricV2DTO, await res.json()) };