Skip to content

Commit

Permalink
feat: add ExperimentStatus class
Browse files Browse the repository at this point in the history
  • Loading branch information
x-oflisback committed Mar 29, 2023
1 parent 6856223 commit 376fbda
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
CaseId,
CaseTrajectories,
CustomFunction,
ExecutionStatus,
ExecutionStatusType,
ExperimentId,
ExperimentTrajectories,
WorkspaceDefinition,
Expand Down Expand Up @@ -385,7 +385,7 @@ class Api {
}: {
experimentId: ExperimentId
workspaceId: WorkspaceId
}): Promise<ExecutionStatus> {
}): Promise<ExecutionStatusType> {
return new Promise((resolve, reject) => {
this.ensureImpactToken()
.then(() => {
Expand Down
40 changes: 40 additions & 0 deletions src/executionStatus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { ExecutionStatusType } from './types'

class ExecutionStatus {
status: ExecutionStatusType

constructor(status: ExecutionStatusType) {
this.status = status
}

getNbrCompilationDone = () =>
this.status?.progresses?.filter(
(p) =>
(p.stage === 'compilation' && p.done) ||
p.stage === 'simulation'
).length || 0

getNbrSimulations = () =>
this.status?.progresses?.filter((p) => p.stage === 'simulation')
.length || 0

getNbrSimulationDone = () =>
this.status?.progresses?.filter(
(p) => p.stage === 'simulation' && p.done
).length || 0

getCompilationProgress = () =>
this.status?.progresses
? this.getNbrCompilationDone() / this.status.progresses.length
: 0

getSimulationProgress = () => {
if (this.getNbrSimulations() === 0) {
return 0
}
return this.status?.progresses
? this.getNbrSimulationDone() / this.getNbrSimulations()
: 0
}
}
export default ExecutionStatus
5 changes: 4 additions & 1 deletion src/experiment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from './types'
import Api from './api'
import Case from './case'
import ExecutionStatus from './executionStatus'

const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms))

Expand Down Expand Up @@ -81,10 +82,12 @@ class Experiment {
}

async getExecutionStatus() {
return await this.api.getExecutionStatus({
const status = await this.api.getExecutionStatus({
experimentId: this.id,
workspaceId: this.workspaceId,
})

return new ExecutionStatus(status)
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import ExperimentDefinition from './experiment-definition'
import {
CaseTrajectories,
CustomFunction,
ExecutionStatusType,
ExperimentId,
ExperimentTrajectories,
ModelicaExperimentExtensions,
Expand All @@ -30,6 +31,7 @@ export {
CaseTrajectories,
Client,
CustomFunction,
ExecutionStatusType,
ExperimentDefinition,
Experiment,
ExperimentId,
Expand Down
2 changes: 1 addition & 1 deletion src/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export type Case = components['schemas']['Case']
export type CustomFunction =
operations['getCustomFunction']['responses']['200']['content']['application/json']

export type ExecutionStatus =
export type ExecutionStatusType =
operations['getExecutionStatus']['responses']['200']['content']['application/json']

export type WorkspaceDefinition = components['schemas']['Workspace']
Expand Down
53 changes: 51 additions & 2 deletions tests/integration/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import basicExperimentDefinition from './basicExperimentDefinition.json'

dotenv.config()

const TwentySeconds = 20 * 1000

const getClient = (options?: {
impactApiKey?: string
jupyterHubToken?: string
Expand Down Expand Up @@ -83,7 +85,7 @@ test(

const customFunctions = await testWorkspace.getCustomFunctions()
expect(customFunctions.length).toBeGreaterThanOrEqual(
['linearize', 'dynamic', 'steady state'].length
['dynamic', 'steady state'].length
)

try {
Expand Down Expand Up @@ -149,5 +151,52 @@ test(
throw new Error('Caught unexpected error while executing test')
}
},
20 * 1000
TwentySeconds
)

test(
'Run simulation and track progress',
async () => {
const experimentDefinition = ExperimentDefinition.from(
basicExperimentDefinition
)

const client = getClient()
const WorkspaceName = 'simulation-progress'

let testWorkspace
try {
testWorkspace = await client.getWorkspace(WorkspaceName)
} catch (e) {
testWorkspace = await client.createWorkspace({
name: WorkspaceName,
})
}

expect(testWorkspace.name).toEqual(WorkspaceName)

try {
let done = false
const experiment = await testWorkspace.executeExperiment({
caseIds: ['case_1', 'case_2'],
experimentDefinition,
})
while (!done) {
const status = await experiment.getExecutionStatus()

await new Promise((resolve) => setTimeout(resolve, 100))

if (status.getSimulationProgress() === 1) {
expect(status.getCompilationProgress() === 1)
done = true
}
}
} catch (e) {
if (e instanceof Error) {
console.log(e.toString())
}
throw new Error('Caught unexpected error while executing test')
}
},
TwentySeconds
)

0 comments on commit 376fbda

Please sign in to comment.