import {z} from 'zod' export type AlertGroupRef = {alert_id: string, group_key?: string | undefined, group_labels?: ({[key: string]: string} | null) | undefined}; export type LogicalOp = 'and' | 'or'; export type PairOperator = '=' | '!='; export type DimensionCondition = {items: Array>, op: LogicalOp, type: 'group'} | {key: string, operator: PairOperator, type: 'pair', value: string}; export type EndOnClear = {alert_id: string, group_key?: (string | null) | undefined}; export type NotificationEvents = {cancelled?: boolean | undefined, ended?: boolean | undefined, ending_soon?: boolean | undefined, extended?: boolean | undefined, started?: boolean | undefined}; export type DowntimeNotifications = {continues?: (string | null) | undefined, destinations?: Array | undefined, ending_soon_lead_secs?: number | undefined, events?: NotificationEvents | undefined}; export type Repeat = 'none' | 'daily' | 'weekly'; export type DowntimeSchedule = {duration_secs: number, ends_at?: (number | null) | undefined, repeat: Repeat, start_time_local?: (string | null) | undefined, starts_at: number, timezone: string, weekdays?: Array | undefined}; export type TargetFolders = {kind: 'all'} | {folder_ids: Array, kind: 'some'}; export type IncidentMode = 'muted' | 'none'; export type TargetModule = 'alerts' | 'anomaly_detections' | 'synthetics' | 'slos'; export type SloCorrectionMode = 'exclude' | 'count_as_good'; export type DowntimeTarget = {folders: TargetFolders, ids?: Array | undefined, incident_mode?: IncidentMode | undefined, module: TargetModule, slo_mode?: (SloCorrectionMode | null) | undefined, tags?: Array | undefined}; export type DowntimeRequest = {alert_groups?: (Array | null) | undefined, condition?: (DimensionCondition | null) | undefined, end_on_clear?: (EndOnClear | null) | undefined, folder_id?: string | undefined, name?: (string | null) | undefined, notifications?: (DowntimeNotifications | null) | undefined, reason?: (string | null) | undefined, schedule: DowntimeSchedule, show_banner?: boolean | undefined, targets: Array, version?: (number | null) | undefined}; export type UpdateApiApiV2OrgIdDowntimesDowntimeIdArgs = {org_id: string, downtime_id: string, body: DowntimeRequest}; export type UpdateApiApiV2OrgIdDowntimesDowntimeIdOptions = {baseUrl: string, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const alertGroupRef = z.object({alert_id: z.string(), group_key: z.string().optional(), group_labels: z.record(z.string(), z.string()).nullable().optional()}); export const logicalOp = z.enum(["and", "or"]); export const pairOperator = z.enum(["=", "!="]); export const dimensionCondition = z.union([z.object({items: z.array(z.object({})), op: logicalOp, type: z.literal("group")}), z.object({key: z.string(), operator: pairOperator, type: z.literal("pair"), value: z.string()})]); export const endOnClear = z.object({alert_id: z.string(), group_key: z.string().nullable().optional()}); export const notificationEvents = z.object({cancelled: z.boolean().optional(), ended: z.boolean().optional(), ending_soon: z.boolean().optional(), extended: z.boolean().optional(), started: z.boolean().optional()}); export const downtimeNotifications = z.object({continues: z.string().nullable().optional(), destinations: z.array(z.string()).optional(), ending_soon_lead_secs: z.number().int().optional(), events: notificationEvents.optional()}); export const repeat = z.enum(["none", "daily", "weekly"]); export const downtimeSchedule = z.object({duration_secs: z.number().int(), ends_at: z.number().int().nullable().optional(), repeat: repeat, start_time_local: z.string().nullable().optional(), starts_at: z.number().int(), timezone: z.string(), weekdays: z.array(z.number().int().gte(0)).optional()}); export const muteSource = z.enum(["slack", "openobserve", "api"]); export const targetFolders = z.union([z.object({kind: z.literal("all")}), z.object({folder_ids: z.array(z.string()), kind: z.literal("some")})]); export const incidentMode = z.enum(["muted", "none"]); export const targetModule = z.enum(["alerts", "anomaly_detections", "synthetics", "slos"]); export const sloCorrectionMode = z.enum(["exclude", "count_as_good"]); export const downtimeTarget = z.object({folders: targetFolders, ids: z.array(z.string()).optional(), incident_mode: incidentMode.optional(), module: targetModule, slo_mode: sloCorrectionMode.nullable().optional(), tags: z.array(z.string()).optional()}); export const downtime = z.object({alert_groups: z.array(alertGroupRef).nullable().optional(), cancelled_at: z.number().int().nullable().optional(), cancelled_by: z.string().nullable().optional(), condition: dimensionCondition.nullable().optional(), created_at: z.number().int(), created_by: z.string(), end_on_clear: endOnClear.nullable().optional(), folder_id: z.string().optional(), id: z.string(), name: z.string(), notifications: downtimeNotifications.nullable().optional(), org: z.string(), origin_region: z.string().nullable().optional(), reason: z.string().nullable().optional(), schedule: downtimeSchedule, show_banner: z.boolean().optional(), source: muteSource.nullable().optional(), targets: z.array(downtimeTarget), updated_at: z.number().int(), updated_by: z.string(), version: z.number().int().optional()}); 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 }; /** * Update a downtime * * PUT /api/v2/{org_id}/downtimes/{downtime_id} */ export const updateApiApiV2OrgIdDowntimesDowntimeId = async (args: UpdateApiApiV2OrgIdDowntimesDowntimeIdArgs, options: UpdateApiApiV2OrgIdDowntimesDowntimeIdOptions) => { 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/v2/${encodeURIComponent(String(args.org_id))}/downtimes/${encodeURIComponent(String(args.downtime_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(downtime, await res.json()) };