import {z} from 'zod' export type Field = {value: string, type: 'func' | 'field' | 'integer' | 'number' | 'regex' | 'wildcard', alias?: string | undefined, args?: Array | undefined}; export type QueryConfig = {id?: string | undefined, database: string, measurement: string, retentionPolicy: string, areTagsAccepted: boolean, rawText?: string | undefined, tags: Record, groupBy: {time: string, tags: Array}, fields: Array, range?: {lower: string, upper: string} | undefined}; export type DBRP = {db: string, rp: string}; export type Rule = {id?: string | undefined, query: QueryConfig, name?: string | undefined, every: string, alertNodes?: ({typeOf?: 'alert' | undefined, stateChangesOnly?: boolean | undefined, noRecoveries?: boolean | undefined, useFlapping?: boolean | undefined, post?: (Array<{url?: string | undefined, headers?: {[key: string]: string} | undefined}>) | undefined, tcp?: (Array<{address?: string | undefined}>) | undefined, email?: (Array<{to?: Array | undefined}>) | undefined, exec?: (Array<{command?: Array | undefined}>) | undefined, log?: (Array<{filePath?: string | undefined}>) | undefined, victorOps?: (Array<{routingKey?: string | undefined}>) | undefined, pagerDuty?: (Array<{serviceKey?: string | undefined}>) | undefined, pagerDuty2?: (Array<{serviceKey?: string | undefined}>) | undefined, pushover?: (Array<{userKey?: string | undefined, device?: string | undefined, title?: string | undefined, url?: string | undefined, urlTitle?: string | undefined, sound?: string | undefined}>) | undefined, sensu?: (Array<{source?: string | undefined, handlers?: Array | undefined}>) | undefined, slack?: (Array<{channel?: string | undefined, username?: string | undefined, iconEmoji?: string | undefined, workspace?: string | undefined}>) | undefined, telegram?: (Array<{chatId?: string | undefined, parseMode?: ('Markdown' | 'HTML') | undefined, disableWebPagePreview?: boolean | undefined, disableNotification?: boolean | undefined}>) | undefined, alerta?: (Array<{token?: string | undefined, resource?: string | undefined, event?: string | undefined, environment?: string | undefined, group?: string | undefined, value?: string | undefined, origin?: string | undefined, service?: Array | undefined}>) | undefined, opsGenie?: (Array<{teams?: Array | undefined, recipients?: Array | undefined}>) | undefined, opsGenie2?: (Array<{teams?: Array | undefined, recipients?: Array | undefined}>) | undefined, talk?: Array> | undefined, kafka?: (Array<{cluster?: string | undefined, 'kafka-topic'?: string | undefined, template?: string | undefined}>) | undefined, serviceNow?: (Array<{source?: string | undefined, node?: string | undefined, type?: string | undefined, resource?: string | undefined, metric_name?: string | undefined, message_key?: string | undefined}>) | undefined, bigPanda?: (Array<{'app-key'?: string | undefined, 'primary-property'?: string | undefined, 'secondary-property'?: string | undefined}>) | undefined, teams?: (Array<{channel_url?: string | undefined}>) | undefined, zenoss?: (Array<{action?: string | undefined, method?: string | undefined, type?: string | undefined, tid?: number | undefined, device?: string | undefined, component?: string | undefined, evclasskey?: string | undefined, evclass?: string | undefined}>) | undefined}) | undefined, message?: string | undefined, details?: string | undefined, trigger: 'deadman' | 'relative' | 'threshold', values?: ({change?: ('% change' | 'change') | undefined, period?: string | undefined, shift?: string | undefined, operator?: ('greater than' | 'less than' | 'equal to or less than' | 'equal to or greater' | 'equal to' | 'not equal to' | 'inside range' | 'outside range') | undefined, value?: string | undefined, rangeValue?: string | undefined}) | undefined, dbrps?: Array | undefined, tickscript?: string | undefined, status?: ('enabled' | 'disabled') | undefined, executing?: boolean | undefined, type?: ('stream' | 'batch') | undefined, error?: string | undefined, created?: string | undefined, modified?: string | undefined, 'last-enabled'?: string | undefined, 'template-id'?: string | undefined, vars?: ({[key: string]: {type?: string | undefined, value?: string | undefined, description?: string | undefined}}) | undefined, links?: ({self: string, kapacitor: string, output?: string | undefined}) | undefined}; export type UpdateApiSourcesIdKapacitorsKapaIdRulesRuleIdArgs = {id: string, kapa_id: string, rule_id: string, body: Rule}; export type UpdateApiSourcesIdKapacitorsKapaIdRulesRuleIdOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const field: z.ZodType = z.object({value: z.string(), type: z.enum(["func", "field", "integer", "number", "regex", "wildcard"]), alias: z.string().optional(), args: z.array(z.lazy(() => field)).optional()}); export const queryConfig = z.object({id: z.string().optional(), database: z.string(), measurement: z.string(), retentionPolicy: z.string(), areTagsAccepted: z.boolean(), rawText: z.string().optional(), tags: z.object({}), groupBy: z.object({time: z.string(), tags: z.array(z.string())}), fields: z.array(field), range: z.object({lower: z.string(), upper: z.string()}).optional()}); export const dBRP = z.object({db: z.string(), rp: z.string()}); export const rule = z.object({id: z.string().optional(), query: queryConfig, name: z.string().optional(), every: z.string(), alertNodes: z.object({typeOf: z.literal("alert").optional(), stateChangesOnly: z.boolean().optional(), noRecoveries: z.boolean().optional(), useFlapping: z.boolean().optional(), post: z.array(z.object({url: z.string().optional(), headers: z.record(z.string(), z.string()).optional()})).optional(), tcp: z.array(z.object({address: z.string().optional()})).optional(), email: z.array(z.object({to: z.array(z.string()).optional()})).optional(), exec: z.array(z.object({command: z.array(z.string()).optional()})).optional(), log: z.array(z.object({filePath: z.string().optional()})).optional(), victorOps: z.array(z.object({routingKey: z.string().optional()})).optional(), pagerDuty: z.array(z.object({serviceKey: z.string().optional()})).optional(), pagerDuty2: z.array(z.object({serviceKey: z.string().optional()})).optional(), pushover: z.array(z.object({userKey: z.string().optional(), device: z.string().optional(), title: z.string().optional(), url: z.string().optional(), urlTitle: z.string().optional(), sound: z.string().optional()})).optional(), sensu: z.array(z.object({source: z.string().optional(), handlers: z.array(z.string()).optional()})).optional(), slack: z.array(z.object({channel: z.string().optional(), username: z.string().optional(), iconEmoji: z.string().optional(), workspace: z.string().optional()})).optional(), telegram: z.array(z.object({chatId: z.string().optional(), parseMode: z.enum(["Markdown", "HTML"]).optional(), disableWebPagePreview: z.boolean().optional(), disableNotification: z.boolean().optional()})).optional(), alerta: z.array(z.object({token: z.string().optional(), resource: z.string().optional(), event: z.string().optional(), environment: z.string().optional(), group: z.string().optional(), value: z.string().optional(), origin: z.string().optional(), service: z.array(z.string()).optional()})).optional(), opsGenie: z.array(z.object({teams: z.array(z.string()).optional(), recipients: z.array(z.string()).optional()})).optional(), opsGenie2: z.array(z.object({teams: z.array(z.string()).optional(), recipients: z.array(z.string()).optional()})).optional(), talk: z.array(z.object({})).optional(), kafka: z.array(z.object({cluster: z.string().optional(), "kafka-topic": z.string().optional(), template: z.string().optional()})).optional(), serviceNow: z.array(z.object({source: z.string().optional(), node: z.string().optional(), type: z.string().optional(), resource: z.string().optional(), metric_name: z.string().optional(), message_key: z.string().optional()})).optional(), bigPanda: z.array(z.object({"app-key": z.string().optional(), "primary-property": z.string().optional(), "secondary-property": z.string().optional()})).optional(), teams: z.array(z.object({channel_url: z.string().optional()})).optional(), zenoss: z.array(z.object({action: z.string().optional(), method: z.string().optional(), type: z.string().optional(), tid: z.number().int().optional(), device: z.string().optional(), component: z.string().optional(), evclasskey: z.string().optional(), evclass: z.string().optional()})).optional()}).optional(), message: z.string().optional(), details: z.string().optional(), trigger: z.enum(["deadman", "relative", "threshold"]), values: z.object({change: z.enum(["% change", "change"]).optional(), period: z.string().optional(), shift: z.string().optional(), operator: z.enum(["greater than", "less than", "equal to or less than", "equal to or greater", "equal to", "not equal to", "inside range", "outside range"]).optional(), value: z.string().optional(), rangeValue: z.string().optional()}).optional(), dbrps: z.array(dBRP).optional(), tickscript: z.string().optional(), status: z.enum(["enabled", "disabled"]).optional(), executing: z.boolean().optional(), type: z.enum(["stream", "batch"]).optional(), error: z.string().optional(), created: z.string().optional(), modified: z.string().optional(), "last-enabled": z.string().optional(), "template-id": z.string().optional(), vars: z.record(z.string(), z.object({type: z.string().optional(), value: z.string().optional(), description: z.string().optional()})).optional(), links: z.object({self: z.string(), kapacitor: z.string(), output: z.string().optional()}).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 }; /** * Update rule alert rule configuration * * PUT /sources/{id}/kapacitors/{kapa_id}/rules/{rule_id} */ export const updateApiSourcesIdKapacitorsKapaIdRulesRuleId = async (args: UpdateApiSourcesIdKapacitorsKapaIdRulesRuleIdArgs, options: UpdateApiSourcesIdKapacitorsKapaIdRulesRuleIdOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/sources/${encodeURIComponent(String(args.id))}/kapacitors/${encodeURIComponent(String(args.kapa_id))}/rules/${encodeURIComponent(String(args.rule_id))}${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(rule, await res.json()) };