import {z} from 'zod' export type GetApiApiV2OrgIdAlertsHistoryArgs = {org_id: string, alert_id?: string | undefined, anomaly_id?: string | undefined, start_time?: number | undefined, end_time?: number | undefined, from?: number | undefined, size?: number | undefined, sort_by?: string | undefined, sort_order?: string | undefined}; export type GetApiApiV2OrgIdAlertsHistoryOptions = {baseUrl: string, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const alertHistoryEntry = z.object({ack_action: z.string().nullable().optional(), ack_source: z.string().nullable().optional(), acked_by: z.string().nullable().optional(), actual_value: z.number().nullable().optional(), alert_name: z.string(), anomaly_count: z.number().int().nullable().optional(), delay_in_secs: z.number().int().nullable().optional(), downtime_id: z.string().nullable().optional(), end_time: z.number().int(), error: z.string().nullable().optional(), evaluation_took_in_secs: z.number().nullable().optional(), group_label: z.string().nullable().optional(), group_size: z.number().int().nullable().optional(), grouped: z.boolean().nullable().optional(), is_partial: z.boolean().nullable().optional(), is_realtime: z.boolean(), is_silenced: z.boolean(), level: z.string().nullable().optional(), org: z.string(), query_took: z.number().int().nullable().optional(), repeat_suppressed: z.boolean().nullable().optional(), retries: z.number().int(), source_node: z.string().nullable().optional(), start_time: z.number().int(), status: z.string(), success_response: z.string().nullable().optional(), taken_over_from: z.string().nullable().optional(), threshold_operator: z.string().nullable().optional(), threshold_value: z.number().nullable().optional(), timestamp: z.number().int(), value_is_lower_bound: z.boolean().nullable().optional()}); export const getApiApiV2OrgIdAlertsHistoryResponse = z.object({from: z.number().int(), hits: z.array(alertHistoryEntry), size: z.number().int(), total: z.number().int().gte(0)}); 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 }; /** * Get alert execution history * * GET /api/v2/{org_id}/alerts/history */ export const getApiApiV2OrgIdAlertsHistory = async (args: GetApiApiV2OrgIdAlertsHistoryArgs, options: GetApiApiV2OrgIdAlertsHistoryOptions) => { const {baseUrl, fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.alert_id != null) query.append("alert_id", String(args.alert_id)) if (args.anomaly_id != null) query.append("anomaly_id", String(args.anomaly_id)) 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.from != null) query.append("from", String(args.from)) if (args.size != null) query.append("size", String(args.size)) if (args.sort_by != null) query.append("sort_by", String(args.sort_by)) if (args.sort_order != null) query.append("sort_order", String(args.sort_order)) if (Authorization !== undefined) headers.set("Authorization", Authorization) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/${encodeURIComponent(String(args.org_id))}/alerts/history${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(getApiApiV2OrgIdAlertsHistoryResponse, await res.json()) };