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 GetApiApiOrgIdStreamNameProfilesSeriesArgs = {org_id: string, stream_name: string, start_time: number, end_time: number, data_source?: string | undefined, service_name?: string | undefined, profile_type?: string | undefined, profile_unit?: string | undefined, filters?: string | undefined, step?: string | undefined, group_by?: string | undefined, group_limit?: number | undefined, timeout?: number | undefined, body: ProfilesQueryBody}; export type GetApiApiOrgIdStreamNameProfilesSeriesOptions = {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 (GET) * * GET /api/{org_id}/{stream_name}/profiles/series */ export const getApiApiOrgIdStreamNameProfilesSeries = async (args: GetApiApiOrgIdStreamNameProfilesSeriesArgs, options: GetApiApiOrgIdStreamNameProfilesSeriesOptions) => { 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 (args.start_time != null) query.append("start_time", String(args.start_time)) if (args.end_time != null) query.append("end_time", String(args.end_time)) if (args.data_source != null) query.append("data_source", String(args.data_source)) if (args.service_name != null) query.append("service_name", String(args.service_name)) if (args.profile_type != null) query.append("profile_type", String(args.profile_type)) if (args.profile_unit != null) query.append("profile_unit", String(args.profile_unit)) if (args.filters != null) query.append("filters", String(args.filters)) if (args.step != null) query.append("step", String(args.step)) if (args.group_by != null) query.append("group_by", String(args.group_by)) if (args.group_limit != null) query.append("group_limit", String(args.group_limit)) if (args.timeout != null) query.append("timeout", String(args.timeout)) 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: "GET", 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()) };