import {z} from 'zod' export type MetricsAggregationMethods = 'AVG' | 'COUNT' | 'MIN' | 'MAX' | 'SUM' | 'LAST'; export type MetricsMetricSeriesType = 'TIMESERIES' | 'SCALAR'; export type GetApiV1MetricsNameMeasurementsArgs = {name: string, filter?: string | undefined, groupBy?: string | undefined, aggregateBy?: MetricsAggregationMethods | undefined, preGroupBy?: string | undefined, preGroupByMethod?: MetricsAggregationMethods | undefined, seriesType: MetricsMetricSeriesType, startTime?: string | undefined, endTime?: string | undefined, pageSize?: number | undefined, skipToken?: string | undefined}; export type GetApiV1MetricsNameMeasurementsOptions = {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 getApiV1MetricsNameMeasurementsResponse = z.object({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 metric measurement values, grouped by attributes, filtered by the filter. An empty list indicates no data points are available for the given parameters. * * GET /v1/metrics/{name}/measurements */ export const getApiV1MetricsNameMeasurements = async (args: GetApiV1MetricsNameMeasurementsArgs, options?: GetApiV1MetricsNameMeasurementsOptions) => { const {baseUrl = "https://api.na-01.cloud.solarwinds.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.filter != null) query.append("filter", String(args.filter)) if (args.groupBy != null) query.append("groupBy", String(args.groupBy)) if (args.aggregateBy != null) query.append("aggregateBy", String(args.aggregateBy)) if (args.preGroupBy != null) query.append("preGroupBy", String(args.preGroupBy)) if (args.preGroupByMethod != null) query.append("preGroupByMethod", String(args.preGroupByMethod)) if (args.seriesType != null) query.append("seriesType", String(args.seriesType)) if (args.startTime != null) query.append("startTime", String(args.startTime)) if (args.endTime != null) query.append("endTime", String(args.endTime)) 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/${encodeURIComponent(String(args.name))}/measurements${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(getApiV1MetricsNameMeasurementsResponse, await res.json()) };