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");
|
const fs = require("fs");
|
||||||
|
|
||||||
|
template = "localdb.json.template"
|
||||||
filename = "localdb.json";
|
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
|
* called at app startup, can be used to initialize any variables needed for database access
|
||||||
*/
|
*/
|
||||||
function init () {
|
function init () {
|
||||||
try {
|
try {
|
||||||
db = JSON.parse(fs.readFileSync(filename));
|
load();
|
||||||
}
|
}
|
||||||
catch {
|
catch {
|
||||||
fs.writeFileSync(filename, JSON.stringify(db));
|
save();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -24,16 +25,12 @@ function save () {
|
|||||||
fs.writeFileSync(filename, JSON.stringify(db));
|
fs.writeFileSync(filename, JSON.stringify(db));
|
||||||
}
|
}
|
||||||
|
|
||||||
function getResourceMeta () {
|
function getResources() {
|
||||||
return db["resource-metadata"];
|
return db.resources;
|
||||||
}
|
|
||||||
|
|
||||||
function getResourceUnits () {
|
|
||||||
return db["resource-units"];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getUser (username) {
|
function getUser (username) {
|
||||||
return db.users[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 {pveAPIToken, listenPort, domain} = require("./vars.js");
|
||||||
const {checkAuth, requestPVE, handleResponse, getUsedResources, getDiskInfo} = require("./pveutils.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();
|
const app = express();
|
||||||
app.use(helmet());
|
app.use(helmet());
|
||||||
@ -44,8 +44,8 @@ app.post("/api/proxmox/*", async (req, res) => { // proxy endpoint for POST prox
|
|||||||
});
|
});
|
||||||
|
|
||||||
async function getUserResources (req, username) {
|
async function getUserResources (req, username) {
|
||||||
let used = await getUsedResources(req, getResourceMeta());
|
let used = await getUsedResources(req, getResources());
|
||||||
let max = getUser(username).max;
|
let max = getUser(username).resources.max;
|
||||||
avail = {};
|
avail = {};
|
||||||
Object.keys(max).forEach((k) => {
|
Object.keys(max).forEach((k) => {
|
||||||
avail[k] = max[k] - used[k];
|
avail[k] = max[k] - used[k];
|
||||||
@ -70,7 +70,7 @@ app.get("/api/user/resources", async(req, res) => {
|
|||||||
// check auth
|
// check auth
|
||||||
await checkAuth(req.cookies, res);
|
await checkAuth(req.cookies, res);
|
||||||
let userResources = await getUserResources(req, req.cookies.username);
|
let userResources = await getUserResources(req, req.cookies.username);
|
||||||
userResources.units = getResourceUnits();
|
userResources.units = getResources();
|
||||||
res.status(200).send(userResources);
|
res.status(200).send(userResources);
|
||||||
res.end();
|
res.end();
|
||||||
return;
|
return;
|
||||||
|
13
pveutils.js
13
pveutils.js
@ -83,14 +83,11 @@ async function getUsedResources (req, resourceMeta) {
|
|||||||
let used = {};
|
let used = {};
|
||||||
let diskprefixes = [];
|
let diskprefixes = [];
|
||||||
for (let resourceName of Object.keys(resourceMeta)) {
|
for (let resourceName of Object.keys(resourceMeta)) {
|
||||||
if (resourceMeta[resourceName].type === "numeric") {
|
used[resourceName] = 0;
|
||||||
used[resourceName] = 0;
|
if (resourceMeta[resourceName].type === "storage") {
|
||||||
}
|
for (let diskPrefix of resourceMeta[resourceName].disks) {
|
||||||
else if (resourceMeta[resourceName].type === "disk") {
|
diskprefixes.push(diskPrefix);
|
||||||
resourceMeta[resourceName].storages.forEach((element) => {
|
}
|
||||||
used[element] = 0;
|
|
||||||
});
|
|
||||||
diskprefixes.push(resourceName);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (instance of response.data.data) {
|
for (instance of response.data.data) {
|
||||||
|
Loading…
Reference in New Issue
Block a user