import {z} from 'zod' export type ProfileFilter = {key: string, op?: string | undefined, value: string}; export type ProfilesQueryBody = {data_source?: (string | null) | undefined, end_time?: number | undefined, filters?: Array | undefined, group_by?: (string | null) | undefined, group_limit?: (number | null) | undefined, max_nodes?: (number | null) | undefined, profile_type?: (string | null) | undefined, profile_unit?: (string | null) | undefined, service_name?: (string | null) | undefined, start_time?: number | undefined, step?: (string | null) | undefined, timeout?: (number | null) | undefined}; export type CreateApiApiOrgIdStreamNameProfilesSeriesArgs = {org_id: string, stream_name: string, body: ProfilesQueryBody}; export type CreateApiApiOrgIdStreamNameProfilesSeriesOptions = {baseUrl: string, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const profilesSeriesPoint = z.object({rate: z.number().nullable().optional(), timestamp: z.number().int(), value: z.number()}); export const profilesSeriesGroup = z.object({others: z.boolean(), series: z.array(profilesSeriesPoint), total: z.number(), value: z.string()}); export const profilesSeriesResponse = z.object({groups: z.array(profilesSeriesGroup).nullable().optional(), profile_type: z.string(), series: z.array(profilesSeriesPoint), step_secs: z.number().int(), took: z.number().int().gte(0), unit: z.string()}); 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 }; /** * Aggregate profile sample values over time * * POST /api/{org_id}/{stream_name}/profiles/series */ export const createApiApiOrgIdStreamNameProfilesSeries = async (args: CreateApiApiOrgIdStreamNameProfilesSeriesArgs, options: CreateApiApiOrgIdStreamNameProfilesSeriesOptions) => { const {baseUrl, fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Authorization !== undefined) headers.set("Authorization", Authorization) const url = `${baseUrl.replace(/\/+$/, '')}/api/${encodeURIComponent(String(args.org_id))}/${encodeURIComponent(String(args.stream_name))}/profiles/series${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(profilesSeriesResponse, await res.json()) };