Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

Commit

Permalink
[webportal] Update webportal job api to use js sdk (#4669)
Browse files Browse the repository at this point in the history
* update webportal job to use js sdk

* update

* fix
  • Loading branch information
yiyione authored Jul 5, 2020
1 parent 0de80f4 commit 2950a8d
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 319 deletions.
93 changes: 23 additions & 70 deletions src/webportal/src/app/home/home/conn.js
Original file line number Diff line number Diff line change
@@ -1,83 +1,52 @@
// Copyright (c) Microsoft Corporation
// All rights reserved.
//
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
// BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import querystring from 'querystring';
import { PAIV2 } from '@microsoft/openpai-js-sdk';

import config from '../../config/webportal.config';

const username = cookies.get('user');
const token = cookies.get('token');

const client = new PAIV2.OpenPAIClient({
rest_server_uri: config.restServerUri,
username: username,
token: token,
});

export class UnauthorizedError extends Error {
constructor(msg) {
super(msg);
this.name = 'UnauthorizedError';
}
}

async function fetchWrapper(...args) {
const res = await fetch(...args);
const json = await res.json();
if (res.ok) {
return json;
} else {
if (json.code === 'UnauthorizedUserError') {
throw new UnauthorizedError(json.message);
const wrapper = async func => {
try {
return await func();
} catch (err) {
if (err.data.code === 'UnauthorizedUserError') {
throw new UnauthorizedError(err.data.message);
} else {
throw new Error(json.message);
throw new Error(err.data.message);
}
}
}
};

export async function listJobs() {
return fetchWrapper(
`${config.restServerUri}/api/v1/jobs?${querystring.stringify({
username,
})}`,
{
headers: {
Authorization: `Bearer ${token}`,
},
},
);
return wrapper(() => client.job.listJobs(username));
}

export async function listAllJobs() {
return fetchWrapper(`${config.restServerUri}/api/v1/jobs`, {
headers: {
Authorization: `Bearer ${token}`,
},
});
return wrapper(() => client.job.listJobs());
}

export async function getUserInfo() {
return fetchWrapper(`${config.restServerUri}/api/v2/user/${username}`, {
headers: {
Authorization: `Bearer ${token}`,
},
});
return wrapper(() => client.user.getUser(username));
}

export async function listVirtualClusters() {
return fetchWrapper(`${config.restServerUri}/api/v2/virtual-clusters`, {
headers: {
Authorization: `Bearer ${token}`,
},
});
return wrapper(() => client.virtualCluster.listVirtualClusters());
}

export async function getAvailableGpuPerNode() {
Expand Down Expand Up @@ -132,23 +101,7 @@ export async function getLowGpuJobInfos() {

export async function stopJob(job) {
const { name, username } = job;
const res = await fetch(
`${config.restServerUri}/api/v1/jobs/${username}~${name}/executionType`,
{
method: 'PUT',
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({ value: 'STOP' }),
},
return wrapper(() =>
client.job.updateJobExecutionType(username, name, 'STOP'),
);
const json = await res.json();
if (!res.ok) {
if (json.code === 'UnauthorizedUserError') {
throw new UnauthorizedError(json.message);
} else {
throw new Error(json.message);
}
}
}
71 changes: 8 additions & 63 deletions src/webportal/src/app/job-submission/utils/conn.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ const wrapper = async func => {
if (err.data.code === 'UnauthorizedUserError') {
alert(err.data.message);
clearToken();
} else if (err.data.code === 'NoJobConfigError') {
throw new NotFoundError(err.data.message);
} else {
throw new Error(err.data.message);
}
Expand All @@ -35,80 +37,23 @@ export class NotFoundError extends Error {
}
}

async function fetchWrapper(...args) {
const res = await fetch(...args);
const json = await res.json();
if (res.ok) {
return json;
} else {
if (json.code === 'UnauthorizedUserError') {
alert(json.message);
clearToken();
} else {
throw new Error(json.message);
}
}
}

export async function submitJob(jobProtocol) {
return fetchWrapper(`${config.restServerUri}/api/v2/jobs`, {
body: jobProtocol,
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'text/yaml',
},
method: 'POST',
});
const job = yaml.safeLoad(jobProtocol);
return wrapper(() => client.job.createJob(job));
}

export async function fetchJobConfig(userName, jobName) {
const url = `${config.restServerUri}/api/v2/jobs/${userName}~${jobName}/config`;
const res = await fetch(url, {
headers: {
Authorization: `Bearer ${token}`,
},
});
const text = await res.text();
const json = yaml.safeLoad(text);
if (res.ok) {
return json;
} else {
if (json.code === 'NoJobConfigError') {
throw new NotFoundError(json.message);
} else {
throw new Error(json.message);
}
}
return wrapper(() => client.job.getJobConfig(userName, jobName));
}

export async function listUserVirtualClusters(user) {
const userInfo = await fetchWrapper(
`${config.restServerUri}/api/v1/user/${user}`,
{
headers: {
Authorization: `Bearer ${token}`,
},
},
);
const userInfo = await wrapper(() => client.user.getUser(user));
return get(userInfo, 'virtualCluster', []);
}

export async function fetchUserGroup(api, user, token) {
const userInfoUrl = `${api}/api/v2/user/${user}`;

return fetch(userInfoUrl, {
headers: {
Authorization: `Bearer ${token}`,
},
}).then(response => {
if (response.ok) {
return response.json().then(responseData => {
return responseData.grouplist;
});
} else {
throw Error(`fetch ${userInfoUrl}: HTTP ${response.status}`);
}
});
const userInfo = await wrapper(() => client.user.getUser(user));
return get(userInfo, 'grouplist', []);
}

export async function listUserStorageConfigs(user) {
Expand Down
89 changes: 32 additions & 57 deletions src/webportal/src/app/job/job-view/fabric/JobList/index.jsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,7 @@
// Copyright (c) Microsoft Corporation
// All rights reserved.
//
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
// BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { PAIV2 } from '@microsoft/openpai-js-sdk';
import * as querystring from 'querystring';

import React, {
Expand Down Expand Up @@ -106,36 +93,26 @@ export default function JobList() {
userAuth.checkToken(token => {
jobs.forEach(job => {
const { name, username } = job;
fetch(
`${webportalConfig.restServerUri}/api/v1/jobs/${username}~${name}/executionType`,
{
method: 'PUT',
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({ value: 'STOP' }),
},
)
.then(response => {
if (response.ok) {
job.executionType = 'STOP';
delete job._statusText;
delete job._statusIndex;
setAllJobs(allJobs.slice());
const client = new PAIV2.OpenPAIClient({
rest_server_uri: webportalConfig.restServerUri,
username: username,
token: token,
});
client.job
.updateJobExecutionType(username, name, 'STOP')
.then(() => {
job.executionType = 'STOP';
delete job._statusText;
delete job._statusIndex;
setAllJobs(allJobs.slice());
})
.catch(err => {
if (err.data.code === 'UnauthorizedUserError') {
alert(err.data.message);
clearToken();
} else {
return response.json().then(data => {
if (data.code === 'UnauthorizedUserError') {
alert(data.message);
clearToken();
} else {
throw new Error(data.message);
}
});
throw new Error(err.data.message);
}
})
.catch(reason => {
setError(reason.message);
});
});
});
Expand All @@ -146,21 +123,19 @@ export default function JobList() {
const refreshJobs = useCallback(function refreshJobs() {
setAllJobs(null);
const token = userAuth.checkToken();
fetch(`${webportalConfig.restServerUri}/api/v1/jobs`, {
headers: {
Authorization: `Bearer ${token}`,
},
})
.then(response => {
if (!response.ok) {
throw Error(response.message);
} else {
return response.json();
}
const client = new PAIV2.OpenPAIClient({
rest_server_uri: webportalConfig.restServerUri,
username: username,
token: token,
});
client.job
.listJobs()
.then(data => {
return data;
})
.then(setAllJobs)
.catch(reason => {
setError(reason.message);
.catch(err => {
throw Error(err.data.message);
});
}, []);

Expand Down
Loading

0 comments on commit 2950a8d

Please sign in to comment.