Track per-device traffic totals and recover inventory state
This commit is contained in:
@@ -10,6 +10,40 @@ export function formatBytes(value) {
|
||||
return `${size.toFixed(index === 0 ? 0 : 1)} ${units[index]}`;
|
||||
}
|
||||
|
||||
const BYTE_STRING_PATTERN = /^\d+$/;
|
||||
|
||||
export function byteString(value) {
|
||||
const normalized = String(value ?? '0');
|
||||
return BYTE_STRING_PATTERN.test(normalized) ? BigInt(normalized) : 0n;
|
||||
}
|
||||
|
||||
export function formatByteString(value) {
|
||||
const bytes = byteString(value);
|
||||
const units = ['Б', 'КБ', 'МБ', 'ГБ', 'ТБ', 'ПБ', 'ЭБ'];
|
||||
let unit = 0;
|
||||
let divisor = 1n;
|
||||
while (bytes >= divisor * 1024n && unit < units.length - 1) {
|
||||
divisor *= 1024n;
|
||||
unit += 1;
|
||||
}
|
||||
if (unit === 0) return `${bytes} ${units[unit]}`;
|
||||
const tenths = (bytes * 10n + divisor / 2n) / divisor;
|
||||
return `${tenths / 10n},${tenths % 10n} ${units[unit]}`;
|
||||
}
|
||||
|
||||
export function sortDevicesByTraffic(devices, direction = 'desc') {
|
||||
const factor = direction === 'asc' ? 1 : -1;
|
||||
return (Array.isArray(devices) ? devices : [])
|
||||
.map((device, index) => ({ device, index }))
|
||||
.sort((left, right) => {
|
||||
const leftTotal = byteString(left.device.uploadBytes) + byteString(left.device.downloadBytes);
|
||||
const rightTotal = byteString(right.device.uploadBytes) + byteString(right.device.downloadBytes);
|
||||
if (leftTotal === rightTotal) return left.index - right.index;
|
||||
return (leftTotal < rightTotal ? -1 : 1) * factor;
|
||||
})
|
||||
.map(({ device }) => device);
|
||||
}
|
||||
|
||||
export function formatRelative(iso) {
|
||||
if (!iso) return "";
|
||||
const ts = new Date(iso).getTime();
|
||||
|
||||
Reference in New Issue
Block a user