import {z} from 'zod' export type ConsumptionBudgetAlertActionConfig = {annotations?: {[key: string]: string} | undefined, instant_rate_sustain_secs?: number | undefined, labels?: {[key: string]: string} | undefined}; export type Configv1LogSearchFilter = {query?: string | undefined}; export type ConsumptionBudgetPriorityFilter = {dataset_slug?: string | undefined, log_filter?: Configv1LogSearchFilter | undefined}; export type ConsumptionBudgetPriority = {filters: Array, priority: number}; export type ConsumptionBudgetResourceGroup = 'LOG_PERSISTED_BYTES' | 'LOG_PROCESSED_BYTES' | 'METRIC_PERSISTED_SERIES' | 'METRIC_ALL' | 'LOG_ALL' | 'TRACE_PROCESSED_BYTES' | 'TRACE_PERSISTED_BYTES' | 'TRACE_ALL' | 'ALL'; export type ConsumptionBudgetThresholdAction = 'ALERT_WARN' | 'ALERT_CRITICAL' | 'DROP'; export type ThresholdInstantRate = {fixed_value_per_sec: unknown}; export type ConsumptionBudgetThresholdType = 'DAILY_VOLUME' | 'INSTANT_RATE' | 'WEEKLY_VOLUME' | 'MONTHLY_VOLUME' | 'HOURLY_VOLUME' | 'ROLLING_1_HOUR_VOLUME' | 'ROLLING_3_HOUR_VOLUME' | 'ROLLING_1_DAY_VOLUME' | 'ROLLING_7_DAY_VOLUME' | 'ROLLING_30_MINUTE_VOLUME'; export type ConsumptionBudgetUnit = 'NATIVE' | 'CREDITS'; export type ThresholdVolume = {fixed_value: unknown}; export type Configv1ConsumptionBudgetThreshold = {action: ConsumptionBudgetThresholdAction, instant_rate?: ThresholdInstantRate | undefined, resource_group?: ConsumptionBudgetResourceGroup | undefined, type: ConsumptionBudgetThresholdType, unit?: ConsumptionBudgetUnit | undefined, volume?: ThresholdVolume | undefined}; export type Configv1ConsumptionBudget = {alert_action_config?: ConsumptionBudgetAlertActionConfig | undefined, created_at?: string | undefined, default_priority?: number | undefined, name: string, notification_policy_slug?: string | undefined, partition_slug_path: string, priorities?: Array | undefined, resource?: ConsumptionBudgetResourceGroup | undefined, slug?: string | undefined, thresholds?: Array | undefined, updated_at?: string | undefined}; export type ConfigV1UpdateConsumptionBudgetBody = {consumption_budget?: Configv1ConsumptionBudget | undefined, create_if_missing?: boolean | undefined, dry_run?: boolean | undefined}; export type UpdateApiApiV1ConfigConsumptionBudgetsSlugArgs = {slug: string, body: ConfigV1UpdateConsumptionBudgetBody}; export type UpdateApiApiV1ConfigConsumptionBudgetsSlugOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const consumptionBudgetAlertActionConfig = z.object({annotations: z.record(z.string(), z.string()).optional(), instant_rate_sustain_secs: z.number().int().optional(), labels: z.record(z.string(), z.string()).optional()}); export const configv1LogSearchFilter = z.object({query: z.string().optional()}); export const consumptionBudgetPriorityFilter = z.object({dataset_slug: z.string().optional(), log_filter: configv1LogSearchFilter.optional()}); export const consumptionBudgetPriority = z.object({filters: z.array(consumptionBudgetPriorityFilter), priority: z.number().int()}); export const consumptionBudgetResourceGroup = z.enum(["LOG_PERSISTED_BYTES", "LOG_PROCESSED_BYTES", "METRIC_PERSISTED_SERIES", "METRIC_ALL", "LOG_ALL", "TRACE_PROCESSED_BYTES", "TRACE_PERSISTED_BYTES", "TRACE_ALL", "ALL"]); export const consumptionBudgetThresholdAction = z.enum(["ALERT_WARN", "ALERT_CRITICAL", "DROP"]); export const thresholdInstantRate = z.object({fixed_value_per_sec: z.string()}); export const consumptionBudgetThresholdType = z.enum(["DAILY_VOLUME", "INSTANT_RATE", "WEEKLY_VOLUME", "MONTHLY_VOLUME", "HOURLY_VOLUME", "ROLLING_1_HOUR_VOLUME", "ROLLING_3_HOUR_VOLUME", "ROLLING_1_DAY_VOLUME", "ROLLING_7_DAY_VOLUME", "ROLLING_30_MINUTE_VOLUME"]); export const consumptionBudgetUnit = z.enum(["NATIVE", "CREDITS"]); export const thresholdVolume = z.object({fixed_value: z.string()}); export const configv1ConsumptionBudgetThreshold = z.object({action: consumptionBudgetThresholdAction, instant_rate: thresholdInstantRate.optional(), resource_group: consumptionBudgetResourceGroup.optional(), type: consumptionBudgetThresholdType, unit: consumptionBudgetUnit.optional(), volume: thresholdVolume.optional()}); export const configv1ConsumptionBudget = z.object({alert_action_config: consumptionBudgetAlertActionConfig.optional(), created_at: z.string().optional(), default_priority: z.number().int().optional(), name: z.string(), notification_policy_slug: z.string().optional(), partition_slug_path: z.string(), priorities: z.array(consumptionBudgetPriority).optional(), resource: consumptionBudgetResourceGroup.optional(), slug: z.string().optional(), thresholds: z.array(configv1ConsumptionBudgetThreshold).optional(), updated_at: z.string().optional()}); export const configv1UpdateConsumptionBudgetResponse = z.object({consumption_budget: configv1ConsumptionBudget.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 }; /** * PUT /api/v1/config/consumption-budgets/{slug} */ export const updateApiApiV1ConfigConsumptionBudgetsSlug = async (args: UpdateApiApiV1ConfigConsumptionBudgetsSlugArgs, options?: UpdateApiApiV1ConfigConsumptionBudgetsSlugOptions) => { 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/consumption-budgets/${encodeURIComponent(String(args.slug))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(configv1UpdateConsumptionBudgetResponse, await res.json()) };