import {z} from 'zod' export type GetApiMonitoringMetricsMetricCurrentArgs = {metric: 'connections' | 'totalConnections' | 'numberOfSubscriptions' | 'sentMessages' | 'sentBytes' | 'receivedMessages' | 'receivedBytes' | 'slowConsumers' | 'memory' | 'storage' | 'accounts' | 'haAssets' | 'apiTotal' | 'apiErrors' | 'apiInflight' | 'streams' | 'consumers' | 'totalMessages' | 'totalBytes' | 'numberOfSubjects' | 'numberOfDeleted' | 'lostMessages' | 'lostBytes' | 'numberOfAckPending' | 'numberOfRedelivered' | 'numberOfWaiting' | 'numberOfPending' | 'pushBound' | 'paused' | 'deliveredConsumerSequence' | 'deliveredStreamSequence' | 'deliveredLastActive' | 'ackFloorConsumerSequence' | 'ackFloorStreamSequence' | 'ackFloorLastActive' | 'ram' | 'cpu' | 'slowConsumersClients' | 'slowConsumersRoutes' | 'slowConsumersGateways' | 'slowConsumersMeshs' | 'sentMessagesRate' | 'sentBytesRate' | 'receivedMessagesRate' | 'receivedBytesRate', node?: string | undefined, account?: string | undefined, stream?: string | undefined, consumer?: string | undefined}; export type GetApiMonitoringMetricsMetricCurrentOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const metricPoint = z.object({ts: z.number().int().optional(), value: z.number().optional()}); export const currentMetricResponse = z.object({message: z.string().optional(), data: metricPoint.optional(), metadata: z.record(z.string(), z.unknown()).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 }; /** * Get current metric value * * GET /monitoring/metrics/{metric}/current * * Also accepts auth: apiKeyAuth */ export const getApiMonitoringMetricsMetricCurrent = async (args: GetApiMonitoringMetricsMetricCurrentArgs, options?: GetApiMonitoringMetricsMetricCurrentOptions) => { const {baseUrl = "https://api.quiva.ai", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.node != null) query.append("node", String(args.node)) if (args.account != null) query.append("account", String(args.account)) if (args.stream != null) query.append("stream", String(args.stream)) if (args.consumer != null) query.append("consumer", String(args.consumer)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/monitoring/metrics/${encodeURIComponent(String(args.metric))}/current${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(currentMetricResponse, await res.json()) };