refactor: extract transformPortSpeed

This commit is contained in:
yangsy
2025-09-26 15:00:36 +08:00
parent 24a202ee55
commit bdb321a407
2 changed files with 49 additions and 49 deletions

View File

@@ -1,51 +1,3 @@
<script lang="ts">
const transformPortSpeed = (portInfo: NdmSwitchPortInfo, type: 'in' | 'out' | 'total'): string => {
const units = ['B/s', 'KB/s', 'MB/s', 'GB/s', 'TB/s'];
const { inBytes, lastInBytes, outBytes, lastOutBytes, inFlow, outFlow, flow } = portInfo;
let result: number = 0;
if (inFlow && outFlow && flow) {
if (type === 'in') {
result = inFlow;
}
if (type === 'out') {
result = outFlow;
}
if (type === 'total') {
result = flow;
}
} else {
let dInBytes = 0;
let dOutBytes = 0;
if (inBytes < lastInBytes) {
dInBytes = inBytes + JAVA_UNSIGNED_INTEGER_MAX_VALUE - lastInBytes;
} else {
dInBytes = inBytes - lastInBytes;
}
if (outBytes < lastOutBytes) {
dOutBytes = outBytes + JAVA_UNSIGNED_INTEGER_MAX_VALUE - lastOutBytes;
} else {
dOutBytes = outBytes - lastOutBytes;
}
if (type === 'in') {
result = dInBytes;
}
if (type === 'out') {
result = dOutBytes;
}
if (type === 'total') {
result = dInBytes + dOutBytes;
}
result /= NDM_SWITCH_PROBE_INTERVAL;
}
let index = 0;
while (result >= 1024 && index < units.length - 1) {
result /= 1024;
index++;
}
return `${result.toFixed(3)} ${units[index]}`;
};
</script>
<script setup lang="ts">
/**
* 交换机端口布局组件
@@ -63,8 +15,8 @@ const transformPortSpeed = (portInfo: NdmSwitchPortInfo, type: 'in' | 'out' | 't
* - 响应式数据处理和布局
*/
import { transformPortSpeed } from '../../helper';
import type { NdmSwitchPortInfo } from '@/apis/domains';
import { JAVA_UNSIGNED_INTEGER_MAX_VALUE, NDM_SWITCH_PROBE_INTERVAL } from '@/constants';
import { NCard, NGrid, NGridItem, NPopover } from 'naive-ui';
import { computed, toRefs } from 'vue';

View File

@@ -0,0 +1,48 @@
import type { NdmSwitchPortInfo } from '@/apis/domains';
import { JAVA_UNSIGNED_INTEGER_MAX_VALUE, NDM_SWITCH_PROBE_INTERVAL } from '@/constants';
export const transformPortSpeed = (portInfo: NdmSwitchPortInfo, type: 'in' | 'out' | 'total'): string => {
const units = ['B/s', 'KB/s', 'MB/s', 'GB/s', 'TB/s'];
const { inBytes, lastInBytes, outBytes, lastOutBytes, inFlow, outFlow, flow } = portInfo;
let result: number = 0;
if (inFlow && outFlow && flow) {
if (type === 'in') {
result = inFlow;
}
if (type === 'out') {
result = outFlow;
}
if (type === 'total') {
result = flow;
}
} else {
let dInBytes = 0;
let dOutBytes = 0;
if (inBytes < lastInBytes) {
dInBytes = inBytes + JAVA_UNSIGNED_INTEGER_MAX_VALUE - lastInBytes;
} else {
dInBytes = inBytes - lastInBytes;
}
if (outBytes < lastOutBytes) {
dOutBytes = outBytes + JAVA_UNSIGNED_INTEGER_MAX_VALUE - lastOutBytes;
} else {
dOutBytes = outBytes - lastOutBytes;
}
if (type === 'in') {
result = dInBytes;
}
if (type === 'out') {
result = dOutBytes;
}
if (type === 'total') {
result = dInBytes + dOutBytes;
}
result /= NDM_SWITCH_PROBE_INTERVAL;
}
let index = 0;
while (result >= 1024 && index < units.length - 1) {
result /= 1024;
index++;
}
return `${result.toFixed(3)} ${units[index]}`;
};