mirror of
https://github.com/HirziDevs/PteroStats
synced 2026-01-09 07:33:46 +00:00
feat: update resource unit conversion, add "byte" and "percentage" as allowed values
This commit is contained in:
@@ -69,7 +69,7 @@ nodes_settings:
|
||||
details: true # enable nodes details i.e memory and disk usage
|
||||
servers: true
|
||||
location: true
|
||||
unit: "mb" # Allowed values- "gb", "mb", "tb", or "%"
|
||||
unit: "mb" # Allowed values: "byte" or "percentage"
|
||||
limit: 100
|
||||
|
||||
# Panel Users and Servers Settings
|
||||
|
||||
@@ -1,20 +1,15 @@
|
||||
module.exports = function convertUnits(value1, value2, unit) {
|
||||
unit = unit.toUpperCase();
|
||||
const units = ['MB', 'GB', 'TB'];
|
||||
const conversionFactors = {
|
||||
'MB': 1,
|
||||
'GB': 1 / 1024,
|
||||
'TB': 1 / (1024 * 1024)
|
||||
};
|
||||
|
||||
if (unit === '%' || unit === "PERCENTAGE" || unit === "PERCENT") {
|
||||
const percentage = Math.floor((value1 / value2) * 100);
|
||||
return `${!percentage ? 0 : percentage}%`;
|
||||
} else if (units.includes(unit)) {
|
||||
const formatValue = (value) => Number.isInteger(value) ? value.toLocaleString() : value.toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2 });
|
||||
|
||||
return `${formatValue(value1 * conversionFactors[unit])} ${unit}/${(value2 === 0 || value2 < 1) ? "Unlimited" : formatValue(value2 * conversionFactors[unit]) + " " + unit}`;
|
||||
} else {
|
||||
return `${value1.toLocaleString()} MB/${value2.toLocaleString()} MB`;
|
||||
}
|
||||
const prettyBytes = require('prettier-bytes');
|
||||
|
||||
module.exports = function convertUnits(value1, value2, unit) {
|
||||
unit = unit.toUpperCase();
|
||||
switch (unit) {
|
||||
case 'PERCENTAGE':
|
||||
case 'PERCENT':
|
||||
const percentage = Math.floor((value1 / value2) * 100);
|
||||
return `${!percentage ? 0 : percentage}%`;
|
||||
case 'BYTE':
|
||||
return `${prettyBytes(value1)} / ${prettyBytes(value2)}`;
|
||||
default:
|
||||
return `${value1.toLocaleString()} ${unit}/${value2.toLocaleString()} ${unit}`;
|
||||
}
|
||||
}
|
||||
@@ -17,7 +17,8 @@
|
||||
"cli-color": "2.0.4",
|
||||
"discord.js": "14.16.1",
|
||||
"dotenv": "16.4.5",
|
||||
"js-yaml": "4.1.0"
|
||||
"js-yaml": "4.1.0",
|
||||
"prettier-bytes": "1.0.4"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
|
||||
Reference in New Issue
Block a user