monex
Version:
Execute one or multiple scripts, interactively or in daemon mode, and restart them whenever they crash or a watched file changes.
60 lines (59 loc) • 1.9 kB
JavaScript
/* IMPORT */
import Table from 'cli-table';
import prettyBytes from 'pretty-bytes';
import prettyMs from 'pretty-ms';
import { color } from 'specialist';
/* MAIN */
//TODO: Show per-worker stats also
const Formatter = {
/* FORMATTERS API */
formatters: {
toPercentage: (value) => {
if (!value)
return '0%';
return `${String(Number(Number(value).toFixed(2)))}%`;
},
toSize: (bytes) => {
return prettyBytes(Number(bytes)).replace(/\s/g, '');
},
toStatus: (isOK) => {
if (isOK)
return color.green('OK');
return color.red('OFFLINE');
},
toTime: (ms) => {
return prettyMs(Number(ms), { colonNotation: true, secondsDecimalDigits: 0 });
}
},
/* API */
formatJSON: (stats) => {
return JSON.stringify(stats);
},
formatTable: (stats) => {
const { toPercentage, toSize, toStatus, toTime } = Formatter.formatters;
const table = new Table({
head: ['PID', 'Name', 'Status', 'Workers', 'Restarts', 'Uptime', 'CPU', 'MEM'].map(head => color.black.bold(head)),
colAligns: ['middle', 'left', 'middle', 'middle', 'middle', 'right', 'right', 'right']
});
stats.forEach(stat => {
table.push([
String(stat.pid),
stat.name,
toStatus(stat.online),
String(stat.cluster),
String(stat.restarts),
toTime(stat.uptime),
toPercentage(stat.cpu),
toSize(stat.memory)
]);
});
return table.toString();
},
format: (stats, pretty) => {
if (pretty)
return Formatter.formatTable(stats);
return Formatter.formatJSON(stats);
}
};
/* EXPORT */
export default Formatter;