Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 | import { ref, onUnmounted, computed, watch, type Ref } from 'vue' import { ElMessage } from 'element-plus' import * as workflowsApi from '../../api/workflows' import * as tasksApi from '../../api/tasks' import { useExecutionStore } from '../../stores/executionStore' import type { ExecutionSummary } from '@/types/generated/ExecutionSummary' import type { ExecutionHistoryPage } from '@/types/generated/ExecutionHistoryPage' import type { ExecutionStatus } from '@/types/generated/ExecutionStatus' import { POLLING_TIMING, ERROR_MESSAGES } from '@/constants' export function useExecutionHistory(workflowId: Readonly<Ref<string | null | undefined>>) { const executionStore = useExecutionStore() const executions = ref<ExecutionSummary[]>([]) const isLoading = ref(false) const pollingInterval = ref<number | null>(null) const selectedExecutionId = ref<string | null>(null) const isFirstPoll = ref(true) const page = ref(1) const pageSize = ref(20) const totalExecutions = ref(0) const totalPages = ref(0) const hasPrevPage = computed(() => page.value > 1) const hasNextPage = computed(() => { if (totalPages.value === 0) return false return page.value < totalPages.value }) const loadHistory = async (targetPage?: number) => { const currentWorkflowId = workflowId.value if (!currentWorkflowId) return null isLoading.value = true const requestedPage = targetPage ?? page.value const previousFirstId = executions.value[0]?.execution_id ?? null try { const history: ExecutionHistoryPage = await workflowsApi.listWorkflowExecutions( currentWorkflowId, requestedPage, pageSize.value ) const newItems = history.items const newFirstId = newItems[0]?.execution_id ?? null executions.value = newItems totalExecutions.value = history.total page.value = history.page pageSize.value = history.page_size totalPages.value = history.total_pages return { previousFirstId, newFirstId } } catch (error) { console.error('Failed to load execution history:', error) ElMessage.error(ERROR_MESSAGES.FAILED_TO_LOAD('execution history')) return null } finally { isLoading.value = false } } const startPolling = () => { const currentWorkflowId = workflowId.value if (!currentWorkflowId) return stopPolling() isFirstPoll.value = true loadHistory(page.value) pollingInterval.value = window.setInterval(async () => { await loadHistory(page.value) isFirstPoll.value = false }, POLLING_TIMING.EXECUTION_HISTORY || 5000) } const stopPolling = () => { if (pollingInterval.value) { clearInterval(pollingInterval.value) pollingInterval.value = null } } const switchToExecution = async (executionId: string) => { selectedExecutionId.value = executionId executionStore.startExecution(executionId) try { const tasks = await tasksApi.getExecutionStatus(executionId) executionStore.updateFromTasks(tasks) const allCompleted = tasks.every(t => t.status === 'Completed' || t.status === 'Failed') if (allCompleted) { executionStore.endExecution() } } catch (error) { console.error('Failed to load execution details:', error) ElMessage.error(ERROR_MESSAGES.FAILED_TO_LOAD('execution details')) // Rollback execution state on error executionStore.clearExecution() selectedExecutionId.value = null } } const getStatusText = (status: ExecutionStatus): string => { const statusMap: Record<ExecutionStatus, string> = { Running: 'Running', Completed: 'Completed', Failed: 'Failed' } return statusMap[status] || status } const getStatusIcon = (status: ExecutionStatus): string => { const iconMap: Record<ExecutionStatus, string> = { Running: '⏳', Completed: '✓', Failed: '✗' } return iconMap[status] || '?' } const formatRelativeTime = (timestamp: number): string => { const now = Date.now() const diff = now - timestamp const seconds = Math.floor(diff / 1000) const minutes = Math.floor(seconds / 60) const hours = Math.floor(minutes / 60) const days = Math.floor(hours / 24) if (seconds < 60) return 'just now' if (minutes < 60) return `${minutes} min ago` if (hours < 24) return `${hours} hr ago` if (days < 7) return `${days} days ago` const date = new Date(timestamp) return date.toLocaleDateString('en-US', { month: 'short', day: 'numeric', year: 'numeric' }) } const formatFullDateTime = (timestamp: number): string => { const date = new Date(timestamp) return date.toLocaleString('en-US', { year: 'numeric', month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: true }) } const hasRunningExecution = computed(() => { return executions.value.some(e => e.status === 'Running') }) const latestExecution = computed(() => { return executions.value[0] || null }) const goToPage = async (targetPage: number) => { if (targetPage < 1) return if (totalPages.value > 0 && targetPage > totalPages.value) return await loadHistory(targetPage) } const goToNextPage = async () => { if (hasNextPage.value) { await goToPage(page.value + 1) } } const goToPrevPage = async () => { if (hasPrevPage.value) { await goToPage(page.value - 1) } } onUnmounted(() => { stopPolling() }) watch( workflowId, newId => { if (!newId) { stopPolling() executions.value = [] selectedExecutionId.value = null return } executions.value = [] selectedExecutionId.value = null page.value = 1 totalExecutions.value = 0 totalPages.value = 0 startPolling() } ) return { // State executions, isLoading, selectedExecutionId, hasRunningExecution, latestExecution, totalExecutions, page, pageSize, totalPages, hasNextPage, hasPrevPage, // Methods loadHistory, startPolling, stopPolling, switchToExecution, getStatusText, getStatusIcon, formatRelativeTime, formatFullDateTime, goToPage, goToNextPage, goToPrevPage, } } |