Skip to content

Commit

Permalink
Send single event to share experiment to Studio (#3422)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattseddon authored Mar 8, 2023
1 parent b48c80d commit 1dda52a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 78 deletions.
51 changes: 16 additions & 35 deletions extension/src/patch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,14 @@ type ExperimentDetails = {
params: ValueTreeRoot
}

type BaseRequestBody = {
type RequestBody = {
client: 'vscode'
repo_url: string
name: string
baseline_sha: string
}

type StartRequestBody = BaseRequestBody & { type: 'start' }

type DoneRequestBody = BaseRequestBody & { type: 'done' }

type DataRequestBody = BaseRequestBody & {
metrics: ValueTreeRoot
params: ValueTreeRoot
plots: ValueTreeRoot
step: number
type: 'data'
type: 'done'
}

const collectExperiment = (data: ExperimentFields) => {
Expand Down Expand Up @@ -96,7 +87,7 @@ const collectExperimentDetails = (

const sendPostRequest = (
studioAccessToken: string,
body: StartRequestBody | DataRequestBody | DoneRequestBody
body: RequestBody
): Promise<FetchResponse> =>
fetch(STUDIO_ENDPOINT, {
body: JSON.stringify(body),
Expand Down Expand Up @@ -124,42 +115,32 @@ const shareWithProgress = (
): Thenable<unknown> =>
Toast.showProgress('Sharing Experiment', async progress => {
const { metrics, params, baselineSha, name } = experimentDetails
const base: BaseRequestBody = {
baseline_sha: baselineSha,
client: 'vscode',
name,
repo_url: repoUrl
}

progress.report({
increment: 0,
message: 'Initializing experiment...'
})

progress.report({ increment: 50, message: 'Sending data...' })
const response = await sendPostRequest(studioAccessToken, {
...base,
type: 'start'
baseline_sha: baselineSha,
client: 'vscode',
metrics: metrics || {},
name,
params: params || {},
repo_url: repoUrl,
type: 'done'
})

progress.report({ increment: 25, message: 'Response received...' })

if (response.status === 401) {
progress.report({ increment: 100, message: 'Access unauthorized' })
progress.report({ increment: 25, message: 'Access unauthorized' })
void showUnauthorized()
return Toast.delayProgressClosing()
}

progress.report({ increment: 33, message: 'Sending data...' })
await sendPostRequest(studioAccessToken, {
...base,
metrics: metrics || {},
params: params || {},
plots: {},
step: 0,
type: 'data'
})

progress.report({ increment: 33, message: 'Completing process...' })
await sendPostRequest(studioAccessToken, { ...base, type: 'done' })

progress.report({ increment: 33, message: 'Done' })
progress.report({ increment: 25, message: 'Done' })

return Toast.delayProgressClosing()
})
Expand Down
70 changes: 27 additions & 43 deletions extension/src/test/suite/patch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,50 +58,30 @@ suite('Patch Test Suite', () => {

expect(mockGetRemoteUrl).to.be.calledOnce
expect(mockExpShow).to.be.calledOnce
expect(mockFetch).to.be.calledThrice
expect(mockFetch).to.be.calledOnce

const { metrics, name, params } = expShowFixture[
'53c3851f46955fa3e2b8f6e1c52999acc8c9ea77'
]['4fb124aebddb2adf1545030907687fa9a4c80e70'].data as ExperimentFields

const baseBody = {
baseline_sha: '53c3851f46955fa3e2b8f6e1c52999acc8c9ea77',
client: 'vscode',
name,
repo_url: mockRepoUrl
}
const headers = {
Authorization: `token ${mockStudioAccessToken}`,
'Content-type': 'application/json'
}

expect(mockFetch).to.be.calledWithExactly(STUDIO_ENDPOINT, {
body: JSON.stringify({ ...baseBody, type: 'start' }),
headers,
method: 'POST'
})

expect(mockFetch).to.be.calledWithExactly(STUDIO_ENDPOINT, {
body: JSON.stringify({
...baseBody,
baseline_sha: '53c3851f46955fa3e2b8f6e1c52999acc8c9ea77',
client: 'vscode',
metrics,
name,
params: {
'params.yaml': params?.['params.yaml']?.data,
[join('nested', 'params.yaml')]:
params?.[join('nested', 'params.yaml')]?.data
},
plots: {},
step: 0,
type: 'data'
repo_url: mockRepoUrl,
type: 'done'
}),
headers,
method: 'POST'
})

expect(mockFetch).to.be.calledWithExactly(STUDIO_ENDPOINT, {
body: JSON.stringify({ ...baseBody, type: 'done' }),
headers,
headers: {
Authorization: `token ${mockStudioAccessToken}`,
'Content-type': 'application/json'
},
method: 'POST'
})
})
Expand Down Expand Up @@ -145,24 +125,28 @@ suite('Patch Test Suite', () => {
RegisteredCommands.CONNECT_SHOW
)

const { name } = expShowFixture[
const { metrics, params, name } = expShowFixture[
'53c3851f46955fa3e2b8f6e1c52999acc8c9ea77'
]['4fb124aebddb2adf1545030907687fa9a4c80e70'].data as ExperimentFields

const baseBody = {
baseline_sha: '53c3851f46955fa3e2b8f6e1c52999acc8c9ea77',
client: 'vscode',
name,
repo_url: mockRepoUrl
}
const headers = {
Authorization: `token ${mockStudioAccessToken}`,
'Content-type': 'application/json'
}

expect(mockFetch).to.be.calledWithExactly(STUDIO_ENDPOINT, {
body: JSON.stringify({ ...baseBody, type: 'start' }),
headers,
body: JSON.stringify({
baseline_sha: '53c3851f46955fa3e2b8f6e1c52999acc8c9ea77',
client: 'vscode',
metrics,
name,
params: {
'params.yaml': params?.['params.yaml']?.data,
[join('nested', 'params.yaml')]:
params?.[join('nested', 'params.yaml')]?.data
},
repo_url: mockRepoUrl,
type: 'done'
}),
headers: {
Authorization: `token ${mockStudioAccessToken}`,
'Content-type': 'application/json'
},
method: 'POST'
})
})
Expand Down

0 comments on commit 1dda52a

Please sign in to comment.