feat: SMF数据单位转换MB显示

This commit is contained in:
TsMask
2025-03-12 11:12:30 +08:00
parent a24122ecd7
commit 37a1f748e7
3 changed files with 33 additions and 18 deletions

View File

@@ -192,17 +192,31 @@ export function parseSizeFromBits(bits: number | string): string {
/** /**
* 字节数转换单位 * 字节数转换单位
* @param byte 字节Byte大小 64009540 = 512.08 MB * @param byte 字节Byte大小 64009540 = 512.08 MB
* @param unit 指定单位 B / KB / MB / GB / TB / PB / EB / ZB / YB
* @returns xx B / KB / MB / GB / TB / PB / EB / ZB / YB * @returns xx B / KB / MB / GB / TB / PB / EB / ZB / YB
*/ */
export function parseSizeFromByte(byte: number | string): string { export function parseSizeFromByte(
byte: number | string,
unit?: string
): string {
byte = Number(byte) || 0; byte = Number(byte) || 0;
if (byte <= 0) return '0 B'; if (byte <= 0) return '0 B';
const units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; const units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
const unitIndex = Math.floor(Math.log2(byte) / 10); let unitIndex = 0;
const unti = units[unitIndex]; let unitStr = 'B';
if (unit) {
const index = units.indexOf(unit);
if (index > -1) {
unitIndex = index;
unitStr = unit;
}
} else {
unitIndex = Math.floor(Math.log2(byte) / 10);
unitStr = units[unitIndex];
}
const value = byte / Math.pow(1000, unitIndex); const value = byte / Math.pow(1000, unitIndex);
if (unitIndex > 0) { if (unitIndex > 0) {
return `${value.toFixed(2)} ${unti}`; return `${value.toFixed(2)} ${unitStr}`;
} }
return `${value} ${unti}`; return `${value} ${unitStr}`;
} }

View File

@@ -22,6 +22,7 @@ import saveAs from 'file-saver';
import { listTenant } from '@/api/system/tenant'; import { listTenant } from '@/api/system/tenant';
import dayjs, { Dayjs } from 'dayjs'; import dayjs, { Dayjs } from 'dayjs';
import { useClipboard } from '@vueuse/core'; import { useClipboard } from '@vueuse/core';
import { parseSizeFromByte } from '@/utils/parse-utils';
const { copy } = useClipboard({ legacy: true }); const { copy } = useClipboard({ legacy: true });
const { t } = useI18n(); const { t } = useI18n();
const ws = new WS(); const ws = new WS();
@@ -172,7 +173,7 @@ let tableColumns = ref<ColumnsType>([
} }
} }
} }
return dataVolumeUplink; return parseSizeFromByte(dataVolumeUplink, 'MB');
}, },
}, },
{ {
@@ -197,7 +198,7 @@ let tableColumns = ref<ColumnsType>([
} }
} }
} }
return dataVolumeDownlink; return parseSizeFromByte(dataVolumeDownlink, 'MB');
}, },
}, },
{ {
@@ -222,7 +223,7 @@ let tableColumns = ref<ColumnsType>([
} }
} }
} }
return dataTotalVolume; return parseSizeFromByte(dataTotalVolume, 'MB');
}, },
}, },
{ {
@@ -855,16 +856,16 @@ onBeforeUnmount(() => {
</strong> --> </strong> -->
<div> <div>
<div> <div>
<span>Data Total Volume: </span> <span>Data Volume Uplink: </span>
<span>{{ udata.dataTotalVolume }}</span> <span>{{ parseSizeFromByte(udata.dataVolumeUplink, 'MB') }}</span>
</div> </div>
<div> <div>
<span>Data Volume Downlink: </span> <span>Data Volume Downlink: </span>
<span>{{ udata.dataVolumeDownlink }}</span> <span>{{ parseSizeFromByte(udata.dataVolumeDownlink, 'MB') }}</span>
</div> </div>
<div> <div>
<span>Data Volume Uplink: </span> <span>Data Total Volume: </span>
<span>{{ udata.dataVolumeUplink }}</span> <span>{{ parseSizeFromByte(udata.dataTotalVolume, 'MB') }}</span>
</div> </div>
<!-- <div> <!-- <div>
<span>Time: </span> <span>Time: </span>

View File

@@ -69,8 +69,8 @@ const option = {
} else { } else {
downlinkValue = params[1].value; downlinkValue = params[1].value;
} }
const uplinkValueF = parseSizeFromByte(uplinkValue); const uplinkValueF = parseSizeFromByte(uplinkValue, 'MB');
const downlinkValueF = parseSizeFromByte(downlinkValue); const downlinkValueF = parseSizeFromByte(downlinkValue, 'MB');
return ` return `
<div style="font-weight: bold;">${title}</div> <div style="font-weight: bold;">${title}</div>
<div>Downlink: ${downlinkValueF}</div> <div>Downlink: ${downlinkValueF}</div>
@@ -466,8 +466,8 @@ function fnRanderChartDataUpdate() {
downlinkTotal += dataVolumeDownlinkYSeriesData[index]; downlinkTotal += dataVolumeDownlinkYSeriesData[index];
} }
state.dataUsage = [ state.dataUsage = [
parseSizeFromByte(uplinkTotal), parseSizeFromByte(uplinkTotal, 'MB'),
parseSizeFromByte(downlinkTotal), parseSizeFromByte(downlinkTotal, 'MB'),
]; ];
} }
@@ -684,7 +684,7 @@ onBeforeUnmount(() => {
<!-- 图数据 --> <!-- 图数据 -->
<div ref="cdrChartDom" style="height: 600px; width: 100%"></div> <div ref="cdrChartDom" style="height: 600px; width: 100%"></div>
<a-descriptions title="Data Usage" bordered :column="2"> <a-descriptions title="Data Usage" bordered :column="2" style="width: 60%;">
<a-descriptions-item label="Total Uplink"> <a-descriptions-item label="Total Uplink">
{{ state.dataUsage[0] }} {{ state.dataUsage[0] }}
</a-descriptions-item> </a-descriptions-item>