import {z} from 'zod' export type MetricsAggregationMethods = 'AVG' | 'COUNT' | 'MIN' | 'MAX' | 'SUM' | 'LAST'; export type MetricsMetricSeriesType = 'TIMESERIES' | 'SCALAR'; export type MetricsMeasurementsFillingMethod = 'NONE' | 'ZERO_FILL' | 'NULL_FILL' | 'LAST_VALUE_FILL'; export type MetricsMeasurementsRequest = {id?: string | undefined, name: string, filter?: string | undefined, groupBy?: Array | undefined, aggregateBy?: MetricsAggregationMethods | undefined, preGroupBy?: Array | undefined, preGroupByMethod?: MetricsAggregationMethods | undefined, seriesType?: MetricsMetricSeriesType | undefined, fillMethod?: MetricsMeasurementsFillingMethod | undefined, fillIfEmpty?: boolean | undefined}; export type CreateApiV1MetricsMeasurementsArgs = {forcePositional?: boolean | undefined, pageSize?: number | undefined, skipToken?: string | undefined, body: {metrics: Array, startTime?: string | undefined, endTime?: string | undefined}}; export type CreateApiV1MetricsMeasurementsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const commonKeyValuePair = z.object({key: z.string(), value: z.string()}); export const metricsMeasurement = z.object({time: z.string(), value: z.number().nullable()}); export const metricsGrouping = z.object({attributes: z.array(commonKeyValuePair), measurements: z.array(metricsMeasurement)}); export const commonPageInfo = z.object({prevPage: z.string(), nextPage: z.string()}); export const createApiV1MetricsMeasurementsResponse = z.object({metrics: z.array(z.object({id: z.string().optional(), name: z.string(), groupings: z.array(metricsGrouping), bucketSizeInSeconds: z.number().int()})), pageInfo: commonPageInfo}); 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 measurements for a batch of metrics * * POST /v1/metrics/measurements */ export const createApiV1MetricsMeasurements = async (args: CreateApiV1MetricsMeasurementsArgs, options?: CreateApiV1MetricsMeasurementsOptions) => { const {baseUrl = "https://api.na-01.cloud.solarwinds.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.forcePositional != null) query.append("forcePositional", String(args.forcePositional)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.skipToken != null) query.append("skipToken", String(args.skipToken)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/metrics/measurements${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(createApiV1MetricsMeasurementsResponse, await res.json()) };