Update convert kilobytes

This commit is contained in:
nguyentrungthat 2026-02-10 09:52:19 +07:00
parent c7094f0fd8
commit 215cd4fbe4
2 changed files with 56 additions and 40 deletions

View File

@ -1457,13 +1457,19 @@ export async function checkStationActive(stationId: string): Promise<boolean> {
export function isRamSufficient(deviceRam: string, defaultRamLimit: string): boolean {
if (!defaultRamLimit || !deviceRam) return false
// ---- Parse + cộng tất cả phần RAM của device ----
const parts = deviceRam.split('/')
if (parts.length === 0) return false
let deviceRamKB = 0
const totalRamStr = parts[0].trim() // lấy phần total (thường là trước dấu '/')
const totalRamKB = convertToKilobytes(totalRamStr)
for (const part of parts) {
const trimmed = part.trim()
deviceRamKB += convertToKilobytes(trimmed)
}
// ---- Parse RAM mặc định ----
const defaultRamKB = convertToKilobytes(defaultRamLimit)
return totalRamKB >= defaultRamKB
return deviceRamKB >= defaultRamKB
}
export function convertToKilobytes(input: string): number {
@ -1526,21 +1532,11 @@ async function loadKeywordRules(log: string): Promise<KeywordRule[]> {
export function convertFromKilobytesString(input: string, decimals = 0): string {
if (!input) return '0 KB'
const parts = input.split('/')
if (parts.length === 0) return ''
if (parts.length === 0) return '0 KB'
const totalRamStr = parts[0].trim()
const trimmed = totalRamStr.trim().toUpperCase()
// Bắt dạng: 491520K, 1024KB, 1M, 1.5G, 2T
const match = trimmed.match(/^([\d.]+)\s*(K|M|G|T)?B?$/)
if (!match) return '0 KB'
const value = Number.parseFloat(match[1])
const unit = match[2] || 'K'
// Quy tất cả về KB
// ---- Step 1: parse + cộng tất cả về KB ----
const toKB: Record<string, number> = {
K: 1,
M: 1024,
@ -1548,16 +1544,31 @@ export function convertFromKilobytesString(input: string, decimals = 0): string
T: 1024 ** 3,
}
let kilobytes = value * toKB[unit]
let totalKB = 0
// ---- Convert KB -> đơn vị đẹp nhất ----
for (const part of parts) {
const trimmed = part.trim().toUpperCase()
const match = trimmed.match(/^([\d.]+)\s*(K|M|G|T)?B?$/)
if (!match) continue
const value = Number.parseFloat(match[1])
const unit = match[2] || 'K'
totalKB += value * toKB[unit]
}
if (totalKB === 0) return '0 KB'
// ---- Step 2: convert KB -> đơn vị đẹp nhất ----
const units = ['KB', 'MB', 'GB', 'TB']
let unitIndex = 0
let displayValue = totalKB
while (kilobytes >= 1024 && unitIndex < units.length - 1) {
kilobytes /= 1024
while (displayValue >= 1024 && unitIndex < units.length - 1) {
displayValue /= 1024
unitIndex++
}
return `${kilobytes.toFixed(decimals)} ${units[unitIndex]}`
return `${displayValue.toFixed(decimals)} ${units[unitIndex]}`
}

View File

@ -202,21 +202,11 @@ export function convertFromKilobytesString(
decimals = 0
): string {
if (!input) return "0 KB";
const parts = input.split("/");
if (parts.length === 0) return "";
if (parts.length === 0) return "0 KB";
const totalRamStr = parts[0].trim();
const trimmed = totalRamStr.trim().toUpperCase();
// Bắt dạng: 491520K, 1024KB, 1M, 1.5G, 2T
const match = trimmed.match(/^([\d.]+)\s*(K|M|G|T)?B?$/);
if (!match) return "0 KB";
const value = Number.parseFloat(match[1]);
const unit = match[2] || "K";
// Quy tất cả về KB
// ---- Step 1: parse + cộng tất cả về KB ----
const toKB: Record<string, number> = {
K: 1,
M: 1024,
@ -224,16 +214,31 @@ export function convertFromKilobytesString(
T: 1024 ** 3,
};
let kilobytes = value * toKB[unit];
let totalKB = 0;
// ---- Convert KB -> đơn vị đẹp nhất ----
for (const part of parts) {
const trimmed = part.trim().toUpperCase();
const match = trimmed.match(/^([\d.]+)\s*(K|M|G|T)?B?$/);
if (!match) continue;
const value = Number.parseFloat(match[1]);
const unit = match[2] || "K";
totalKB += value * toKB[unit];
}
if (totalKB === 0) return "0 KB";
// ---- Step 2: convert KB -> đơn vị đẹp nhất ----
const units = ["KB", "MB", "GB", "TB"];
let unitIndex = 0;
let displayValue = totalKB;
while (kilobytes >= 1024 && unitIndex < units.length - 1) {
kilobytes /= 1024;
while (displayValue >= 1024 && unitIndex < units.length - 1) {
displayValue /= 1024;
unitIndex++;
}
return `${kilobytes.toFixed(decimals)} ${units[unitIndex]}`;
return `${displayValue.toFixed(decimals)} ${units[unitIndex]}`;
}