perf(chat): 使用预聚合快照优化客服会话统计

This commit is contained in:
yml
2026-11-20 19:46:09 +08:00
parent ec6f79f196
commit e01a01c1aa
8 changed files with 282 additions and 24 deletions
@@ -76,6 +76,9 @@ const remarkEditing = ref(false)
const remarkValue = ref('')
let searchTimer: ReturnType<typeof setTimeout> | null = null
let countsTimer: ReturnType<typeof setTimeout> | null = null
let countsRecheckTimer: ReturnType<typeof setTimeout> | null = null
let countsPollTimer: ReturnType<typeof setInterval> | null = null
let countsRecheckAttempts = 0
let countsCacheKey = ''
let countsLoadedAt = 0
@@ -203,11 +206,16 @@ onMounted(async () => {
}
keyword.value = firstQueryValue(route.query.chat_keyword)
await Promise.all([loadConversations(), loadCounts(true), loadQuickReplies()])
countsPollTimer = setInterval(() => {
if (!document.hidden) void loadCounts(true)
}, 30_000)
})
onBeforeUnmount(() => {
if (searchTimer) clearTimeout(searchTimer)
if (countsTimer) clearTimeout(countsTimer)
if (countsRecheckTimer) clearTimeout(countsRecheckTimer)
if (countsPollTimer) clearInterval(countsPollTimer)
})
async function loadConversations(showLoading = true, resetPage = false) {
@@ -244,22 +252,37 @@ async function loadCounts(force = false) {
keyword: keyword.value.trim(),
}
const cacheKey = JSON.stringify(query)
if (!force && cacheKey === countsCacheKey && Date.now() - countsLoadedAt < 5000) return
if (!force && cacheKey === countsCacheKey && Date.now() - countsLoadedAt < 10_000) return
try {
chatCounts.value = await fetchAdminChatCounts(query)
countsCacheKey = cacheKey
countsLoadedAt = Date.now()
if (chatCounts.value.refreshing && countsRecheckAttempts < 2) {
countsRecheckAttempts++
if (countsRecheckTimer) clearTimeout(countsRecheckTimer)
countsRecheckTimer = setTimeout(() => {
countsRecheckTimer = null
void loadCounts(true)
}, 1500)
} else if (!chatCounts.value.refreshing) {
countsRecheckAttempts = 0
}
} catch {
/* keep the last successful counts visible */
}
}
function scheduleCountsRefresh() {
countsRecheckAttempts = 0
if (countsRecheckTimer) {
clearTimeout(countsRecheckTimer)
countsRecheckTimer = null
}
if (countsTimer) clearTimeout(countsTimer)
countsTimer = setTimeout(() => {
countsTimer = null
void loadCounts(true)
}, 1000)
}, 2000)
}
function handlePageChange(page: number) {