simplify db
Signed-off-by: Arthur Lu <learthurgo@gmail.com>
This commit is contained in:
parent
f2ccee5119
commit
9943a35d7e
17
db.js
17
db.js
@ -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
42
localdb.json.template
Normal 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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
8
main.js
8
main.js
@ -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;
|
||||
|
@ -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;
|
||||
if (resourceMeta[resourceName].type === "storage") {
|
||||
for (let diskPrefix of resourceMeta[resourceName].disks) {
|
||||
diskprefixes.push(diskPrefix);
|
||||
}
|
||||
else if (resourceMeta[resourceName].type === "disk") {
|
||||
resourceMeta[resourceName].storages.forEach((element) => {
|
||||
used[element] = 0;
|
||||
});
|
||||
diskprefixes.push(resourceName);
|
||||
}
|
||||
}
|
||||
for (instance of response.data.data) {
|
||||
|
Loading…
Reference in New Issue
Block a user