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 });
} catch (error) {
this.handleError(error, res);
this.server.client.util.handleError(error);
}
});
this.router.get('/moderations/:id?', async (req: Req, res) => {
try {
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) {
const filtered = moderations.filter((moderation) => moderation.logID === req.params.id);
res.status(200).json({ code: this.constants.codes.SUCCESS, message: { filtered } });
@ -43,7 +42,6 @@ export default class Account extends Route {
}
} catch (error) {
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.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,
message: {
cpu: Number(`${cpuUsage.split('\n')[0] || '0'}`),
ram: (Number(ramUsage) * 1000) / 1024 / 1024,
disk: Number(diskUsage) / 1024 / 1024,
disk: Number(storage) / 1024 / 1024,
},
});
} catch (error) {
this.handleError(error, res);
this.server.client.util.handleError(error);
}
});
}

View File

@ -8,33 +8,45 @@ export default class Root extends Route {
}
public bind() {
this.router.get('/', async (req, res) => {
this.router.get('/', async (_req, res) => {
try {
const date = new Date();
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 technicians = accounts.filter((account) => account?.permissions?.director === true).length;
const staff = accounts.filter((account) => account?.permissions?.staff === true).length;
const response = {
nodeVersion: process.version,
uptime: process.uptime(),
server: {
users: accounts.length,
administrators,
csd: {
nodeVersion: process.version,
uptime: process.uptime(),
},
stats: {
uptime: os.uptime(),
loadAverage: os.loadavg(),
cpuModel: os.cpus()[0].model,
cpuClock: os.cpus()[0].speed / 1000,
cpuCores: os.cpus().length,
hostname: os.hostname(),
ipv4: os.networkInterfaces().enp0s3.filter((r) => r.family === 'IPv4')[0].address,
server: {
userCount: accounts.length,
administratorCount: administrators,
technicianCount: technicians,
staffCount: staff,
stats: {
uptime: os.uptime(),
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 });
} catch (error) {
this.handleError(error, res);
this.server.client.util.handleError(error);
}
});
}