import {z} from 'zod' export type GetApiEventsRoiArgs = {sensorId?: string | undefined, place?: string | undefined, roiId?: string | undefined, fromTimestamp: string, toTimestamp: string, maxResultSize?: number | undefined, objectType?: string | undefined}; export type GetApiEventsRoiOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const locations = z.object({type: z.literal("linestring"), coordinates: z.array(z.array(z.number()))}); export const sensor = z.object({id: z.string().max(10000), description: z.string().max(10000), type: z.string().max(10000), info: z.record(z.string(), z.string().max(10000)).optional()}); export const place = z.object({name: z.string().max(10000), info: z.record(z.string(), z.string().max(10000)).optional()}); export const event = z.object({type: z.string().max(10000), id: z.string().max(10000), info: z.record(z.string(), z.string().max(10000)).optional()}).nullable(); export const bbox = z.object({topY: z.number().gte(-10000).lte(10000), rightX: z.number().gte(-10000).lte(10000), bottomY: z.number().gte(-10000).lte(10000), leftX: z.number().gte(-10000).lte(10000), embeddings: z.array(z.object({vector: z.array(z.number())}).nullable()), confidence: z.number(), info: z.record(z.string(), z.string().max(10000)).optional()}); export const object = z.object({type: z.string().max(10000), location: z.object({lon: z.number().gte(-180).lte(180), lat: z.number().gte(-90).lte(90), alt: z.number().gte(0).lte(100000)}), bbox: bbox, id: z.string().max(10000), speed: z.number().gte(0).lte(100000), coordinate: z.object({x: z.number().gte(-10000).lte(10000), y: z.number().gte(-10000).lte(10000), z: z.number().gte(-10000).lte(10000)}), info: z.record(z.string(), z.string().max(10000)).optional()}); export const analyticsModule = z.object({id: z.string().max(10000).optional(), description: z.string().max(10000).optional(), version: z.string().max(10000).optional(), source: z.string().max(10000).optional(), info: z.record(z.string(), z.string().max(10000)).optional()}); export const behavior = z.object({timestamp: z.string().max(24), Id: z.string().max(10000), id: z.string().max(10000), timeInterval: z.number().gte(0).lte(10000), end: z.string().max(24), locations: locations.optional(), smoothLocations: locations.optional(), length: z.number().int().gte(-10000).lte(10000), speedOverTime: z.array(z.number().gte(-100000).lte(100000)), sensor: sensor, place: place, event: event.optional(), object: object, edges: z.array(z.string().max(10000)).optional(), analyticsModule: analyticsModule.optional(), direction: z.enum(["N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW", "Right", "Left", "Up", "Down"]), speed: z.number(), distance: z.number(), bearing: z.number(), videoPath: z.string().max(10000)}); export const roiEvents = z.object({roiEvents: z.array(behavior)}); 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 ROI events * * GET /events/roi */ export const getApiEventsRoi = async (args: GetApiEventsRoiArgs, options?: GetApiEventsRoiOptions) => { const {baseUrl = "https://ai.nvidia.com/api/v3.3/analytics", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.sensorId != null) query.append("sensorId", String(args.sensorId)) if (args.place != null) query.append("place", String(args.place)) if (args.roiId != null) query.append("roiId", String(args.roiId)) if (args.fromTimestamp != null) query.append("fromTimestamp", String(args.fromTimestamp)) if (args.toTimestamp != null) query.append("toTimestamp", String(args.toTimestamp)) if (args.maxResultSize != null) query.append("maxResultSize", String(args.maxResultSize)) if (args.objectType != null) query.append("objectType", String(args.objectType)) const url = `${baseUrl.replace(/\/+$/, '')}/events/roi${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(roiEvents, await res.json()) };