From 8808338a5f11c1dcbcc15a46d612ddf8dcd153a9 Mon Sep 17 00:00:00 2001 From: Brian Kuzma Date: Tue, 30 Apr 2024 15:06:05 +0200 Subject: [PATCH] feat: add simulation run data API (#1098) * feat: add simulation run data API * fix: fix optional types --- .../api/simulationRunDataApi.spec.ts | 35 +++++++++++++++++ .../api/simulators/simulationRunDataApi.ts | 16 ++++++++ .../alpha/src/api/simulators/simulatorsApi.ts | 13 +++++++ packages/alpha/src/types.ts | 39 +++++++++++++++++++ 4 files changed, 103 insertions(+) create mode 100644 packages/alpha/src/__tests__/api/simulationRunDataApi.spec.ts create mode 100644 packages/alpha/src/api/simulators/simulationRunDataApi.ts diff --git a/packages/alpha/src/__tests__/api/simulationRunDataApi.spec.ts b/packages/alpha/src/__tests__/api/simulationRunDataApi.spec.ts new file mode 100644 index 00000000000..be017295b6c --- /dev/null +++ b/packages/alpha/src/__tests__/api/simulationRunDataApi.spec.ts @@ -0,0 +1,35 @@ +// Copyright 2023 Cognite AS +import CogniteClientAlpha from '../../cogniteClient'; +import { setupLoggedInClient } from '../testUtils'; + +const SHOULD_RUN_TESTS = process.env.RUN_SDK_SIMINT_TESTS == 'true'; + +const describeIf = SHOULD_RUN_TESTS ? describe : describe.skip; + +describeIf('simulation run data api', () => { + const client: CogniteClientAlpha = setupLoggedInClient(); + + test('list simulation run data', async () => { + const runs = await client.simulators.listRuns({ + filter: { + simulatorName: 'DWSIM', + status: 'success', + }, + }); + + const runId = runs.items[0].id; + + const runData = await client.simulators.listRunData([ + { + runId, + }, + ]); + + expect(runData).toBeDefined(); + expect(runData.length).toBeGreaterThan(0); + + const item = runData[0]; + + expect(item.runId).toBe(runId); + }); +}); diff --git a/packages/alpha/src/api/simulators/simulationRunDataApi.ts b/packages/alpha/src/api/simulators/simulationRunDataApi.ts new file mode 100644 index 00000000000..a3c1f3c615b --- /dev/null +++ b/packages/alpha/src/api/simulators/simulationRunDataApi.ts @@ -0,0 +1,16 @@ +// Copyright 2023 Cognite AS + +import { BaseResourceAPI, CDFHttpClient, MetadataMap } from '@cognite/sdk-core'; +import { SimulationRunData, SimulationRunId } from '../../types'; + +export class SimulationRunDataAPI extends BaseResourceAPI { + constructor(...args: [string, CDFHttpClient, MetadataMap]) { + super(...args); + } + + public retrieve = async (ids: SimulationRunId[]) => { + const path = this.url('simulators/runs/data/list'); + + return this.retrieveEndpoint(ids, {}, path); + }; +} diff --git a/packages/alpha/src/api/simulators/simulatorsApi.ts b/packages/alpha/src/api/simulators/simulatorsApi.ts index 1727b1c239b..946657ab83d 100644 --- a/packages/alpha/src/api/simulators/simulatorsApi.ts +++ b/packages/alpha/src/api/simulators/simulatorsApi.ts @@ -24,9 +24,11 @@ import { SimulatorModelRevisionChange, SimulatorRoutineRevisionCreate, SimulatorRoutineRevisionslFilterQuery, + SimulationRunId, } from '../../types'; import { IntegrationsAPI } from './integrationsApi'; import { SimulationRunsAPI } from './simulationRunsApi'; +import { SimulationRunDataAPI } from './simulationRunDataApi'; import { ModelsAPI } from './modelsApi'; import { ModelRevisionsAPI } from './modelRevisionsApi'; import { RoutinesAPI } from './routinesApi'; @@ -36,6 +38,7 @@ import { LogsAPI } from './logsApi'; export class SimulatorsAPI extends BaseResourceAPI { private integrationsApi: IntegrationsAPI; private runsApi: SimulationRunsAPI; + private runDataApi: SimulationRunDataAPI; private modelsApi: ModelsAPI; private modelRevisionsApi: ModelRevisionsAPI; private routinesApi: RoutinesAPI; @@ -66,6 +69,12 @@ export class SimulatorsAPI extends BaseResourceAPI { metadataMap ); + this.runDataApi = new SimulationRunDataAPI( + `${resourcePath}/runs/data`, + client, + metadataMap + ); + this.modelsApi = new ModelsAPI( `${resourcePath}/models`, client, @@ -127,6 +136,10 @@ export class SimulatorsAPI extends BaseResourceAPI { return this.runsApi.list(filter); }; + public listRunData = async (ids: SimulationRunId[]) => { + return this.runDataApi.retrieve(ids); + }; + public runSimulation = async (items: SimulationRunCreate[]) => { return this.runsApi.run(items); }; diff --git a/packages/alpha/src/types.ts b/packages/alpha/src/types.ts index 8af2b038777..f969f9f6ea9 100644 --- a/packages/alpha/src/types.ts +++ b/packages/alpha/src/types.ts @@ -230,6 +230,45 @@ export interface SimulationRun { lastUpdatedTime: Date; } +export type SimulationRunDataValueType = + | 'STRING' + | 'DOUBLE' + | 'STRING_ARRAY' + | 'DOUBLE_ARRAY'; + +export const SimulationRunDataValueType = { + STRING: 'STRING' as SimulationRunDataValueType, + DOUBLE: 'DOUBLE' as SimulationRunDataValueType, + STRING_ARRAY: 'STRING_ARRAY' as SimulationRunDataValueType, + DOUBLE_ARRAY: 'DOUBLE_ARRAY' as SimulationRunDataValueType, +}; + +export interface SimulationRunDataOutput { + referenceId: CogniteInternalId; + value: string | number | string[] | number[]; + valueType: SimulationRunDataValueType; + unit?: { + name: string; + externalId?: CogniteExternalId; + }; + simulatorObjectReference?: Record; + timeseriesExternalId?: CogniteExternalId; +} + +export interface SimulationRunDataInput extends SimulationRunDataOutput { + overridden?: boolean; +} + +export interface SimulationRunId { + runId: CogniteInternalId; +} + +export interface SimulationRunData { + runId: CogniteInternalId; + inputs: SimulationRunDataInput[]; + outputs: SimulationRunDataOutput[]; +} + export type SimulatorLogSeverityLevel = | 'Debug' | 'Information'