simplify db

Signed-off-by: Arthur Lu <learthurgo@gmail.com>
This commit is contained in:
Arthur Lu 2023-04-20 20:43:05 +00:00
parent 109f9993cc
commit c3160f760f
4 changed files with 58 additions and 22 deletions

17
db.js
View File

@ -1,18 +1,19 @@
const fs = require("fs");
template = "localdb.json.template"
filename = "localdb.json";
let db = {};
let db = JSON.parse(fs.readFileSync(template));
/**
* called at app startup, can be used to initialize any variables needed for database access
*/
function init () {
try {
db = JSON.parse(fs.readFileSync(filename));
load();
}
catch {
fs.writeFileSync(filename, JSON.stringify(db));
save();
}
}
@ -24,16 +25,12 @@ function save () {
fs.writeFileSync(filename, JSON.stringify(db));
}
function getResourceMeta () {
return db["resource-metadata"];
}
function getResourceUnits () {
return db["resource-units"];
function getResources() {
return db.resources;
}
function getUser (username) {
return db.users[username];
}
module.exports = {init, getResourceMeta, getResourceUnits, getUser};
module.exports = {init, getUser, getResources};

42
localdb.json.template Normal file
View File

@ -0,0 +1,42 @@
{
"resources": {
"cores": {
"type": "numeric",
"multiplier": 1,
"compact": false,
"unit": "Cores"
},
"memory": {
"type": "numeric",
"multiplier": 1048576,
"compact": true,
"unit": "B"
},
"local": {
"type": "storage",
"multiplier": 1,
"compact": true,
"unit": "B",
"disks": ["rootfs", "mp", "sata", "unused"]
},
"cephpl": {
"type": "storage",
"multiplier": 1,
"compact": true,
"unit": "B",
"disks": ["rootfs", "mp", "sata", "unused"]
}
},
"users": {
"exampleuser@realm": {
"resources": {
"max": {
"cores": 128,
"memory": 131072,
"local": 1099511627776,
"cephpl": 1099511627776
}
}
}
}
}

View File

@ -8,7 +8,7 @@ var api = require("./package.json");
const {pveAPIToken, listenPort, domain} = require("./vars.js");
const {checkAuth, requestPVE, handleResponse, getUsedResources, getDiskInfo} = require("./pveutils.js");
const {init, getResourceMeta, getUser, getResourceUnits} = require("./db.js");
const {init, getUser, getResources} = require("./db.js");
const app = express();
app.use(helmet());
@ -44,8 +44,8 @@ app.post("/api/proxmox/*", async (req, res) => { // proxy endpoint for POST prox
});
async function getUserResources (req, username) {
let used = await getUsedResources(req, getResourceMeta());
let max = getUser(username).max;
let used = await getUsedResources(req, getResources());
let max = getUser(username).resources.max;
avail = {};
Object.keys(max).forEach((k) => {
avail[k] = max[k] - used[k];
@ -70,7 +70,7 @@ app.get("/api/user/resources", async(req, res) => {
// check auth
await checkAuth(req.cookies, res);
let userResources = await getUserResources(req, req.cookies.username);
userResources.units = getResourceUnits();
userResources.units = getResources();
res.status(200).send(userResources);
res.end();
return;

View File

@ -83,14 +83,11 @@ async function getUsedResources (req, resourceMeta) {
let used = {};
let diskprefixes = [];
for (let resourceName of Object.keys(resourceMeta)) {
if (resourceMeta[resourceName].type === "numeric") {
used[resourceName] = 0;
}
else if (resourceMeta[resourceName].type === "disk") {
resourceMeta[resourceName].storages.forEach((element) => {
used[element] = 0;
});
diskprefixes.push(resourceName);
used[resourceName] = 0;
if (resourceMeta[resourceName].type === "storage") {
for (let diskPrefix of resourceMeta[resourceName].disks) {
diskprefixes.push(diskPrefix);
}
}
}
for (instance of response.data.data) {