import {z} from 'zod' export type GetApiPipelinesPipelineIdMetricsArgs = {pipeline_id: string, time_interval?: number | undefined, time_unit?: ('minute' | 'hour' | 'day' | 'week' | 'month') | undefined, timestamp_from?: (string | null) | undefined, timestamp_to?: (string | null) | undefined}; export type GetApiPipelinesPipelineIdMetricsOptions = {baseUrl?: string | undefined, FronteggHTTPAuthentication?: string | undefined, fetch?: typeof fetch | undefined}; export const pipelineLatestMetrics = z.object({latency: z.union([z.number(), z.string()]).nullable().optional(), recordsLag: z.union([z.number().int(), z.string()]).nullable().optional(), status: z.string().nullable().optional()}).and(z.record(z.string(), z.unknown())); export const pipelineLatestMetricsWithMetadata = z.object({data: pipelineLatestMetrics, metadata: z.record(z.string(), z.record(z.string(), z.string()))}).and(z.record(z.string(), z.unknown())); 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 Metrics For Pipeline * * GET /pipelines/{pipeline_id}/metrics */ export const getApiPipelinesPipelineIdMetrics = async (args: GetApiPipelinesPipelineIdMetricsArgs, options?: GetApiPipelinesPipelineIdMetricsOptions) => { const {baseUrl = "https://api.streamkap.com", fetch: fetchFn = fetch, FronteggHTTPAuthentication} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.time_interval != null) query.append("time_interval", String(args.time_interval)) if (args.time_unit != null) query.append("time_unit", String(args.time_unit)) if (args.timestamp_from != null) query.append("timestamp_from", String(args.timestamp_from)) if (args.timestamp_to != null) query.append("timestamp_to", String(args.timestamp_to)) if (FronteggHTTPAuthentication !== undefined) headers.set('authorization', `Bearer ${FronteggHTTPAuthentication}`) const url = `${baseUrl.replace(/\/+$/, '')}/pipelines/${encodeURIComponent(String(args.pipeline_id))}/metrics${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(pipelineLatestMetricsWithMetadata, await res.json()) };