修改精度

main
陈博文 3 days ago
parent 1bbfacbdfe
commit 49d76da34f

@ -2,23 +2,14 @@
<block-box :title="title">
<template #extra>
<el-radio-group v-model="tabActive" size="small" @change="handleTabChange">
<el-radio-button
v-for="{ tab, key } in config"
:label="tab"
:value="key"
/>
<el-radio-button v-for="{ tab, key } in config" :label="tab" :value="key" />
</el-radio-group>
</template>
<echarts-plus
:options="
getTopOptions(
currentConfig.find((item) => item.key === tabActive)?.data || [],
)
"
:onClick="handleClick"
style="min-height: 250px; height: 100%"
/>
<echarts-plus :options="getTopOptions(
currentConfig.find((item) => item.key === tabActive)?.data || [],
)
" :onClick="handleClick" style="min-height: 250px; height: 100%" />
</block-box>
</template>
@ -28,6 +19,7 @@ import { onMounted, ref } from 'vue';
import EchartsPlus from '@/components/Echarts-plus.vue';
import cardApi from '~/vgpu/api/card';
import { cloneDeep } from 'lodash';
import { formatSmartPercentage } from '@/utils';
const props = defineProps({
title: String,
@ -60,13 +52,14 @@ const getTopOptions = () => {
type: 'shadow',
},
formatter: function (params) {
// console.log(params, 'params')
var res = params[0].name + '<br/>';
for (var i = 0; i < params.length; i++) {
console.log(params[i].value, formatSmartPercentage(params[i].value), 'params')
res +=
params[i].marker +
params[i].seriesName +
(+params[i].value).toFixed(0) +
formatSmartPercentage(params[i].value) +
`${config.unit || '%'}<br/>`;
}
return res;

@ -481,14 +481,14 @@ const fetchRangeData = () => {
}
cardApi
.getRangeVector({
...params,
query: `sum({__name__=~"alert:.*:count"})`,
})
.then((res) => {
alarmData.value = res.data[0].values;
});
// cardApi
// .getRangeVector({
// ...params,
// query: `sum({__name__=~"alert:.*:count"})`,
// })
// .then((res) => {
// alarmData.value = res.data[0].values;
// });
};

@ -63,22 +63,22 @@ const topConfig = [
title: '任务资源申请 Top5',
key: 'apply',
config: [
{
tab: 'CPU',
key: 'cpu',
data: [],
nameKey: 'container_pod_uuid',
unit: '核',
query: 'topk(5, sum by(container_pod_uuid) (hami_container_vcore_allocated))',
},
{
tab: '内存',
key: 'internal',
data: [],
unit: 'GiB',
nameKey: 'container_pod_uuid',
query: 'topk(5, sum by(container_pod_uuid) (hami_container_vmemory_allocated))',
},
// {
// tab: 'CPU',
// key: 'cpu',
// data: [],
// nameKey: 'container_pod_uuid',
// unit: '',
// query: 'topk(5, sum by(container_pod_uuid) (hami_container_vcore_allocated))',
// },
// {
// tab: '',
// key: 'internal',
// data: [],
// unit: 'GiB',
// nameKey: 'container_pod_uuid',
// query: 'topk(5, sum by(container_pod_uuid) (hami_container_vmemory_allocated))',
// },
{
tab: '算力',
key: 'core',

@ -588,3 +588,28 @@ export function parseUrl(url) {
return { pathname, query };
}
export function formatSmartPercentage(value) {
if (value === 0) return '0';
// 整数直接返回
if (Number.isInteger(value)) return value.toString();
const str = value.toString();
const decimal = str.split('.')[1] || '';
// 统计前导0的个数
let leadingZeros = 0;
for (const char of decimal) {
if (char === '0') {
leadingZeros++;
} else {
break;
}
}
const keep = leadingZeros + 2; // 0 的个数 + 1 位有效数字 + 1 位补充位
const rounded = value.toFixed(keep);
return parseFloat(rounded).toString(); // 去除多余的 0 和小数点
}

Loading…
Cancel
Save