Skip to content

Commit

Permalink
feat: extract functionality to Workspace class
Browse files Browse the repository at this point in the history
  • Loading branch information
x-oflisback committed Mar 1, 2023
1 parent b782504 commit 4443f6d
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 108 deletions.
4 changes: 2 additions & 2 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
ExecutionStatus,
ExperimentId,
ExperimentTrajectories,
Workspace,
WorkspaceDefinition,
WorkspaceId,
} from './types'

Expand Down Expand Up @@ -264,7 +264,7 @@ class Api {
await this.ensureAxiosConfig()
}

getWorkspaces(): Promise<Workspace[]> {
getWorkspaces(): Promise<WorkspaceDefinition[]> {
return new Promise((resolve, reject) => {
this.ensureImpactToken()
.then(() => {
Expand Down
87 changes: 14 additions & 73 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { CaseId, CustomFunction, Workspace, WorkspaceId } from './types'
import Api from './api'
import Experiment from './experiment'
import ExperimentDefinition from './experiment-definition'
import Workspace from './workspace'
import { WorkspaceDefinition, WorkspaceId } from './types'

class Client {
private api: Api
Expand Down Expand Up @@ -50,26 +49,17 @@ class Client {
return new Client(api)
}

getWorkspaces(): Promise<Workspace[]> {
getWorkspaces(): Promise<WorkspaceDefinition[]> {
return this.api.getWorkspaces()
}

private async createExperiment({
experimentDefinition,
workspaceId,
}: {
experimentDefinition: ExperimentDefinition
workspaceId: WorkspaceId
}): Promise<Experiment> {
const experimentId = await this.api.createExperiment({
experimentDefinition,
workspaceId,
})
return new Experiment({
api: this.api,
id: experimentId,
workspaceId,
})
async getWorkspace(workspaceId: WorkspaceId): Promise<Workspace> {
const workspaces = await this.getWorkspaces()

if (workspaces.find((ws) => ws.id === workspaceId)) {
return new Workspace({ api: this.api, id: workspaceId })
}
throw new Error(`Workspace "${workspaceId}" not found.`)
}

async createWorkspace({
Expand All @@ -78,67 +68,18 @@ class Client {
}: {
description?: string
name: string
}): Promise<WorkspaceId> {
return await this.api.createWorkspace({
}): Promise<Workspace> {
const workspaceId = await this.api.createWorkspace({
description,
name,
})

return new Workspace({ api: this.api, id: workspaceId })
}

async deleteWorkspace(workspaceId: WorkspaceId): Promise<void> {
return await this.api.deleteWorkspace(workspaceId)
}

async executeExperiment({
caseIds,
experimentDefinition,
workspaceId,
}: {
caseIds: CaseId[]
experimentDefinition: ExperimentDefinition
workspaceId: WorkspaceId
}): Promise<Experiment> {
const experiment = await this.createExperiment({
experimentDefinition,
workspaceId,
})

await this.api.runExperiment({
cases: caseIds,
experimentId: experiment.id,
workspaceId,
})

return new Experiment({
api: this.api,
id: experiment.id,
workspaceId,
})
}

async executeExperimentSync({
caseIds,
experimentDefinition,
workspaceId,
}: {
caseIds: CaseId[]
experimentDefinition: ExperimentDefinition
workspaceId: WorkspaceId
}): Promise<Experiment> {
const experiment = await this.createExperiment({
experimentDefinition,
workspaceId,
})

await experiment.run(caseIds)
await experiment.executionDone()

return experiment
}

getCustomFunctions(workspaceId: WorkspaceId): Promise<CustomFunction[]> {
return this.api.getCustomFunctions(workspaceId)
}
}

export default Client
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ import {
ModelicaExperimentExtensions,
ModelicaExperimentModifiers,
ModelicaExperimentParameters,
Workspace,
WorkspaceDefinition,
} from './types/index.d'
import Workspace from './workspace'

export {
Api,
Expand All @@ -43,4 +44,5 @@ export {
ServerNotStarted,
UnknownApiError,
Workspace,
WorkspaceDefinition,
}
2 changes: 1 addition & 1 deletion src/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export type CustomFunction =
export type ExecutionStatus =
operations['getExecutionStatus']['responses']['200']['content']['application/json']

export type Workspace = components['schemas']['Workspace']
export type WorkspaceDefinition = components['schemas']['Workspace']

export type ModelicaExperimentDefinition =
components['schemas']['ExperimentDefinition']
Expand Down
71 changes: 71 additions & 0 deletions src/workspace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { CaseId, CustomFunction, WorkspaceId } from './types'
import Api from './api'
import Experiment from './experiment'
import ExperimentDefinition from './experiment-definition'

class Workspace {
private api: Api
id: WorkspaceId

constructor({ api, id }: { api: Api; id: WorkspaceId }) {
this.api = api
this.id = id
}

private async createExperiment(
experimentDefinition: ExperimentDefinition
): Promise<Experiment> {
const experimentId = await this.api.createExperiment({
experimentDefinition,
workspaceId: this.id,
})
return new Experiment({
api: this.api,
id: experimentId,
workspaceId: this.id,
})
}

async executeExperiment({
caseIds,
experimentDefinition,
}: {
caseIds: CaseId[]
experimentDefinition: ExperimentDefinition
}): Promise<Experiment> {
const experiment = await this.createExperiment(experimentDefinition)

await this.api.runExperiment({
cases: caseIds,
experimentId: experiment.id,
workspaceId: this.id,
})

return new Experiment({
api: this.api,
id: experiment.id,
workspaceId: this.id,
})
}

async executeExperimentSync({
caseIds,
experimentDefinition,
}: {
caseIds: CaseId[]
experimentDefinition: ExperimentDefinition
}): Promise<Experiment> {
const experiment = await this.createExperiment(experimentDefinition)

await experiment.run(caseIds)
await experiment.executionDone()

return experiment
}

getCustomFunctions(): Promise<CustomFunction[]> {
return this.api.getCustomFunctions(this.id)
}
}

export default Workspace
51 changes: 20 additions & 31 deletions tests/integration/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
ExperimentDefinition,
InvalidApiKey,
JhTokenError,
WorkspaceDefinition,
} from '../../dist'
import basicExperimentDefinition from './basicExperimentDefinition.json'

Expand All @@ -22,17 +23,10 @@ const getClient = (options?: {
})

test('Try to use invalid impact API key', (done) => {
const experimentDefinition = ExperimentDefinition.from(
basicExperimentDefinition
)
const client = getClient({ impactApiKey: 'invalid-api-key' })

client
.executeExperimentSync({
caseIds: ['case_1', 'case_2'],
experimentDefinition,
workspaceId: 'non-existing-workspace',
})
.getWorkspace('non-existing-workspace')
.then(() => {
throw new Error('Test should have caught error')
})
Expand All @@ -48,17 +42,10 @@ test('Try to use invalid impact API key', (done) => {
})

test('Try to use invalid jupyter hub token', (done) => {
const experimentDefinition = ExperimentDefinition.from(
basicExperimentDefinition
)
const client = getClient({ jupyterHubToken: 'invalid-jh-token' })

client
.executeExperimentSync({
caseIds: ['case_1', 'case_2'],
experimentDefinition,
workspaceId: 'non-existing-workspace',
})
.getWorkspace('non-existing-workspace')
.then(() => {
throw new Error('Test should have caught error')
})
Expand All @@ -83,23 +70,24 @@ test(
const client = getClient()
const WorkspaceName = 'setup-and-exec'

let testWorkspace
try {
const workspaces = await client.getWorkspaces()
let workspaceId
const testWorkspace = workspaces.find(
(w) => w.definition.name === WorkspaceName
)
if (testWorkspace) {
workspaceId = testWorkspace.id
} else {
workspaceId = await client.createWorkspace({
name: WorkspaceName,
})
}
const experiment = await client.executeExperimentSync({
testWorkspace = await client.getWorkspace(WorkspaceName)
} catch (e) {
testWorkspace = await client.createWorkspace({
name: WorkspaceName,
})
}

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

try {
const experiment = await testWorkspace.executeExperimentSync({
caseIds: ['case_1', 'case_2'],
experimentDefinition,
workspaceId,
})
expect(typeof experiment).toBe('object')

Expand Down Expand Up @@ -148,7 +136,8 @@ test(
const workspacesAfterDelete = await client.getWorkspaces()
expect(
workspacesAfterDelete.find(
(w) => w.definition.name === WorkspaceName
(w: WorkspaceDefinition) =>
w.definition.name === WorkspaceName
)
).toEqual(undefined)
} catch (e) {
Expand Down

0 comments on commit 4443f6d

Please sign in to comment.