update resource data format,

add names to pci and cpu list resources
This commit is contained in:
2023-09-08 20:48:33 +00:00
parent 93cf6d1873
commit 8912ae5f3c
3 changed files with 74 additions and 42 deletions

View File

@@ -44,11 +44,11 @@ router.get(`/:node(${nodeRegexP})/pci`, async (req, res) => {
return;
}
// get remaining user resources
const userAvailPci = (await getUserResources(req, req.cookies.username)).avail.pci;
const userAvailPci = (await getUserResources(req, req.cookies.username)).pci;
// get node avail devices
let nodeAvailPci = await getNodeAvailDevices(params.node, req.cookies);
nodeAvailPci = nodeAvailPci.filter(nodeAvail => userAvailPci.some((userAvail) => {
return nodeAvail.device_name && nodeAvail.device_name.includes(userAvail);
return nodeAvail.device_name && nodeAvail.device_name.includes(userAvail.match) && userAvail.avail > 0;
}));
res.status(200).send(nodeAvailPci);
res.end();

View File

@@ -49,21 +49,25 @@ export async function getUserResources (req, username) {
const db = global.db;
const dbResources = db.getGlobalConfig().resources;
const used = await getUsedResources(req, dbResources);
const max = db.getUserConfig(username).resources.max;
const avail = {};
Object.keys(max).forEach((k) => {
const userResources = db.getUserConfig(username).resources;
Object.keys(userResources).forEach((k) => {
if (dbResources[k] && dbResources[k].type === "list") {
avail[k] = structuredClone(max[k]);
userResources[k].forEach((listResource) => {
listResource.used = 0;
listResource.avail = listResource.max;
});
used[k].forEach((usedDeviceName) => {
const index = avail[k].findIndex((maxElement) => usedDeviceName.includes(maxElement));
avail[k].splice(index, 1);
const index = userResources[k].findIndex((availEelement) => usedDeviceName.includes(availEelement.match));
userResources[k][index].used++;
userResources[k][index].avail--;
});
}
else {
avail[k] = max[k] - used[k];
userResources[k].used = used[k];
userResources[k].avail = userResources[k].max - used[k];
}
});
return { used, max, avail, resources: dbResources };
return userResources;
}
/**
@@ -74,24 +78,26 @@ export async function getUserResources (req, username) {
* @returns {boolean} true if the available resources can fullfill the requested resources, false otherwise.
*/
export async function approveResources (req, username, request) {
const user = await getUserResources(req, username);
const avail = user.avail;
const resources = user.resources;
const db = global.db;
const dbResources = db.getGlobalConfig().resources;
const userResources = await getUserResources(req, username);
let approved = true;
Object.keys(request).forEach((key) => {
if (!(key in avail)) { // if requested resource is not in avail, block
if (!(key in userResources)) { // if requested resource is not in avail, block
approved = false;
}
else if (resources[key].type === "list") {
const inAvail = avail[key].some(availElem => request[key].includes(availElem));
if (inAvail !== resources[key].whitelist) {
else if (dbResources[key].type === "list") { // if the resource type is list, check if the requested resource exists in the list
const index = userResources[key].findIndex((availElement) => request[key].includes(availElement.match));
// if no matching resource when index == -1, then remaining is -1 otherwise use the remaining value
const avail = index === -1 ? false : userResources[key][index].avail > 0;
if (avail !== dbResources[key].whitelist) {
approved = false;
}
}
else if (isNaN(avail[key]) || isNaN(request[key])) { // if either the requested or avail resource is NaN, block
else if (isNaN(userResources[key].avail) || isNaN(request[key])) { // if either the requested or avail resource is NaN, block
approved = false;
}
else if (avail[key] - request[key] < 0) { // if the avail resources is less than the requested resources, block
else if (userResources[key].avail - request[key] < 0) { // if the avail resources is less than the requested resources, block
approved = false;
}
});