import {z} from 'zod' export type DerivedMetricVariable = {default_prometheus_selector: string, name: string}; export type DerivedMetricQuery = {prometheus_expr: string, variables?: Array | undefined}; export type Configv1DerivedMetricLabelMatcherMatcherType = 'EXACT'; export type Configv1DerivedMetricLabelMatcher = {name?: string | undefined, type?: Configv1DerivedMetricLabelMatcherMatcherType | undefined, value?: string | undefined}; export type DerivedMetricSelector = {labels?: Array | undefined}; export type DerivedMetricSelectorQuery = {query: DerivedMetricQuery, selector?: DerivedMetricSelector | undefined}; export type Configv1DerivedMetric = {created_at?: string | undefined, description?: string | undefined, metric_name: string, name: string, queries: Array, slug?: string | undefined, updated_at?: string | undefined}; export type ConfigV1UpdateDerivedMetricBody = {create_if_missing?: boolean | undefined, derived_metric?: Configv1DerivedMetric | undefined, dry_run?: boolean | undefined}; export type UpdateApiApiV1ConfigDerivedMetricsSlugArgs = {slug: string, body: ConfigV1UpdateDerivedMetricBody}; export type UpdateApiApiV1ConfigDerivedMetricsSlugOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const derivedMetricVariable = z.object({default_prometheus_selector: z.string(), name: z.string()}); export const derivedMetricQuery = z.object({prometheus_expr: z.string(), variables: z.array(derivedMetricVariable).optional()}); export const configv1DerivedMetricLabelMatcherMatcherType = z.literal("EXACT"); export const configv1DerivedMetricLabelMatcher = z.object({name: z.string().optional(), type: configv1DerivedMetricLabelMatcherMatcherType.optional(), value: z.string().optional()}); export const derivedMetricSelector = z.object({labels: z.array(configv1DerivedMetricLabelMatcher).optional()}); export const derivedMetricSelectorQuery = z.object({query: derivedMetricQuery, selector: derivedMetricSelector.optional()}); export const configv1DerivedMetric = z.object({created_at: z.string().optional(), description: z.string().optional(), metric_name: z.string(), name: z.string(), queries: z.array(derivedMetricSelectorQuery), slug: z.string().optional(), updated_at: z.string().optional()}); export const configv1UpdateDerivedMetricResponse = z.object({derived_metric: configv1DerivedMetric.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/derived-metrics/{slug} */ export const updateApiApiV1ConfigDerivedMetricsSlug = async (args: UpdateApiApiV1ConfigDerivedMetricsSlugArgs, options?: UpdateApiApiV1ConfigDerivedMetricsSlugOptions) => { 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/derived-metrics/${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(configv1UpdateDerivedMetricResponse, await res.json()) };