system: add disk usage task

This commit is contained in:
Girish Ramakrishnan
2025-07-16 23:09:06 +02:00
parent 11a6cf8236
commit 5539f74bea
10 changed files with 150 additions and 94 deletions

View File

@@ -3,8 +3,6 @@
exports = module.exports = {
reboot,
getInfo,
getDiskUsage,
updateDiskUsage,
getMemory,
getLogs,
getLogStream,
@@ -12,6 +10,7 @@ exports = module.exports = {
getMetricStream,
getBlockDevices,
getFilesystems,
getFilesystemUsage,
getCpus,
};
@@ -37,20 +36,6 @@ async function getInfo(req, res, next) {
next(new HttpSuccess(200, { info }));
}
async function getDiskUsage(req, res, next) {
const [error, result] = await safe(system.getDiskUsage());
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, { usage: result }));
}
async function updateDiskUsage(req, res, next) {
const [error, taskId] = await safe(system.startUpdateDiskUsage());
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(201, { taskId }));
}
async function getMemory(req, res, next) {
const [error, result] = await safe(system.getMemory());
if (error) return next(BoxError.toHttpError(error));
@@ -183,3 +168,34 @@ async function getFilesystems(req, res, next) {
next(new HttpSuccess(200, { filesystems }));
}
async function getFilesystemUsage(req, res, next) {
if (typeof req.query.filesystem !== 'string') return next(new HttpError(400, 'getFilesystemUsage'));
if (req.headers.accept !== 'text/event-stream') return next(new HttpError(400, 'This API call requires EventStream'));
const [error, task] = await safe(system.getFilesystemUsage(req.query.filesystem));
if (error) return next(BoxError.toHttpError(error));
res.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
'X-Accel-Buffering': 'no', // disable nginx buffering
'Access-Control-Allow-Origin': '*'
});
res.write('retry: 30000\n'); // client should .close() to prevent reconnect within 30s
res.on('close', () => task.stop());
task.on('data', function (type, data) {
const obj = { type, ...data };
const sse = `data: ${JSON.stringify(obj)}\n\n`;
res.write(sse);
});
task.on('done', function (error) {
const obj = { type: 'done', ...error };
const sse = `data: ${JSON.stringify(obj)}\n\n`;
res.write(sse);
res.end();
});
task.start();
}