import {z} from 'zod' export type GetApiApiProjectIdClassifyV2SampleIdRawDataWindowIndexArgs = {projectId: number, sampleId: number, windowIndex: number, datastreamIndex?: number | undefined, impulseId?: number | undefined, truncateStructuredLabels?: boolean | undefined}; export type GetApiApiProjectIdClassifyV2SampleIdRawDataWindowIndexOptions = {baseUrl?: string | undefined, ApiKeyAuthentication?: string | undefined, fetch?: typeof fetch | undefined}; export const rawDataCategory = z.enum(["training", "testing", "validation", "post-processing"]); export const sensor = z.object({index: z.number().int(), name: z.string(), units: z.string()}); export const boundingBox = z.object({label: z.string(), x: z.number().int(), y: z.number().int(), width: z.number().int(), height: z.number().int()}); export const projectLabelingMethod = z.enum(["single_label", "object_detection", "label_map"]); export const sampleLabelMapLabels = z.object({type: z.literal("key-values"), labels: z.record(z.string(), z.string())}); export const structuredLabel = z.object({startIndex: z.number().int(), endIndex: z.number().int(), label: z.string(), labelMap: sampleLabelMapLabels.optional()}); export const lastUpdatedByUser = z.object({type: z.literal("user"), id: z.number().int()}); export const lastUpdatedByProjectApiKey = z.object({type: z.literal("projectApiKey"), id: z.number().int()}); export const lastUpdatedByOAuthClient = z.object({type: z.literal("oauthClient"), oauthClientId: z.string(), userId: z.number().int().optional()}); export const lastUpdatedByDatasetVersionRestore = z.object({type: z.literal("datasetVersionRestore"), datasetVersionId: z.number().int(), groupKey: z.string(), actor: z.union([z.object({type: z.literal("user"), id: z.number().int()}), z.object({type: z.literal("projectApiKey"), id: z.number().int()}), z.object({type: z.literal("oauthClient"), oauthClientId: z.string(), userId: z.number().int().optional()})]).optional()}); export const sampleDatastream = z.object({index: z.number().int(), chartType: z.enum(["chart", "image", "video", "table"]), intervalMs: z.number(), frequencyHz: z.number(), sensors: z.array(sensor), valuesCount: z.number().int(), imageDimensions: z.object({width: z.number().int(), height: z.number().int()}).optional(), totalLengthMs: z.number()}); export const sample = z.object({id: z.number().int(), filename: z.string(), signatureValidate: z.boolean(), signatureMethod: z.string().optional(), signatureKey: z.string().optional(), created: z.string(), lastModified: z.string(), category: rawDataCategory, coldstorageFilename: z.string(), label: z.string(), intervalMs: z.number(), frequency: z.number(), originalIntervalMs: z.number(), originalFrequency: z.number(), deviceName: z.string().optional(), deviceType: z.string(), sensors: z.array(sensor), valuesCount: z.number().int(), totalLengthMs: z.number().optional(), added: z.string(), boundingBoxes: z.array(boundingBox), boundingBoxesType: z.enum(["object_detection", "constrained_object_detection"]), chartType: z.enum(["chart", "image", "video", "table"]), thumbnailVideo: z.string().optional(), thumbnailVideoFull: z.string().optional(), isDisabled: z.boolean(), isProcessing: z.boolean(), processingJobId: z.number().int().optional(), processingError: z.boolean(), processingErrorString: z.string().optional(), isCropped: z.boolean(), metadata: z.record(z.string(), z.string()).optional(), projectId: z.number().int(), projectOwnerName: z.string().optional(), projectName: z.string().optional(), projectLabelingMethod: projectLabelingMethod.optional(), sha256Hash: z.string(), structuredLabels: z.array(structuredLabel).optional(), structuredLabelsList: z.array(z.string()).optional(), createdBySyntheticDataJobId: z.number().int().optional(), imageDimensions: z.object({width: z.number().int(), height: z.number().int()}).optional(), videoUrl: z.string().optional(), videoUrlFull: z.string().optional(), labelMap: sampleLabelMapLabels.optional(), lastUpdatedBy: z.discriminatedUnion("type", [lastUpdatedByUser, lastUpdatedByProjectApiKey, lastUpdatedByOAuthClient, lastUpdatedByDatasetVersionRestore]).optional(), datastreams: z.array(sampleDatastream)}); export const rawSamplePayload = z.object({device_name: z.string().optional(), device_type: z.string(), sensors: z.array(sensor), values: z.array(z.array(z.number())), cropStart: z.number().int().optional(), cropEnd: z.number().int().optional(), intervalMs: z.number(), frequencyHz: z.number()}); export const getSampleDspResponse = z.object({success: z.boolean(), error: z.string().optional(), sample: sample, payload: rawSamplePayload, totalPayloadLength: z.number().int()}); 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 a window of raw sample features from cache, after a live classification job has completed. * * GET /api/{projectId}/classify/v2/{sampleId}/raw-data/{windowIndex} * * Also accepts auth: JWTAuthentication, JWTHttpHeaderAuthentication, OAuth2 */ export const getApiApiProjectIdClassifyV2SampleIdRawDataWindowIndex = async (args: GetApiApiProjectIdClassifyV2SampleIdRawDataWindowIndexArgs, options?: GetApiApiProjectIdClassifyV2SampleIdRawDataWindowIndexOptions) => { const {baseUrl = "https://studio.edgeimpulse.com/v1", fetch: fetchFn = fetch, ApiKeyAuthentication} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.datastreamIndex != null) query.append("datastreamIndex", String(args.datastreamIndex)) if (args.impulseId != null) query.append("impulseId", String(args.impulseId)) if (args.truncateStructuredLabels != null) query.append("truncateStructuredLabels", String(args.truncateStructuredLabels)) if (ApiKeyAuthentication !== undefined) headers.set("x-api-key", ApiKeyAuthentication) const url = `${baseUrl.replace(/\/+$/, '')}/api/${encodeURIComponent(String(args.projectId))}/classify/v2/${encodeURIComponent(String(args.sampleId))}/raw-data/${encodeURIComponent(String(args.windowIndex))}${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(getSampleDspResponse, await res.json()) };