diff --git a/BACKEND/app/ultils/helper.ts b/BACKEND/app/ultils/helper.ts index d8c18a1..42efa7c 100644 --- a/BACKEND/app/ultils/helper.ts +++ b/BACKEND/app/ultils/helper.ts @@ -1457,13 +1457,19 @@ export async function checkStationActive(stationId: string): Promise { 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 { 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 = { 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]}` } diff --git a/FRONTEND/src/untils/helper.ts b/FRONTEND/src/untils/helper.ts index aa8470e..e9517d2 100644 --- a/FRONTEND/src/untils/helper.ts +++ b/FRONTEND/src/untils/helper.ts @@ -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 = { 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]}`; }