Skip to content

Commit c53684d

Browse files
author
Rui Gao
committed
add history vc list retrieve when user login/update
1 parent 72e5dac commit c53684d

File tree

3 files changed

+40
-12
lines changed

3 files changed

+40
-12
lines changed

src/rest-server/src/controllers/v2/job.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,6 @@ const { Op } = require('sequelize');
2626
const { userProperty } = require('@pai/config/token');
2727
const userController = require('@pai/controllers/v2/user');
2828

29-
const getUserHistoryVCs = async (username) => {
30-
const jobs = await job.list(['virtualCluster'], { userName: username });
31-
const vcs = Array.from(new Set(jobs.map((j) => j.virtualCluster)));
32-
logger.info(`User ${username} has accessed historical virtual clusters: ${vcs}`);
33-
return vcs;
34-
};
35-
3629
const list = asyncHandler(async (req, res) => {
3730
// ?keyword=<keyword filter>&username=<username1>,<username2>&vc=<vc1>,<vc2>
3831
// &state=<state1>,<state2>&offset=<offset>&limit=<limit>&withTotalCount=true
@@ -392,7 +385,6 @@ const getLogs = asyncHandler(async (req, res) => {
392385

393386
// module exports
394387
module.exports = {
395-
getUserHistoryVCs,
396388
list,
397389
get,
398390
update,

src/rest-server/src/models/v2/job/k8s.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1556,6 +1556,38 @@ const getEvents = async (frameworkName, attributes, filters) => {
15561556
}
15571557
};
15581558

1559+
const listVCsFromJob = async (username) => {
1560+
try {
1561+
logger.info(`Start to list jobs for user ${username}`);
1562+
// Remove limit: 0 so it fetches all records, and optionally add a sensible order
1563+
const frameworks = await databaseModel.Framework.findAll({
1564+
attributes: [
1565+
'name',
1566+
'jobName',
1567+
'userName',
1568+
'virtualCluster',
1569+
],
1570+
where: { userName: username },
1571+
});
1572+
1573+
logger.info(`Frameworks raw result for user ${username}: ${JSON.stringify(frameworks, null, 2)}`);
1574+
1575+
const vcsSet = new Set();
1576+
frameworks.forEach((framework) => {
1577+
if (framework.virtualCluster) {
1578+
vcsSet.add(framework.virtualCluster);
1579+
}
1580+
});
1581+
const vcs = Array.from(vcsSet);
1582+
1583+
logger.info(`User ${username} has accessed historical virtual clusters: ${vcs}`);
1584+
return vcs;
1585+
} catch (error) {
1586+
logger.error(`Failed to get historical virtual clusters for user ${username}: ${error}`);
1587+
return [];
1588+
}
1589+
};
1590+
15591591
// module exports
15601592
module.exports = {
15611593
list,
@@ -1567,4 +1599,5 @@ module.exports = {
15671599
addTag,
15681600
deleteTag,
15691601
getEvents,
1602+
listVCsFromJob,
15701603
};

src/rest-server/src/utils/manager/user/crudK8sSecret.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const logger = require('@pai/config/logger');
2020
const groupModel = require('@pai/models/v2/group');
2121
const k8sModel = require('@pai/models/kubernetes/kubernetes');
2222
const { Mutex } = require('async-mutex');
23-
const jobController = require('@pai/controllers/v2/job');
23+
const { job } = require('@pai/models/v2/job');
2424

2525
const USER_NAMESPACE = process.env.PAI_USER_NAMESPACE || 'pai-user-v2';
2626

@@ -207,7 +207,8 @@ async function create(key, value) {
207207
await User.encryptUserPassword(userInstance);
208208

209209
// retrieve VC list from the history job list belonging to the user if exists
210-
userInstance.history_vclist = await jobController.getUserHistoryVCs(userInstance.username);
210+
const vcsFromJob = await job.listVCsFromJob(userInstance.username);
211+
userInstance.history_vclist = vcsFromJob;
211212

212213
// retrieve VC from group list
213214
const vcSet = new Set();
@@ -286,7 +287,8 @@ async function update(key, value, updatePassword = false) {
286287
grouplist: value.grouplist,
287288
email: value.email,
288289
extension: value.extension,
289-
history_vclist: value.history_vclist || [],
290+
//history_vclist: value.history_vclist || [],
291+
history_vclist: [],
290292
});
291293
if (updatePassword) {
292294
await User.encryptUserPassword(userInstance);
@@ -295,7 +297,8 @@ async function update(key, value, updatePassword = false) {
295297
// if userInstance.history_vclist is empty, set it to the retrieved VC list
296298
// retrieve VC list from the job list belonging to the user
297299
if (!userInstance.history_vclist || userInstance.history_vclist.length === 0) {
298-
userInstance.history_vclist = await jobController.getUserHistoryVCs(userInstance.username);
300+
const vcsFromJob = await job.listVCsFromJob(userInstance.username);
301+
userInstance.history_vclist = vcsFromJob;
299302
}
300303

301304
// retrieve VC from group list

0 commit comments

Comments
 (0)