1
0
Fork 0

changes to server data and moderation routes

refactor/models
Matthew 2020-07-04 22:58:09 -04:00
parent 1b0f099885
commit 8495560e18
No known key found for this signature in database
GPG Key ID: 210AF32ADE3B5C4B
2 changed files with 38 additions and 21 deletions

View File

@ -27,14 +27,13 @@ export default class Account extends Route {
res.status(200).json({ code: this.constants.codes.SUCCESS, message: acc }); res.status(200).json({ code: this.constants.codes.SUCCESS, message: acc });
} catch (error) { } catch (error) {
this.handleError(error, res); this.handleError(error, res);
this.server.client.util.handleError(error);
} }
}); });
this.router.get('/moderations/:id?', async (req: Req, res) => { this.router.get('/moderations/:id?', async (req: Req, res) => {
try { try {
const moderations = await this.server.client.db.Moderation.find({ username: req.account.username }); const moderations = await this.server.client.db.Moderation.find({ username: req.account.username });
if (!moderations.length) res.sendStatus(204); if (!moderations.length) return res.status(204).json({ code: this.constants.codes.NOT_FOUND, message: null });
if (req.params.id) { if (req.params.id) {
const filtered = moderations.filter((moderation) => moderation.logID === req.params.id); const filtered = moderations.filter((moderation) => moderation.logID === req.params.id);
res.status(200).json({ code: this.constants.codes.SUCCESS, message: { filtered } }); res.status(200).json({ code: this.constants.codes.SUCCESS, message: { filtered } });
@ -43,7 +42,6 @@ export default class Account extends Route {
} }
} catch (error) { } catch (error) {
this.handleError(error, res); this.handleError(error, res);
this.server.client.util.handleError(error);
} }
}); });
@ -54,16 +52,23 @@ export default class Account extends Route {
this.server.client.util.exec(`memory ${req.account.username}`), this.server.client.util.exec(`memory ${req.account.username}`),
this.server.client.redis.get(`storage-${req.account.username}`), this.server.client.redis.get(`storage-${req.account.username}`),
]); ]);
let storage: string;
if (req.query?.cache === 'true') {
const val = await this.server.client.util.exec(`du -sb /home/${req.account.username}`);
// eslint-disable-next-line prefer-destructuring
storage = val.split('\t')[0];
} else {
storage = diskUsage;
}
res.status(200).json({ code: this.constants.codes.SUCCESS, res.status(200).json({ code: this.constants.codes.SUCCESS,
message: { message: {
cpu: Number(`${cpuUsage.split('\n')[0] || '0'}`), cpu: Number(`${cpuUsage.split('\n')[0] || '0'}`),
ram: (Number(ramUsage) * 1000) / 1024 / 1024, ram: (Number(ramUsage) * 1000) / 1024 / 1024,
disk: Number(diskUsage) / 1024 / 1024, disk: Number(storage) / 1024 / 1024,
}, },
}); });
} catch (error) { } catch (error) {
this.handleError(error, res); this.handleError(error, res);
this.server.client.util.handleError(error);
} }
}); });
} }

View File

@ -8,33 +8,45 @@ export default class Root extends Route {
} }
public bind() { public bind() {
this.router.get('/', async (req, res) => { this.router.get('/', async (_req, res) => {
try { try {
const date = new Date(); const date = new Date();
date.setSeconds(-process.uptime()); date.setSeconds(-process.uptime());
const accounts = await this.server.client.db.Account.find(); const accounts = await this.server.client.db.Account.find().lean().exec();
const administrators = accounts.filter((account) => account.root === true).length; const administrators = accounts.filter((account) => account.root === true).length;
const technicians = accounts.filter((account) => account?.permissions?.director === true).length;
const staff = accounts.filter((account) => account?.permissions?.staff === true).length;
const response = { const response = {
nodeVersion: process.version, csd: {
uptime: process.uptime(), nodeVersion: process.version,
server: { uptime: process.uptime(),
users: accounts.length,
administrators,
}, },
stats: { server: {
uptime: os.uptime(), userCount: accounts.length,
loadAverage: os.loadavg(), administratorCount: administrators,
cpuModel: os.cpus()[0].model, technicianCount: technicians,
cpuClock: os.cpus()[0].speed / 1000, staffCount: staff,
cpuCores: os.cpus().length, stats: {
hostname: os.hostname(), uptime: os.uptime(),
ipv4: os.networkInterfaces().enp0s3.filter((r) => r.family === 'IPv4')[0].address, loadAverages: os.loadavg(),
hostname: os.hostname(),
ipv4Address: os.networkInterfaces().enp0s3.filter((r) => r.family === 'IPv4')[0].address,
operatingSystem: {
platform: os.platform(),
kernelVersion: os.version(),
release: os.release(),
},
cpu: {
model: os.cpus()[0].model,
clockSpeed: os.cpus()[0].speed / 1000,
coreCount: os.cpus().length,
},
},
}, },
}; };
res.status(200).json({ code: this.constants.codes.SUCCESS, message: response }); res.status(200).json({ code: this.constants.codes.SUCCESS, message: response });
} catch (error) { } catch (error) {
this.handleError(error, res); this.handleError(error, res);
this.server.client.util.handleError(error);
} }
}); });
} }