refactor(ops): 简化自动刷新定时器逻辑

- 合并双定时器为单一倒计时定时器
- 倒计时归零时触发数据刷新
- 添加自定义时间范围的安全回退
This commit is contained in:
IanShaw027
2026-01-15 22:07:23 +08:00
parent a478822b8e
commit 8b95d16220
4 changed files with 44 additions and 24 deletions

View File

@@ -355,23 +355,21 @@ const autoRefreshCountdown = ref(0)
// Used to trigger child component refreshes in a single shared cadence.
const dashboardRefreshToken = ref(0)
// Auto refresh timer
const { pause: pauseAutoRefresh, resume: resumeAutoRefresh } = useIntervalFn(
() => {
if (autoRefreshEnabled.value && opsEnabled.value && !loading.value) {
fetchData()
}
},
autoRefreshIntervalMs,
{ immediate: false }
)
// Countdown timer (updates every second)
// Countdown timer (drives auto refresh; updates every second)
const { pause: pauseCountdown, resume: resumeCountdown } = useIntervalFn(
() => {
if (autoRefreshEnabled.value && autoRefreshCountdown.value > 0) {
autoRefreshCountdown.value--
if (!autoRefreshEnabled.value) return
if (!opsEnabled.value) return
if (loading.value) return
if (autoRefreshCountdown.value <= 0) {
// Fetch immediately when the countdown reaches 0.
// fetchData() will reset the countdown to the full interval.
fetchData()
return
}
autoRefreshCountdown.value -= 1
},
1000,
{ immediate: false }
@@ -477,12 +475,19 @@ function buildApiParams() {
group_id: groupId.value ?? undefined,
mode: queryMode.value
}
if (timeRange.value === 'custom' && customStartTime.value && customEndTime.value) {
params.start_time = customStartTime.value
params.end_time = customEndTime.value
if (timeRange.value === 'custom') {
if (customStartTime.value && customEndTime.value) {
params.start_time = customStartTime.value
params.end_time = customEndTime.value
} else {
// Safety fallback: avoid sending time_range=custom (backend may not support it)
params.time_range = '1h'
}
} else {
params.time_range = timeRange.value
}
return params
}
@@ -679,7 +684,6 @@ onMounted(async () => {
// Start auto refresh if enabled
if (autoRefreshEnabled.value) {
resumeAutoRefresh()
resumeCountdown()
}
})
@@ -697,7 +701,6 @@ async function loadThresholds() {
onUnmounted(() => {
window.removeEventListener('keydown', handleKeydown)
abortDashboardFetch()
pauseAutoRefresh()
pauseCountdown()
})
@@ -705,10 +708,8 @@ onUnmounted(() => {
watch(autoRefreshEnabled, (enabled) => {
if (enabled) {
autoRefreshCountdown.value = Math.floor(autoRefreshIntervalMs.value / 1000)
resumeAutoRefresh()
resumeCountdown()
} else {
pauseAutoRefresh()
pauseCountdown()
autoRefreshCountdown.value = 0
}