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 ProfilesDiffBody = {baseline: ProfilesQueryBody, comparison: ProfilesQueryBody, max_nodes?: (number | null) | undefined}; export type CreateApiApiOrgIdStreamNameProfilesDiffArgs = {org_id: string, stream_name: string, body: ProfilesDiffBody}; export type CreateApiApiOrgIdStreamNameProfilesDiffOptions = {baseUrl: string, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const profilesDiffResponse = z.object({baseline_total: z.number().int(), comparison_total: z.number().int(), profile_type: z.string(), root: z.object({}), took: z.number().int().gte(0), top: z.array(z.object({})), tree_truncated: z.boolean(), truncated: z.boolean(), 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 }; /** * Compare two profile queries as one call tree * * POST /api/{org_id}/{stream_name}/profiles/diff */ export const createApiApiOrgIdStreamNameProfilesDiff = async (args: CreateApiApiOrgIdStreamNameProfilesDiffArgs, options: CreateApiApiOrgIdStreamNameProfilesDiffOptions) => { 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/diff${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(profilesDiffResponse, await res.json()) };