ExecutionView.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. <template>
  2. <Section :title="'Execution Results: ' + title" id = "execution-results-popup">
  3. <template #toolbar>
  4. <router-link v-if="actionId" :to="`/action/${actionId}`" title="View all executions for this action" class="button neutral">
  5. <svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" viewBox="0 0 24 24">
  6. <path fill="currentColor" d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 18c-4.41 0-8-3.59-8-8s3.59-8 8-8 8 3.59 8 8-3.59 8-8 8zm.31-8.86c-1.77-.45-2.34-.94-2.34-1.67 0-.84.79-1.43 2.1-1.43 1.38 0 1.9.66 1.94 1.64h1.71c-.05-1.34-.87-2.57-2.49-2.97V5H10.9v1.69c-1.51.32-2.72 1.3-2.72 2.81 0 1.79 1.49 2.69 3.66 3.21 1.95.46 2.34 1.22 2.34 1.8 0 .53-.39 1.39-2.1 1.39-1.6 0-2.05-.56-2.13-1.45H8.04c.08 1.5 1.18 2.37 2.82 2.69V19h2.34v-1.63c1.65-.35 2.48-1.24 2.48-2.77-.01-1.88-1.51-2.87-3.7-3.23z"/>
  7. </svg>
  8. Action Details
  9. </router-link>
  10. <button @click="toggleSize" title="Toggle dialog size" class = "neutral">
  11. <svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" viewBox="0 0 24 24">
  12. <path fill="currentColor"
  13. d="M3 3h6v2H6.462l4.843 4.843l-1.415 1.414L5 6.367V9H3zm0 18h6v-2H6.376l4.929-4.928l-1.415-1.414L5 17.548V15H3zm12 0h6v-6h-2v2.524l-4.867-4.866l-1.414 1.414L17.647 19H15zm6-18h-6v2h2.562l-4.843 4.843l1.414 1.414L19 6.39V9h2z" />
  14. </svg>
  15. </button>
  16. </template>
  17. <div v-if="logEntry" class = "flex-row">
  18. <dl class = "fg1">
  19. <dt>Duration</dt>
  20. <dd><span v-html="duration"></span></dd>
  21. <dt>Status</dt>
  22. <dd>
  23. <ActionStatusDisplay :log-entry="logEntry" id = "execution-dialog-status" />
  24. </dd>
  25. </dl>
  26. <span class="icon" role="img" v-html="icon" style = "align-self: start"></span>
  27. </div>
  28. <div v-if="notFound" class="error-message padded-content">
  29. <h3>Execution Not Found</h3>
  30. <p>{{ errorMessage }}</p>
  31. <p>The execution with ID <code>{{ executionTrackingId }}</code> could not be found.</p>
  32. <router-link to="/logs">View all logs</router-link> or <router-link to="/">return to home</router-link>.
  33. </div>
  34. <div ref="xtermOutput"></div>
  35. <br />
  36. <div class="flex-row g1 buttons padded-content">
  37. <button @click="goBack" title="Go back">
  38. <HugeiconsIcon :icon="ArrowLeftIcon" />
  39. Back
  40. </button>
  41. <div class = "fg1" />
  42. <button :disabled="!canRerun" @click="rerunAction" title="Rerun">
  43. <HugeiconsIcon :icon="WorkoutRunIcon" />
  44. Rerun
  45. </button>
  46. <button :disabled="!canKill" @click="killAction" title="Kill" id = "execution-dialog-kill-action">
  47. <HugeiconsIcon :icon="Cancel02Icon" />
  48. Kill
  49. </button>
  50. </div>
  51. </Section>
  52. </template>
  53. <script setup>
  54. import { ref, onMounted, onBeforeUnmount, watch } from 'vue'
  55. import ActionStatusDisplay from '../components/ActionStatusDisplay.vue'
  56. import Section from 'picocrank/vue/components/Section.vue'
  57. import { OutputTerminal } from '../../../js/OutputTerminal.js'
  58. import { HugeiconsIcon } from '@hugeicons/vue'
  59. import { WorkoutRunIcon, Cancel02Icon, ArrowLeftIcon } from '@hugeicons/core-free-icons'
  60. import { useRouter } from 'vue-router'
  61. import { buttonResults } from '../stores/buttonResults'
  62. const router = useRouter()
  63. // Refs for DOM elements
  64. const xtermOutput = ref(null)
  65. const props = defineProps({
  66. executionTrackingId: {
  67. type: String,
  68. required: true
  69. }
  70. })
  71. const executionTrackingId = ref(props.executionTrackingId)
  72. const hideBasics = ref(false)
  73. const hideDetails = ref(false)
  74. const hideDetailsOnResult = ref(false)
  75. const executionSeconds = ref(0)
  76. const icon = ref('')
  77. const title = ref('Waiting for result...')
  78. const titleTooltip = ref('')
  79. const duration = ref('')
  80. const logEntry = ref(null)
  81. const canRerun = ref(false)
  82. const canKill = ref(false)
  83. const actionId = ref('')
  84. const notFound = ref(false)
  85. const errorMessage = ref('')
  86. let executionTicker = null
  87. let terminal = null
  88. function initializeTerminal() {
  89. terminal = new OutputTerminal(executionTrackingId.value)
  90. terminal.open(xtermOutput.value)
  91. terminal.resize(80, 40)
  92. window.terminal = terminal
  93. }
  94. function toggleSize() {
  95. if (!xtermOutput.value) {
  96. return
  97. }
  98. if (xtermOutput.value.requestFullscreen) {
  99. xtermOutput.value.requestFullscreen()
  100. } else if (xtermOutput.value.webkitRequestFullscreen) {
  101. xtermOutput.value.webkitRequestFullscreen()
  102. } else if (xtermOutput.value.mozRequestFullScreen) {
  103. xtermOutput.value.mozRequestFullScreen()
  104. } else if (xtermOutput.value.msRequestFullscreen) {
  105. xtermOutput.value.msRequestFullscreen()
  106. }
  107. }
  108. async function reset() {
  109. executionSeconds.value = 0
  110. executionTrackingId.value = 'notset'
  111. hideBasics.value = false
  112. hideDetails.value = false
  113. hideDetailsOnResult.value = false
  114. icon.value = ''
  115. title.value = 'Waiting for result...'
  116. titleTooltip.value = ''
  117. duration.value = ''
  118. canRerun.value = false
  119. canKill.value = false
  120. logEntry.value = null
  121. notFound.value = false
  122. errorMessage.value = ''
  123. if (terminal) {
  124. await terminal.reset()
  125. terminal.fit()
  126. }
  127. }
  128. function show(actionButton) {
  129. if (actionButton) {
  130. icon.value = actionButton.domIcon.innerText
  131. }
  132. canKill.value = true
  133. // Clear existing ticker
  134. if (executionTicker) {
  135. clearInterval(executionTicker)
  136. }
  137. executionSeconds.value = 0
  138. executionTick()
  139. executionTicker = setInterval(() => {
  140. executionTick()
  141. }, 1000)
  142. }
  143. async function rerunAction() {
  144. if (!logEntry.value || !logEntry.value.actionId) {
  145. console.error('Cannot rerun: no action ID available')
  146. return
  147. }
  148. try {
  149. const startActionArgs = {
  150. "bindingId": logEntry.value.actionId,
  151. "arguments": []
  152. }
  153. const res = await window.client.startAction(startActionArgs)
  154. router.push(`/logs/${res.executionTrackingId}`)
  155. } catch (err) {
  156. console.error('Failed to rerun action:', err)
  157. window.showBigError('rerun-action', 'rerunning action', err, false)
  158. }
  159. }
  160. async function killAction() {
  161. if (!executionTrackingId.value || executionTrackingId.value === 'notset') {
  162. return
  163. }
  164. const killActionArgs = {
  165. executionTrackingId: executionTrackingId.value
  166. }
  167. try {
  168. await window.client.killAction(killActionArgs)
  169. } catch (err) {
  170. console.error('Failed to kill action:', err)
  171. }
  172. }
  173. function executionTick() {
  174. executionSeconds.value++
  175. updateDuration(null)
  176. }
  177. function hideEverythingApartFromOutput() {
  178. hideDetailsOnResult.value = true
  179. hideBasics.value = true
  180. hideDetailsOnResult.value = true
  181. hideBasics.value = true
  182. }
  183. async function fetchExecutionResult(executionTrackingIdParam) {
  184. console.log("fetchExecutionResult", executionTrackingIdParam)
  185. executionTrackingId.value = executionTrackingIdParam
  186. notFound.value = false
  187. errorMessage.value = ''
  188. const executionStatusArgs = {
  189. executionTrackingId: executionTrackingId.value
  190. }
  191. try {
  192. const logEntryResult = await window.client.executionStatus(executionStatusArgs)
  193. await renderExecutionResult(logEntryResult)
  194. } catch (err) {
  195. // Check if it's a "not found" error (404 or similar)
  196. if (err.status === 404 || err.code === 'NotFound' || err.message?.includes('not found')) {
  197. notFound.value = true
  198. errorMessage.value = err.message || 'The execution could not be found in the system.'
  199. } else {
  200. renderError(err)
  201. }
  202. throw err
  203. }
  204. }
  205. function updateDuration(logEntryParam) {
  206. logEntry.value = logEntryParam
  207. if (logEntry.value == null) {
  208. duration.value = executionSeconds.value + ' seconds'
  209. duration.value = duration.value
  210. } else if (!logEntry.value.executionStarted) {
  211. duration.value = logEntry.value.datetimeStarted + ' (request time). Not executed.'
  212. } else if (logEntry.value.executionStarted && !logEntry.value.executionFinished) {
  213. duration.value = logEntry.value.datetimeStarted
  214. } else {
  215. let delta = ''
  216. try {
  217. delta = (new Date(logEntry.value.datetimeFinished) - new Date(logEntry.value.datetimeStarted)) / 1000
  218. delta = new Intl.RelativeTimeFormat().format(delta, 'seconds').replace('in ', '').replace('ago', '')
  219. } catch (e) {
  220. console.warn('Failed to calculate delta', e)
  221. }
  222. duration.value = logEntry.value.datetimeStarted + ' &rarr; ' + logEntry.value.datetimeFinished
  223. if (delta !== '') {
  224. duration.value += ' (' + delta + ')'
  225. }
  226. }
  227. }
  228. async function renderExecutionResult(res) {
  229. logEntry.value = res.logEntry
  230. // Clear ticker
  231. if (executionTicker) {
  232. clearInterval(executionTicker)
  233. }
  234. executionTicker = null
  235. if (hideDetailsOnResult.value) {
  236. hideDetails.value = true
  237. }
  238. executionTrackingId.value = res.logEntry.executionTrackingId
  239. canRerun.value = res.logEntry.executionFinished
  240. canKill.value = res.logEntry.canKill
  241. icon.value = res.logEntry.actionIcon
  242. title.value = res.logEntry.actionTitle
  243. titleTooltip.value = 'Action ID: ' + res.logEntry.actionId + '\nExecution ID: ' + res.logEntry.executionTrackingId
  244. actionId.value = res.logEntry.actionId
  245. updateDuration(res.logEntry)
  246. if (terminal) {
  247. await terminal.reset()
  248. await terminal.write(res.logEntry.output, () => {
  249. terminal.fit()
  250. })
  251. }
  252. }
  253. function renderError(err) {
  254. window.showBigError('execution-dlg-err', 'in the execution dialog', 'Failed to fetch execution result. ' + err, false)
  255. }
  256. function handleClose() {
  257. if (executionTicker) {
  258. clearInterval(executionTicker)
  259. }
  260. executionTicker = null
  261. }
  262. function cleanup() {
  263. if (executionTicker) {
  264. clearInterval(executionTicker)
  265. }
  266. executionTicker = null
  267. if (terminal != null) {
  268. terminal.close()
  269. }
  270. terminal = null
  271. }
  272. function goBack() {
  273. router.back()
  274. }
  275. onMounted(() => {
  276. document.addEventListener('fullscreenchange', (e) => {
  277. setTimeout(() => { // Wait for the DOM to settle
  278. if (document.fullscreenElement) {
  279. terminal.fit()
  280. } else {
  281. terminal.resize(80, 40)
  282. terminal.fit()
  283. }
  284. }, 100)
  285. })
  286. initializeTerminal()
  287. fetchExecutionResult(props.executionTrackingId)
  288. watch(
  289. () => buttonResults[props.executionTrackingId],
  290. (newResult, oldResult) => {
  291. if (newResult) {
  292. renderExecutionResult({
  293. logEntry: newResult
  294. })
  295. }
  296. }
  297. )
  298. })
  299. onBeforeUnmount(() => {
  300. cleanup()
  301. })
  302. // Expose methods for parent/imperative use
  303. defineExpose({
  304. reset,
  305. show,
  306. rerunAction,
  307. killAction,
  308. fetchExecutionResult,
  309. renderExecutionResult,
  310. hideEverythingApartFromOutput,
  311. handleClose
  312. })
  313. </script>
  314. <style scoped>
  315. .action-history-link {
  316. color: var(--link-color, #007bff);
  317. text-decoration: none;
  318. display: inline-block;
  319. font-size: 0.9rem;
  320. }
  321. .error-message {
  322. background-color: #f8d7da;
  323. border: 1px solid #f5c2c7;
  324. border-radius: 0.25rem;
  325. padding: 1.5rem;
  326. margin: 1rem 0;
  327. }
  328. .error-message h3 {
  329. margin: 0 0 0.5rem 0;
  330. color: #721c24;
  331. }
  332. .error-message p {
  333. margin: 0.5rem 0;
  334. color: #721c24;
  335. }
  336. .error-message code {
  337. background-color: #f8d7da;
  338. padding: 0.125rem 0.25rem;
  339. border-radius: 0.125rem;
  340. font-family: monospace;
  341. }
  342. .error-message a {
  343. color: #721c24;
  344. text-decoration: underline;
  345. font-weight: 500;
  346. }
  347. </style>