import {z} from 'zod' export type AlertType = 'BrokerAlert' | 'ConsumerGroupAlert' | 'DataQualityPolicyAlert' | 'KafkaConnectAlert' | 'TopicAlert'; export type GetApiPublicMonitoringV2ClusterClusterAlertArgs = {cluster: string, connector?: string | undefined, connect?: string | undefined, topic?: string | undefined, consumerGroup?: string | undefined, alertType?: AlertType | undefined}; export type GetApiPublicMonitoringV2ClusterClusterAlertOptions = {baseUrl: string, httpAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const alertKind = z.literal("Alert"); export const alertStatus = z.enum(["Firing", "Healthy", "Pending"]); export const alertMetadataOutput = z.object({name: z.string(), cluster: z.string(), updatedAt: z.string(), updatedBy: z.string(), lastTriggeredAt: z.string().optional(), status: alertStatus}); export const operator = z.enum(["GreaterThan", "GreaterThanOrEqual", "LessThan", "LessThanOrEqual", "NotEqual"]); export const brokerMetricType = z.enum(["MessageIn", "MessageOut", "MessageSize", "OfflinePartitionCount", "PartitionCount", "UnderMinIsrPartitionCount", "UnderReplicatedPartitionCount"]); export const brokerAlertOutputV2 = z.object({threshold: z.number().int(), operator: operator, metric: brokerMetricType, promQl: z.string(), 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 consumerGroupAlertOutputV2 = z.object({consumerGroupName: z.string(), threshold: z.number().int(), operator: operator, metric: consumerGroupMetricType, promQl: z.string(), 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 kafkaConnectAlertOutputV2 = z.object({connectName: z.string(), connectorName: z.string(), metric: kafkaConnectMetricType, threshold: z.number().int(), operator: operator, promQl: z.string(), 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 topicAlertOutputV2 = z.object({topicName: z.string(), threshold: z.number().int(), operator: operator, metric: topicMetricType, promQl: z.string(), description: z.string().optional(), displayName: z.string().optional(), disable: z.boolean().optional(), type: z.literal("TopicAlert")}); export const alertSpecOutputV2 = z.discriminatedUnion("type", [brokerAlertOutputV2, consumerGroupAlertOutputV2, kafkaConnectAlertOutputV2, topicAlertOutputV2]); export const alertOutputResourceV2 = z.object({apiVersion: z.string(), kind: alertKind, metadata: alertMetadataOutput, spec: alertSpecOutputV2}); export const getApiPublicMonitoringV2ClusterClusterAlertResponse = z.array(alertOutputResourceV2); 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 }; /** * List Alert resources * * GET /public/monitoring/v2/cluster/{cluster}/alert */ export const getApiPublicMonitoringV2ClusterClusterAlert = async (args: GetApiPublicMonitoringV2ClusterClusterAlertArgs, options: GetApiPublicMonitoringV2ClusterClusterAlertOptions) => { const {baseUrl, fetch: fetchFn = fetch, httpAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.connector != null) query.append("connector", String(args.connector)) if (args.connect != null) query.append("connect", String(args.connect)) if (args.topic != null) query.append("topic", String(args.topic)) if (args.consumerGroup != null) query.append("consumerGroup", String(args.consumerGroup)) if (args.alertType != null) query.append("alertType", String(args.alertType)) 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: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiPublicMonitoringV2ClusterClusterAlertResponse, await res.json()) };