mirror of
https://github.com/labring/sealos.git
synced 2026-09-01 15:38:06 +08:00
fix(dbprovider): resource quantity resolved incorrectly (#6826)
* fix(dbprovider): resource quantity resolved incorrectly Signed-off-by: Nixieboluo <me@sagirii.me> * fixup! fix(dbprovider): resource quantity resolved incorrectly --------- Signed-off-by: Nixieboluo <me@sagirii.me>
This commit is contained in:
@@ -12,6 +12,7 @@ dayjs.extend(timezone);
|
||||
import yaml from 'js-yaml';
|
||||
import ini from 'ini';
|
||||
import { DBType, PodDetailType } from '@/types/db';
|
||||
import { Quantity, Scale } from '@sealos/shared';
|
||||
|
||||
export const formatTime = (time: string | number | Date, format = 'YYYY-MM-DD HH:mm:ss') => {
|
||||
return dayjs(time).tz('Asia/Shanghai').format(format);
|
||||
@@ -99,6 +100,8 @@ export const str2Num = (str?: string | number) => {
|
||||
* Format CPU value to standard C format
|
||||
* @param cpu CPU value, like "500m", "1", "2"
|
||||
* @returns Standardized CPU value with C suffix, like "0.5C", "1C", "2C"
|
||||
*
|
||||
* @deprecated We need to migrate all resource representations to Quantity (in the near future).
|
||||
*/
|
||||
export const cpuFormatToC = (cpu: string | number = '0'): string => {
|
||||
if (!cpu || cpu === '0') {
|
||||
@@ -123,40 +126,30 @@ export const cpuFormatToC = (cpu: string | number = '0'): string => {
|
||||
* Format memory value to standard Gi format
|
||||
* @param memory Memory value, like "512Mi", "1Gi", "2048Mi"
|
||||
* @returns Standardized memory value with Gi suffix, like "0.5Gi", "1Gi", "2Gi"
|
||||
*
|
||||
* @deprecated We need to migrate all resource representations to Quantity (in the near future).
|
||||
*/
|
||||
export const memoryFormatToGi = (memory: string | number = '0'): string => {
|
||||
if (!memory || memory === '0') {
|
||||
return '0Gi';
|
||||
}
|
||||
|
||||
let value: number;
|
||||
const memoryStr = memory.toString();
|
||||
|
||||
if (/Mi$/i.test(memoryStr)) {
|
||||
// Convert Mi to Gi
|
||||
value = parseFloat(memoryStr) / 1024;
|
||||
} else if (/Gi$/i.test(memoryStr)) {
|
||||
// Already in Gi
|
||||
value = parseFloat(memoryStr);
|
||||
} else if (/Ti$/i.test(memoryStr)) {
|
||||
// Convert Ti to Gi
|
||||
value = parseFloat(memoryStr) * 1024;
|
||||
} else if (/Ki$/i.test(memoryStr)) {
|
||||
// Convert Ki to Gi
|
||||
value = parseFloat(memoryStr) / 1024 / 1024;
|
||||
} else {
|
||||
// Assume the value is in bytes and convert to Gi
|
||||
value = parseFloat(memoryStr) / 1024 / 1024 / 1024;
|
||||
}
|
||||
|
||||
return `${value.toFixed(1)}Gi`;
|
||||
return Quantity.fromJSON(memory).formatForDisplay({
|
||||
scale: Scale.Giga,
|
||||
format: 'BinarySI',
|
||||
round: false,
|
||||
digits: 3
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* storage format
|
||||
* storage format ()
|
||||
*
|
||||
* @deprecated We need to migrate all resource representations to Quantity (in the near future).
|
||||
*/
|
||||
export const storageFormatToNum = (storage = '0') => {
|
||||
return +`${storage.replace(/gi/i, '')}`;
|
||||
return (
|
||||
Math.round(
|
||||
(Number(Quantity.fromJSON(storage).withFormat('BinarySI').scaledValue(Scale.Mega)) / 1024) *
|
||||
100
|
||||
) / 100
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -164,37 +157,26 @@ export const storageFormatToNum = (storage = '0') => {
|
||||
* @param value Storage value string
|
||||
* @param defaultValue Default value if parsing fails
|
||||
* @returns Storage value in Gi units
|
||||
*
|
||||
* @deprecated We need to migrate all resource representations to Quantity (in the near future).
|
||||
*/
|
||||
export const storageFormatToGi = (value: string | undefined, defaultValue: number = 0): number => {
|
||||
if (!value) return defaultValue;
|
||||
|
||||
const valueStr = value.toString();
|
||||
let numValue: number;
|
||||
|
||||
if (valueStr.endsWith('Gi')) {
|
||||
numValue = parseFloat(valueStr.slice(0, -2));
|
||||
} else if (valueStr.endsWith('Mi')) {
|
||||
numValue = parseInt(valueStr.slice(0, -2)) / 1024;
|
||||
} else if (valueStr.endsWith('Ti')) {
|
||||
numValue = parseFloat(valueStr.slice(0, -2)) * 1024;
|
||||
} else if (valueStr.endsWith('G')) {
|
||||
numValue = parseFloat(valueStr.slice(0, -1));
|
||||
} else if (valueStr.endsWith('M')) {
|
||||
numValue = parseInt(valueStr.slice(0, -1)) / 1024;
|
||||
} else if (valueStr.endsWith('T')) {
|
||||
numValue = parseFloat(valueStr.slice(0, -1)) * 1024;
|
||||
} else {
|
||||
numValue = parseFloat(valueStr);
|
||||
}
|
||||
|
||||
return isNaN(numValue) ? defaultValue : numValue;
|
||||
return (
|
||||
Math.round(
|
||||
(Number(Quantity.fromJSON(value).withFormat('BinarySI').scaledValue(Scale.Mega)) / 1024) * 100
|
||||
) / 100
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* print memory to Mi of Gi
|
||||
*
|
||||
* @deprecated We need to migrate all resource representations to Quantity (in the near future).
|
||||
*/
|
||||
export const printMemory = (val: number) => {
|
||||
return val >= 1024 ? `${Math.round(val / 1024)} Gi` : `${val} Mi`;
|
||||
return Quantity.newScaledQuantity(BigInt(val), Scale.Mega)
|
||||
.withFormat('BinarySI')
|
||||
.formatForDisplay();
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user