import {z} from 'zod' export type AlertKind = 'Alert'; export type AlertMetadata = {name: string, cluster: string}; export type Operator = 'GreaterThan' | 'GreaterThanOrEqual' | 'LessThan' | 'LessThanOrEqual' | 'NotEqual'; export type BrokerMetricType = 'MessageIn' | 'MessageOut' | 'MessageSize' | 'OfflinePartitionCount' | 'PartitionCount' | 'UnderMinIsrPartitionCount' | 'UnderReplicatedPartitionCount'; export type BrokerAlertV2 = {threshold: number, operator: Operator, metric: BrokerMetricType, description?: string | undefined, displayName?: string | undefined, disable?: boolean | undefined, type: 'BrokerAlert'}; export type ConsumerGroupMetricType = 'OffsetLag' | 'TimeLag'; export type ConsumerGroupAlertV2 = {consumerGroupName: string, threshold: number, operator: Operator, metric: ConsumerGroupMetricType, description?: string | undefined, displayName?: string | undefined, disable?: boolean | undefined, topicName?: string | undefined, type: 'ConsumerGroupAlert'}; export type KafkaConnectMetricType = 'FailedTaskCount'; export type KafkaConnectAlertV2 = {connectName: string, connectorName: string, threshold: number, operator: Operator, metric: KafkaConnectMetricType, description?: string | undefined, displayName?: string | undefined, disable?: boolean | undefined, type: 'KafkaConnectAlert'}; export type TopicMetricType = 'MessageCount' | 'MessageIn' | 'MessageOut' | 'MessageSize'; export type TopicAlertV2 = {topicName: string, threshold: number, operator: Operator, metric: TopicMetricType, description?: string | undefined, displayName?: string | undefined, disable?: boolean | undefined, type: 'TopicAlert'}; export type AlertSpecV2 = BrokerAlertV2 | ConsumerGroupAlertV2 | KafkaConnectAlertV2 | TopicAlertV2; export type AlertResourceV2 = {apiVersion: string, kind: AlertKind, metadata: AlertMetadata, spec: AlertSpecV2}; export type CreateApiPublicMonitoringV2ClusterClusterAlertArgs = {cluster: string, body: AlertResourceV2}; export type CreateApiPublicMonitoringV2ClusterClusterAlertOptions = {baseUrl: string, httpAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const alertKind = z.literal("Alert"); export const alertMetadata = z.object({name: z.string(), cluster: z.string()}); export const operator = z.enum(["GreaterThan", "GreaterThanOrEqual", "LessThan", "LessThanOrEqual", "NotEqual"]); export const brokerMetricType = z.enum(["MessageIn", "MessageOut", "MessageSize", "OfflinePartitionCount", "PartitionCount", "UnderMinIsrPartitionCount", "UnderReplicatedPartitionCount"]); export const brokerAlertV2 = z.object({threshold: z.number().int(), operator: operator, metric: brokerMetricType, description: z.string().optional(), displayName: z.string().optional(), disable: z.boolean().optional(), type: z.literal("BrokerAlert")}); export const consumerGroupMetricType = z.enum(["OffsetLag", "TimeLag"]); export const consumerGroupAlertV2 = z.object({consumerGroupName: z.string(), threshold: z.number().int(), operator: operator, metric: consumerGroupMetricType, description: z.string().optional(), displayName: z.string().optional(), disable: z.boolean().optional(), topicName: z.string().optional(), type: z.literal("ConsumerGroupAlert")}); export const kafkaConnectMetricType = z.literal("FailedTaskCount"); export const kafkaConnectAlertV2 = z.object({connectName: z.string(), connectorName: z.string(), threshold: z.number().int(), operator: operator, metric: kafkaConnectMetricType, description: z.string().optional(), displayName: z.string().optional(), disable: z.boolean().optional(), type: z.literal("KafkaConnectAlert")}); export const topicMetricType = z.enum(["MessageCount", "MessageIn", "MessageOut", "MessageSize"]); export const topicAlertV2 = z.object({topicName: z.string(), threshold: z.number().int(), operator: operator, metric: topicMetricType, description: z.string().optional(), displayName: z.string().optional(), disable: z.boolean().optional(), type: z.literal("TopicAlert")}); export const alertSpecV2 = z.discriminatedUnion("type", [brokerAlertV2, consumerGroupAlertV2, kafkaConnectAlertV2, topicAlertV2]); export const alertResourceV2 = z.object({apiVersion: z.string(), kind: alertKind, metadata: alertMetadata, spec: alertSpecV2}); export const createResult = z.enum(["AlreadyExist", "Created"]); export const alertCreateResultV2 = z.object({resource: alertResourceV2, createResult: createResult}); 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 a Alert * * POST /public/monitoring/v2/cluster/{cluster}/alert */ export const createApiPublicMonitoringV2ClusterClusterAlert = async (args: CreateApiPublicMonitoringV2ClusterClusterAlertArgs, options: CreateApiPublicMonitoringV2ClusterClusterAlertOptions) => { const {baseUrl, fetch: fetchFn = fetch, httpAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (httpAuth !== undefined) headers.set('authorization', `Bearer ${httpAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/public/monitoring/v2/cluster/${encodeURIComponent(String(args.cluster))}/alert${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(alertCreateResultV2, await res.json()) };