修改精度

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

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

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

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

@ -588,3 +588,28 @@ export function parseUrl(url) {
return { pathname, query }; 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