moki 3 дней назад
Родитель
Сommit
e0e09a8d82
3 измененных файлов с 29 добавлено и 17 удалено
  1. 6 0
      src/api/work-duration.ts
  2. 4 0
      src/types/index.ts
  3. 19 17
      src/views/Dashboard.vue

+ 6 - 0
src/api/work-duration.ts

@@ -0,0 +1,6 @@
+import http from './index'
+import type {ApiResponse, WorkDuration} from '@/types'
+
+export function getWorkDuration() {
+    return http.get<ApiResponse<WorkDuration>>('/work-duration')
+}

+ 4 - 0
src/types/index.ts

@@ -82,6 +82,10 @@ export interface DeviceStatus {
   ble: { running: boolean; device: string }
 }
 
+export interface WorkDuration {
+  duration_minutes: number
+}
+
 export const STATUS_CONFIG: Record<StatusCode, { color: string; label: string }> = {
     idle: {color: 'green', label: '当前空闲'},
   busy: {color: 'blue', label: '正在工作'},

+ 19 - 17
src/views/Dashboard.vue

@@ -9,6 +9,7 @@ import StatusCard from '@/components/StatusCard.vue'
 import http from '@/api/index'
 import {getHistory} from '@/api/history'
 import {getStatus} from '@/api/status'
+import {getWorkDuration} from '@/api/work-duration'
 
 const {connected, onMessage, onDisconnect} = useSSE('/api/events')
 const {theme} = useTheme()
@@ -96,26 +97,25 @@ const todayStats = computed(() => {
       }))
 })
 
-const activeStatusCodes: StatusCode[] = ['busy', 'reasoning', 'using_tool', 'running', 'pending', 'retry']
+const workDuration = ref('0m')
 
-const workDuration = computed(() => {
-  const items = eventLog.value.slice().reverse()
-  if (items.length === 0) return '0m'
-
-  let totalMs = 0
-  for (let i = 0; i < items.length; i++) {
-    if (activeStatusCodes.includes(items[i].code)) {
-      const end = i + 1 < items.length ? items[i + 1].ts : Date.now()
-      totalMs += end - items[i].ts
+async function fetchWorkDuration() {
+  try {
+    const res = await getWorkDuration()
+    const data = res.data
+    if (data.code === 0 && data.data) {
+      const min = data.data.duration_minutes
+      if (min < 60) {
+        workDuration.value = `${min}m`
+      } else {
+        const hr = Math.floor(min / 60)
+        const m = min % 60
+        workDuration.value = `${hr}h ${m}m`
+      }
     }
+  } catch {
   }
-
-  const totalMin = Math.floor(totalMs / 60000)
-  if (totalMin < 60) return `${totalMin}m`
-  const hr = Math.floor(totalMin / 60)
-  const min = totalMin % 60
-  return `${hr}h ${min}m`
-})
+}
 
 function getOrCreatePieChart(): echarts.ECharts {
   if (!pieChart && pieChartRef.value) {
@@ -238,6 +238,7 @@ const removeListener = onMessage((msg: SseMessage) => {
     ts: Date.now(),
   })
   if (eventLog.value.length > 50) eventLog.value.pop()
+  fetchWorkDuration()
 })
 
 watch(connected, (val) => {
@@ -262,6 +263,7 @@ onMounted(() => {
 
   fetchHistory()
   fetchStatus()
+  fetchWorkDuration()
 
   if (pieChartRef.value) {
     pieChart = echarts.init(pieChartRef.value)