Skip to content

Commit 520cbda

Browse files
committed
fix(web): apply dynamic step calculation to all locations
Signed-off-by: Shenhan11 <1670285296@qq.com>
1 parent 790a7b9 commit 520cbda

6 files changed

Lines changed: 27 additions & 23 deletions

File tree

packages/web/projects/vgpu/hooks/useInstantVector.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import cardApi from '~/vgpu/api/card';
22
import { onMounted, ref, watch, watchEffect } from 'vue';
3-
import { timeParse } from '@/utils';
3+
import { timeParse, calculatePrometheusStep } from '@/utils';
44

55
const useInstantVector = (configs, parseQuery = (query) => query, times) => {
66
const data = ref(configs);
@@ -38,7 +38,7 @@ const useInstantVector = (configs, parseQuery = (query) => query, times) => {
3838
range: {
3939
start: timeParse(times?.value[0]),
4040
end: timeParse(times?.value[1]),
41-
step: '1m',
41+
step: calculatePrometheusStep(times?.value[0], times?.value[1]),
4242
},
4343
});
4444

@@ -63,7 +63,7 @@ const useInstantVector = (configs, parseQuery = (query) => query, times) => {
6363
range: {
6464
start: timeParse(times.value[0]),
6565
end: timeParse(times.value[1]),
66-
step: '1m',
66+
step: calculatePrometheusStep(times.value[0], times.value[1]),
6767
},
6868
});
6969

packages/web/projects/vgpu/hooks/useRangeVector.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
import cardApi from '~/vgpu/api/card';
22
import { ref, watchEffect } from 'vue';
3-
import { timeParse } from '@/utils';
3+
import { timeParse, calculatePrometheusStep } from '@/utils';
44

55
const useInstantVector = (configs, parseQuery = (query) => query) => {
66
const data = ref(configs);
77

88
const fetchData = async () => {
99
const start = new Date();
1010
start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
11+
const end = new Date();
1112

1213
const reqs = configs.map(({ query }) =>
1314
cardApi.getRangeVector({
1415
range: {
1516
start: timeParse(start),
16-
end: timeParse(new Date()),
17-
step: '1m',
17+
end: timeParse(end),
18+
step: calculatePrometheusStep(start, end),
1819
},
1920
query: item.query.replace('$node', detail.value.name),
2021
}),

packages/web/projects/vgpu/views/card/admin/Detail.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ import useInstantVector from '~/vgpu/hooks/useInstantVector';
9999
import { QuestionFilled } from '@element-plus/icons-vue';
100100
import EchartsPlus from '@/components/Echarts-plus.vue';
101101
import cardApi from '~/vgpu/api/card';
102-
import { timeParse } from '@/utils';
102+
import { timeParse, calculatePrometheusStep } from '@/utils';
103103
import { getLineOptions } from '~/vgpu/views/monitor/overview/getOptions';
104104
import { getLineOptions as getLineOptions2 } from '~/vgpu/components/config';
105105
import TimeSelect from '~/vgpu/components/timeSelect.vue';
@@ -304,7 +304,7 @@ const fetchLineData = async () => {
304304
range: {
305305
start: timeParse(times.value[0]),
306306
end: timeParse(times.value[1]),
307-
step: '1m',
307+
step: calculatePrometheusStep(times.value[0], times.value[1]),
308308
},
309309
query: item.query.replaceAll(`$deviceuuid`, route.params.uuid),
310310
})

packages/web/projects/vgpu/views/monitor/overview/index.vue

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -378,19 +378,12 @@ const nodeTotalTop = {
378378
const rangeConfig = ref(rangeConfigInit);
379379
380380
const fetchRangeData = () => {
381-
// Calculate dynamic step based on time range to avoid exceeding Prometheus 11,000 data points limit
382-
const step = calculatePrometheusStep(times.value[0], times.value[1]);
383-
console.log('[Prometheus Step]', {
384-
start: times.value[0],
385-
end: times.value[1],
386-
calculatedStep: step,
387-
});
388381
389382
const params = {
390383
range: {
391384
start: timeParse(times.value[0]),
392385
end: timeParse(times.value[1]),
393-
step: step,
386+
step: calculatePrometheusStep(times.value[0], times.value[1]),
394387
},
395388
};
396389

packages/web/projects/vgpu/views/task/admin/Detail.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ import { onMounted, ref, watch, watchEffect } from 'vue';
6464
import useInstantVector from '~/vgpu/hooks/useInstantVector';
6565
import cardApi from '~/vgpu/api/card';
6666
import { QuestionFilled } from '@element-plus/icons-vue';
67-
import { roundToDecimal, timeParse, calculateDuration } from '@/utils';
67+
import { roundToDecimal, timeParse, calculateDuration, calculatePrometheusStep } from '@/utils';
6868
import taskApi from '~/vgpu/api/task';
6969
import BlockBox from '@/components/BlockBox.vue';
7070
import Gauge from '~/vgpu/components/gauge.vue';
@@ -246,7 +246,7 @@ const fetchLineData = async () => {
246246
range: {
247247
start: timeParse(times.value[0]),
248248
end: timeParse(times.value[1]),
249-
step: '1m',
249+
step: calculatePrometheusStep(times.value[0], times.value[1]),
250250
},
251251
query: item.query
252252
.replaceAll(`$container`, detail.value.name)

packages/web/src/utils/index.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -573,15 +573,20 @@ export const clearEdges = (items) =>
573573
export const calculatePrometheusStep = (startTime, endTime, maxPoints = 11000) => {
574574
const start = new Date(startTime);
575575
const end = new Date(endTime);
576+
577+
if (isNaN(start.getTime()) || isNaN(end.getTime())) {
578+
return '1m';
579+
}
580+
576581
const durationMs = end.getTime() - start.getTime();
577-
578-
if (durationMs <= 0) {
582+
583+
if (durationMs <= 0 || !isFinite(durationMs)) {
579584
return '1m';
580585
}
581-
586+
582587
const durationMinutes = durationMs / (1000 * 60);
583588
const minStepMinutes = Math.ceil(durationMinutes / maxPoints);
584-
589+
585590
if (durationMinutes <= 15) {
586591
// ≤ 15 minutes: second-level precision
587592
const points15s = Math.ceil(durationMinutes / 0.25);
@@ -606,8 +611,13 @@ export const calculatePrometheusStep = (startTime, endTime, maxPoints = 11000) =
606611
if (dataPointsWith24h <= maxPoints) {
607612
return '24h';
608613
}
609-
614+
610615
const minStepDays = Math.ceil(dataPointsWith24h / maxPoints);
616+
617+
if (!isFinite(minStepDays) || isNaN(minStepDays) || minStepDays <= 0) {
618+
return '24h';
619+
}
620+
611621
return `${minStepDays * 24}h`;
612622
}
613623
};

0 commit comments

Comments
 (0)