init
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
/**
|
||||
* Core CDF Data Modeling Types
|
||||
* These are the base types used across the application for working with CDF data models.
|
||||
* They represent the raw structure of data returned from CDF APIs.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Reference to a view in the data model
|
||||
*/
|
||||
export type ViewId = {
|
||||
space: string;
|
||||
externalId: string;
|
||||
version: string;
|
||||
};
|
||||
|
||||
/**
|
||||
* Base interface for CDF nodes representing the raw structure from CDF APIs.
|
||||
* CDF nests properties as: properties[space][view/version]
|
||||
*/
|
||||
export interface CDFNode {
|
||||
space: string;
|
||||
externalId: string;
|
||||
properties: Record<string, unknown>;
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
/**
|
||||
* Data Mapper Utilities
|
||||
* Unwraps CDF's nested property structure to flat application types
|
||||
*/
|
||||
|
||||
import type { CDFNode, ViewId } from './cdf-types';
|
||||
|
||||
/**
|
||||
* Unwraps properties from a CDF node for a specific view.
|
||||
* CDF nests properties as: properties[space][view/version]
|
||||
* This function flattens them to: { space, externalId, ...properties }
|
||||
*
|
||||
* @example
|
||||
* const batch = unwrapProperties<Batch>(cdfBatch, BATCH_VIEW);
|
||||
* const material = unwrapProperties<Material>(cdfMaterial, MATERIAL_VIEW);
|
||||
*/
|
||||
export function unwrapProperties<
|
||||
T extends { space: string; externalId: string },
|
||||
>(node: CDFNode, view: ViewId): T {
|
||||
const props: Record<string, unknown> = {};
|
||||
|
||||
if (node.properties) {
|
||||
const spaceProps = node.properties[view.space] as
|
||||
| Record<string, unknown>
|
||||
| undefined;
|
||||
if (spaceProps) {
|
||||
const viewKey = `${view.externalId}/${view.version}`;
|
||||
const viewProps = spaceProps[viewKey] as
|
||||
| Record<string, unknown>
|
||||
| undefined;
|
||||
if (viewProps) {
|
||||
Object.assign(props, viewProps);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
space: node.space,
|
||||
externalId: node.externalId,
|
||||
...props,
|
||||
} as T;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unwraps an array of CDF nodes
|
||||
*
|
||||
* @example
|
||||
* const batches = unwrapPropertiesArray<Batch>(cdfBatches, BATCH_VIEW);
|
||||
*/
|
||||
export function unwrapPropertiesArray<
|
||||
T extends { space: string; externalId: string },
|
||||
>(nodes: CDFNode[], view: ViewId): T[] {
|
||||
return nodes.map((node) => unwrapProperties<T>(node, view));
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
/**
|
||||
* Execute async callbacks in parallel with a controlled concurrency limit.
|
||||
* Prevents overwhelming the API while maximizing throughput.
|
||||
*/
|
||||
export async function executeParallel<T>(
|
||||
callbacks: Array<() => Promise<T>>,
|
||||
maxParallel: number
|
||||
): Promise<Array<T | undefined>> {
|
||||
const results: Array<T | undefined> = new Array(callbacks.length);
|
||||
let nextIndex = 0;
|
||||
|
||||
async function runNext(): Promise<void> {
|
||||
const currentIndex = nextIndex++;
|
||||
if (currentIndex >= callbacks.length) return;
|
||||
|
||||
try {
|
||||
results[currentIndex] = await callbacks[currentIndex]();
|
||||
} catch (error) {
|
||||
console.error(`executeParallel: callback ${currentIndex} failed:`, error);
|
||||
results[currentIndex] = undefined;
|
||||
}
|
||||
|
||||
await runNext();
|
||||
}
|
||||
|
||||
await Promise.all(
|
||||
Array.from({ length: Math.min(maxParallel, callbacks.length) }, () => runNext())
|
||||
);
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Split an array into smaller arrays of the given size.
|
||||
*/
|
||||
export function chunk<T>(array: T[], size: number): T[][] {
|
||||
const chunks: T[][] = [];
|
||||
for (let i = 0; i < array.length; i += size) {
|
||||
chunks.push(array.slice(i, i + size));
|
||||
}
|
||||
return chunks;
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
/**
|
||||
* Core Data Model view constants
|
||||
* These reference the standard Cognite Core Data Model views
|
||||
*/
|
||||
|
||||
import { ViewId } from './cdf-types';
|
||||
|
||||
/**
|
||||
* Core Data Model space constant
|
||||
*/
|
||||
export const CORE_DM_SPACE = 'cdf_cdm';
|
||||
|
||||
/**
|
||||
* Core Data Model version
|
||||
*/
|
||||
export const CORE_DM_VERSION = 'v1';
|
||||
|
||||
/**
|
||||
* View reference for CogniteAsset entities (from Core Data Model)
|
||||
*/
|
||||
export const ASSET_VIEW: ViewId = {
|
||||
space: CORE_DM_SPACE,
|
||||
externalId: 'CogniteAsset',
|
||||
version: CORE_DM_VERSION,
|
||||
};
|
||||
|
||||
/**
|
||||
* View reference for Cognite3DObject entities (from Core Data Model)
|
||||
*/
|
||||
export const COGNITE_3D_OBJECT_VIEW: ViewId = {
|
||||
space: CORE_DM_SPACE,
|
||||
externalId: 'Cognite3DObject',
|
||||
version: CORE_DM_VERSION,
|
||||
};
|
||||
|
||||
/**
|
||||
* View reference for CogniteCADNode entities (from Core Data Model)
|
||||
*/
|
||||
export const COGNITE_CAD_NODE_VIEW: ViewId = {
|
||||
space: CORE_DM_SPACE,
|
||||
externalId: 'CogniteCADNode',
|
||||
version: CORE_DM_VERSION,
|
||||
};
|
||||
|
||||
/**
|
||||
* View reference for CogniteVisualizable entities (from Core Data Model)
|
||||
*/
|
||||
export const COGNITE_VISUALIZABLE_VIEW: ViewId = {
|
||||
space: CORE_DM_SPACE,
|
||||
externalId: 'CogniteVisualizable',
|
||||
version: CORE_DM_VERSION,
|
||||
};
|
||||
|
||||
/**
|
||||
* View reference for Cognite3DModel entities (from Core Data Model)
|
||||
*/
|
||||
export const COGNITE_3D_MODEL_VIEW: ViewId = {
|
||||
space: CORE_DM_SPACE,
|
||||
externalId: 'Cognite3DModel',
|
||||
version: CORE_DM_VERSION,
|
||||
};
|
||||
|
||||
/**
|
||||
* View reference for CogniteCADRevision entities (from Core Data Model)
|
||||
*/
|
||||
export const COGNITE_CAD_REVISION_VIEW: ViewId = {
|
||||
space: CORE_DM_SPACE,
|
||||
externalId: 'CogniteCADRevision',
|
||||
version: CORE_DM_VERSION,
|
||||
};
|
||||
Reference in New Issue
Block a user