import {z} from 'zod' export type Configv1AggregationType = 'LAST' | 'MIN' | 'MAX' | 'MEAN' | 'MEDIAN' | 'COUNT' | 'SUM' | 'SUMSQ' | 'STDEV' | 'P10' | 'P20' | 'P30' | 'P40' | 'P50' | 'P60' | 'P70' | 'P80' | 'P90' | 'P95' | 'P99' | 'P999' | 'P9999' | 'P25' | 'P75' | 'COUNT_SAMPLES' | 'HISTOGRAM'; export type Configv1MappingRuleStoragePolicy = {resolution?: string | undefined, retention?: string | undefined}; export type MappingRuleAggregationPolicy = {aggregation?: Configv1AggregationType | undefined, drop_timestamp?: boolean | undefined, interval?: string | undefined, storage_policy?: Configv1MappingRuleStoragePolicy | undefined}; export type Configv1LabelFilter = {name?: string | undefined, value_glob?: string | undefined}; export type Configv1MappingRuleMode = 'ENABLED' | 'PREVIEW'; export type Configv1MappingRule = {aggregation_policy?: MappingRuleAggregationPolicy | undefined, bucket_slug?: string | undefined, created_at?: string | undefined, drop?: boolean | undefined, filters: Array, mode?: Configv1MappingRuleMode | undefined, name: string, slug?: string | undefined, updated_at?: string | undefined}; export type Configv1CreateMappingRuleRequest = {dry_run?: boolean | undefined, mapping_rule?: Configv1MappingRule | undefined}; export type CreateApiApiV1ConfigMappingRulesArgs = {body: Configv1CreateMappingRuleRequest}; export type CreateApiApiV1ConfigMappingRulesOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const configv1AggregationType = z.enum(["LAST", "MIN", "MAX", "MEAN", "MEDIAN", "COUNT", "SUM", "SUMSQ", "STDEV", "P10", "P20", "P30", "P40", "P50", "P60", "P70", "P80", "P90", "P95", "P99", "P999", "P9999", "P25", "P75", "COUNT_SAMPLES", "HISTOGRAM"]); export const configv1MappingRuleStoragePolicy = z.object({resolution: z.string().optional(), retention: z.string().optional()}); export const mappingRuleAggregationPolicy = z.object({aggregation: configv1AggregationType.optional(), drop_timestamp: z.boolean().optional(), interval: z.string().optional(), storage_policy: configv1MappingRuleStoragePolicy.optional()}); export const configv1LabelFilter = z.object({name: z.string().optional(), value_glob: z.string().optional()}); export const configv1MappingRuleMode = z.enum(["ENABLED", "PREVIEW"]); export const configv1MappingRule = z.object({aggregation_policy: mappingRuleAggregationPolicy.optional(), bucket_slug: z.string().optional(), created_at: z.string().optional(), drop: z.boolean().optional(), filters: z.array(configv1LabelFilter), mode: configv1MappingRuleMode.optional(), name: z.string(), slug: z.string().optional(), updated_at: z.string().optional()}); export const configv1CreateMappingRuleResponse = z.object({mapping_rule: configv1MappingRule.optional()}); 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 }; /** * POST /api/v1/config/mapping-rules */ export const createApiApiV1ConfigMappingRules = async (args: CreateApiApiV1ConfigMappingRulesArgs, options?: CreateApiApiV1ConfigMappingRulesOptions) => { const {baseUrl = "https://tenant.chronosphere.io", fetch: fetchFn = fetch, ApiKeyAuth} = 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("API-Token", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/config/mapping-rules${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(configv1CreateMappingRuleResponse, await res.json()) };