ExecutionView.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. <ActionIconGlyph class="icon" role="img" :glyph="icon" style="align-self: start" />
  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 ActionIconGlyph from '../components/ActionIconGlyph.vue'
  56. import ActionStatusDisplay from '../components/ActionStatusDisplay.vue'
  57. import Section from 'picocrank/vue/components/Section.vue'
  58. import { OutputTerminal } from '../../../js/OutputTerminal.js'
  59. import { HugeiconsIcon } from '@hugeicons/vue'
  60. import { WorkoutRunIcon, Cancel02Icon, ArrowLeftIcon } from '@hugeicons/core-free-icons'
  61. import { useRouter } from 'vue-router'
  62. import { buttonResults } from '../stores/buttonResults'
  63. const router = useRouter()
  64. // Refs for DOM elements
  65. const xtermOutput = ref(null)
  66. const props = defineProps({
  67. executionTrackingId: {
  68. type: String,
  69. required: true
  70. }
  71. })
  72. const executionTrackingId = ref(props.executionTrackingId)
  73. const hideBasics = ref(false)
  74. const hideDetails = ref(false)
  75. const hideDetailsOnResult = ref(false)
  76. const executionSeconds = ref(0)
  77. const icon = ref('')
  78. const title = ref('Waiting for result...')
  79. const titleTooltip = ref('')
  80. const duration = ref('')
  81. const logEntry = ref(null)
  82. const canRerun = ref(false)
  83. const canKill = ref(false)
  84. const actionId = ref('')
  85. const notFound = ref(false)
  86. const errorMessage = ref('')
  87. let executionTicker = null
  88. let terminal = null
  89. function initializeTerminal() {
  90. terminal = new OutputTerminal(executionTrackingId.value)
  91. terminal.open(xtermOutput.value)
  92. terminal.resize(80, 40)
  93. window.terminal = terminal
  94. }
  95. function toggleSize() {
  96. if (!xtermOutput.value) {
  97. return
  98. }
  99. if (xtermOutput.value.requestFullscreen) {
  100. xtermOutput.value.requestFullscreen()
  101. } else if (xtermOutput.value.webkitRequestFullscreen) {
  102. xtermOutput.value.webkitRequestFullscreen()
  103. } else if (xtermOutput.value.mozRequestFullScreen) {
  104. xtermOutput.value.mozRequestFullScreen()
  105. } else if (xtermOutput.value.msRequestFullscreen) {
  106. xtermOutput.value.msRequestFullscreen()
  107. }
  108. }
  109. async function reset() {
  110. executionSeconds.value = 0
  111. executionTrackingId.value = 'notset'
  112. hideBasics.value = false
  113. hideDetails.value = false
  114. hideDetailsOnResult.value = false
  115. icon.value = ''
  116. title.value = 'Waiting for result...'
  117. titleTooltip.value = ''
  118. duration.value = ''
  119. canRerun.value = false
  120. canKill.value = false
  121. logEntry.value = null
  122. notFound.value = false
  123. errorMessage.value = ''
  124. if (terminal) {
  125. await terminal.reset()
  126. terminal.fit()
  127. }
  128. }
  129. function show(actionButton) {
  130. if (actionButton) {
  131. icon.value = actionButton.glyph ?? ''
  132. }
  133. canKill.value = true
  134. // Clear existing ticker
  135. if (executionTicker) {
  136. clearInterval(executionTicker)
  137. }
  138. executionSeconds.value = 0
  139. executionTick()
  140. executionTicker = setInterval(() => {
  141. executionTick()
  142. }, 1000)
  143. }
  144. async function rerunAction() {
  145. const bindingId = logEntry.value?.bindingId
  146. if (!logEntry.value || !bindingId) {
  147. console.error('Cannot rerun: no action ID available')
  148. return
  149. }
  150. try {
  151. const startActionArgs = {
  152. "bindingId": bindingId,
  153. "arguments": []
  154. }
  155. const res = await window.client.startAction(startActionArgs)
  156. router.push(`/logs/${res.executionTrackingId}`)
  157. } catch (err) {
  158. console.error('Failed to rerun action:', err)
  159. window.showBigError('rerun-action', 'rerunning action', err, false)
  160. }
  161. }
  162. async function killAction() {
  163. if (!executionTrackingId.value || executionTrackingId.value === 'notset') {
  164. return
  165. }
  166. const killActionArgs = {
  167. executionTrackingId: executionTrackingId.value
  168. }
  169. try {
  170. await window.client.killAction(killActionArgs)
  171. } catch (err) {
  172. console.error('Failed to kill action:', err)
  173. }
  174. }
  175. function executionTick() {
  176. executionSeconds.value++
  177. updateDuration(null)
  178. }
  179. function hideEverythingApartFromOutput() {
  180. hideDetailsOnResult.value = true
  181. hideBasics.value = true
  182. hideDetailsOnResult.value = true
  183. hideBasics.value = true
  184. }
  185. async function fetchExecutionResult(executionTrackingIdParam) {
  186. console.log("fetchExecutionResult", executionTrackingIdParam)
  187. executionTrackingId.value = executionTrackingIdParam
  188. notFound.value = false
  189. errorMessage.value = ''
  190. const executionStatusArgs = {
  191. executionTrackingId: executionTrackingId.value
  192. }
  193. try {
  194. const logEntryResult = await window.client.executionStatus(executionStatusArgs)
  195. await renderExecutionResult(logEntryResult)
  196. } catch (err) {
  197. // Check if it's a "not found" error (404 or similar)
  198. if (err.status === 404 || err.code === 'NotFound' || err.message?.includes('not found')) {
  199. notFound.value = true
  200. errorMessage.value = err.message || 'The execution could not be found in the system.'
  201. } else {
  202. renderError(err)
  203. }
  204. throw err
  205. }
  206. }
  207. function updateDuration(logEntryParam) {
  208. logEntry.value = logEntryParam
  209. if (logEntry.value == null) {
  210. duration.value = executionSeconds.value + ' seconds'
  211. duration.value = duration.value
  212. } else if (!logEntry.value.executionStarted) {
  213. duration.value = logEntry.value.datetimeStarted + ' (request time). Not executed.'
  214. } else if (logEntry.value.executionStarted && !logEntry.value.executionFinished) {
  215. duration.value = logEntry.value.datetimeStarted
  216. } else {
  217. let delta = ''
  218. try {
  219. delta = (new Date(logEntry.value.datetimeFinished) - new Date(logEntry.value.datetimeStarted)) / 1000
  220. delta = new Intl.RelativeTimeFormat().format(delta, 'seconds').replace('in ', '').replace('ago', '')
  221. } catch (e) {
  222. console.warn('Failed to calculate delta', e)
  223. }
  224. duration.value = logEntry.value.datetimeStarted + ' &rarr; ' + logEntry.value.datetimeFinished
  225. if (delta !== '') {
  226. duration.value += ' (' + delta + ')'
  227. }
  228. }
  229. }
  230. async function renderExecutionResult(res) {
  231. logEntry.value = res.logEntry
  232. // Clear ticker
  233. if (executionTicker) {
  234. clearInterval(executionTicker)
  235. }
  236. executionTicker = null
  237. if (hideDetailsOnResult.value) {
  238. hideDetails.value = true
  239. }
  240. executionTrackingId.value = res.logEntry.executionTrackingId
  241. canRerun.value = res.logEntry.executionFinished && !!res.logEntry.bindingId
  242. canKill.value = res.logEntry.canKill
  243. icon.value = res.logEntry.actionIcon
  244. title.value = res.logEntry.actionTitle
  245. titleTooltip.value = 'Action ID: ' + res.logEntry.bindingId + '\nExecution ID: ' + res.logEntry.executionTrackingId
  246. actionId.value = res.logEntry.bindingId
  247. updateDuration(res.logEntry)
  248. if (terminal) {
  249. await terminal.reset()
  250. await terminal.write(res.logEntry.output, () => {
  251. terminal.fit()
  252. })
  253. }
  254. }
  255. function renderError(err) {
  256. window.showBigError('execution-dlg-err', 'in the execution dialog', 'Failed to fetch execution result. ' + err, false)
  257. }
  258. function handleClose() {
  259. if (executionTicker) {
  260. clearInterval(executionTicker)
  261. }
  262. executionTicker = null
  263. }
  264. function cleanup() {
  265. if (executionTicker) {
  266. clearInterval(executionTicker)
  267. }
  268. executionTicker = null
  269. if (terminal != null) {
  270. terminal.close()
  271. }
  272. terminal = null
  273. }
  274. function goBack() {
  275. router.back()
  276. }
  277. onMounted(() => {
  278. document.addEventListener('fullscreenchange', (e) => {
  279. setTimeout(() => { // Wait for the DOM to settle
  280. if (document.fullscreenElement) {
  281. terminal.fit()
  282. } else {
  283. terminal.resize(80, 40)
  284. terminal.fit()
  285. }
  286. }, 100)
  287. })
  288. initializeTerminal()
  289. fetchExecutionResult(props.executionTrackingId)
  290. watch(
  291. () => buttonResults[props.executionTrackingId],
  292. (newResult, oldResult) => {
  293. if (newResult) {
  294. renderExecutionResult({
  295. logEntry: newResult
  296. })
  297. }
  298. }
  299. )
  300. })
  301. onBeforeUnmount(() => {
  302. cleanup()
  303. })
  304. // Expose methods for parent/imperative use
  305. defineExpose({
  306. reset,
  307. show,
  308. rerunAction,
  309. killAction,
  310. fetchExecutionResult,
  311. renderExecutionResult,
  312. hideEverythingApartFromOutput,
  313. handleClose
  314. })
  315. </script>
  316. <style scoped>
  317. .action-history-link {
  318. color: var(--link-color, #007bff);
  319. text-decoration: none;
  320. display: inline-block;
  321. font-size: 0.9rem;
  322. }
  323. .error-message {
  324. background-color: #f8d7da;
  325. border: 1px solid #f5c2c7;
  326. border-radius: 0.25rem;
  327. padding: 1.5rem;
  328. margin: 1rem 0;
  329. }
  330. .error-message h3 {
  331. margin: 0 0 0.5rem 0;
  332. color: #721c24;
  333. }
  334. .error-message p {
  335. margin: 0.5rem 0;
  336. color: #721c24;
  337. }
  338. .error-message code {
  339. background-color: #f8d7da;
  340. padding: 0.125rem 0.25rem;
  341. border-radius: 0.125rem;
  342. font-family: monospace;
  343. }
  344. .error-message a {
  345. color: #721c24;
  346. text-decoration: underline;
  347. font-weight: 500;
  348. }
  349. </style>