useDashboardPolling.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { ref } from 'vue'
  2. import * as statsApi from '@/api/stats'
  3. import type {
  4. StatsHomeScreenQueryData,
  5. StatsHomeScreenFallHistory,
  6. StatsHomeScreenAlarmHistory,
  7. } from '@/api/stats/types'
  8. export function useDashboardPolling(options: { tenantId: string }) {
  9. const todayScreenData = ref<StatsHomeScreenQueryData | null>(null)
  10. const fallHistoryData = ref<StatsHomeScreenFallHistory | null>(null)
  11. const alarmHistoryData = ref<StatsHomeScreenAlarmHistory | null>(null)
  12. const fallQueryType = ref<'day' | 'month'>('day')
  13. const alarmQueryType = ref<'day' | 'month'>('day')
  14. let realtimeTimer: ReturnType<typeof setInterval> | null = null
  15. let businessTimer: ReturnType<typeof setInterval> | null = null
  16. let isVisible = true
  17. const MAX_RETRY = 3
  18. let retryCountRealtime = 0
  19. let retryCountBusiness = 0
  20. const getTodayData = async () => {
  21. if (!isVisible) return
  22. try {
  23. const res = await statsApi.statsHomeScreenQuery({
  24. tenantId: options.tenantId,
  25. })
  26. todayScreenData.value = res.data
  27. retryCountRealtime = 0
  28. } catch {
  29. retryCountRealtime++
  30. if (retryCountRealtime < MAX_RETRY) {
  31. setTimeout(getTodayData, 1000)
  32. }
  33. }
  34. }
  35. const getHistoryData = async () => {
  36. if (!isVisible) return
  37. try {
  38. const [fallRes, alarmRes] = await Promise.all([
  39. statsApi.statsHomeScreenFallHistory({
  40. tenantId: options.tenantId,
  41. queryType: fallQueryType.value,
  42. }),
  43. statsApi.statsHomeScreenAlarmHistory({
  44. tenantId: options.tenantId,
  45. queryType: alarmQueryType.value,
  46. }),
  47. ])
  48. fallHistoryData.value = fallRes.data
  49. alarmHistoryData.value = alarmRes.data
  50. retryCountBusiness = 0
  51. } catch {
  52. retryCountBusiness++
  53. if (retryCountBusiness < MAX_RETRY) {
  54. setTimeout(getHistoryData, 2000)
  55. }
  56. }
  57. }
  58. const start = () => {
  59. getTodayData()
  60. getHistoryData()
  61. realtimeTimer = setInterval(getTodayData, 5000)
  62. businessTimer = setInterval(getHistoryData, 30000)
  63. document.addEventListener('visibilitychange', handleVisibilityChange)
  64. }
  65. const stop = () => {
  66. if (realtimeTimer) clearInterval(realtimeTimer)
  67. if (businessTimer) clearInterval(businessTimer)
  68. realtimeTimer = null
  69. businessTimer = null
  70. document.removeEventListener('visibilitychange', handleVisibilityChange)
  71. }
  72. const handleVisibilityChange = () => {
  73. isVisible = document.visibilityState === 'visible'
  74. if (isVisible) {
  75. start()
  76. } else {
  77. stop()
  78. }
  79. }
  80. const updateFallQueryType = async (type: 'day' | 'month') => {
  81. fallQueryType.value = type
  82. const res = await statsApi.statsHomeScreenFallHistory({
  83. tenantId: options.tenantId,
  84. queryType: fallQueryType.value,
  85. })
  86. fallHistoryData.value = res.data
  87. }
  88. const updateAlarmQueryType = async (type: 'day' | 'month') => {
  89. alarmQueryType.value = type
  90. const res = await statsApi.statsHomeScreenAlarmHistory({
  91. tenantId: options.tenantId,
  92. queryType: alarmQueryType.value,
  93. })
  94. alarmHistoryData.value = res.data
  95. }
  96. return {
  97. todayScreenData,
  98. fallHistoryData,
  99. alarmHistoryData,
  100. updateFallQueryType,
  101. updateAlarmQueryType,
  102. start,
  103. stop,
  104. }
  105. }