ExecutionView.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. <template>
  2. <Section
  3. id="execution-results-popup"
  4. :icon="ComputerTerminal01Icon"
  5. >
  6. <template #title>
  7. <span class="section-title-with-icon">
  8. Execution Results:
  9. <router-link
  10. v-if="actionId"
  11. :to="`/action/${actionId}`"
  12. class="action-details-title-link"
  13. :title="titleTooltip"
  14. >
  15. <ActionIconGlyph
  16. class="action-title-icon"
  17. :glyph="icon"
  18. />
  19. <LogActionTitle
  20. v-if="logEntry"
  21. :action-title="title"
  22. :justification="logEntry.justification"
  23. />
  24. <span v-else>{{ title }}</span>
  25. </router-link>
  26. <template v-else>
  27. <LogActionTitle
  28. v-if="logEntry"
  29. :action-title="title"
  30. :justification="logEntry.justification"
  31. />
  32. <span v-else>{{ title }}</span>
  33. </template>
  34. </span>
  35. </template>
  36. <template #toolbar>
  37. <button
  38. v-for="dashboard in backToDashboards"
  39. :key="dashboard.path"
  40. :title="'Back to ' + dashboard.title"
  41. class="button neutral inline-icon"
  42. @click="goToDashboard(dashboard.path)"
  43. >
  44. <HugeiconsIcon :icon="DashboardSquare01Icon" />
  45. {{ dashboard.title }}
  46. </button>
  47. <button
  48. v-if="backToDashboards.length === 0"
  49. title="Go back"
  50. class="button neutral inline-icon"
  51. @click="goBack"
  52. >
  53. <HugeiconsIcon :icon="ArrowLeftIcon" />
  54. Back
  55. </button>
  56. </template>
  57. <div
  58. v-if="logEntry"
  59. class="flex-row"
  60. >
  61. <dl class="fg1">
  62. <dt>Duration</dt>
  63. <dd>{{ duration }}</dd>
  64. <dt>Status</dt>
  65. <dd class="execution-dialog-status">
  66. <ActionStatusDisplay
  67. :log-entry="logEntry"
  68. :link-queued-status="true"
  69. />
  70. </dd>
  71. </dl>
  72. </div>
  73. <div
  74. v-if="notFound"
  75. class="error-message padded-content"
  76. >
  77. <h3>Execution Not Found</h3>
  78. <p>{{ errorMessage }}</p>
  79. <p>The execution with ID <code>{{ executionTrackingId }}</code> could not be found.</p>
  80. <router-link to="/logs">
  81. View all logs
  82. </router-link> or <router-link to="/">
  83. return to home
  84. </router-link>.
  85. </div>
  86. <div class="xterm-output-container">
  87. <div class="xterm-overlay-toolbar">
  88. <button
  89. type="button"
  90. class="xterm-overlay-button"
  91. title="Copy to clipboard"
  92. @click="copyOutput"
  93. >
  94. <HugeiconsIcon :icon="Copy01Icon" />
  95. </button>
  96. <button
  97. type="button"
  98. class="xterm-overlay-button"
  99. title="Toggle fullscreen"
  100. @click="toggleSize"
  101. >
  102. <svg
  103. xmlns="http://www.w3.org/2000/svg"
  104. width="1em"
  105. height="1em"
  106. viewBox="0 0 24 24"
  107. >
  108. <path
  109. fill="currentColor"
  110. 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"
  111. />
  112. </svg>
  113. </button>
  114. </div>
  115. <div ref="xtermOutput" />
  116. </div>
  117. <br>
  118. <div class="flex-row g1 buttons padded-content">
  119. <div class="fg1" />
  120. <button
  121. type="button"
  122. class="inline-icon"
  123. :disabled="!canRerun"
  124. title="Rerun"
  125. @click="rerunAction"
  126. >
  127. <HugeiconsIcon :icon="WorkoutRunIcon" />
  128. Rerun
  129. </button>
  130. <button
  131. id="execution-dialog-kill-action"
  132. type="button"
  133. class="inline-icon"
  134. :disabled="!canKill"
  135. title="Kill"
  136. @click="killAction"
  137. >
  138. <HugeiconsIcon :icon="Cancel02Icon" />
  139. Kill
  140. </button>
  141. </div>
  142. </Section>
  143. </template>
  144. <script setup>
  145. import { ref, onMounted, onBeforeUnmount, watch } from 'vue'
  146. import ActionIconGlyph from '../components/ActionIconGlyph.vue'
  147. import ActionStatusDisplay from '../components/ActionStatusDisplay.vue'
  148. import LogActionTitle from '../components/LogActionTitle.vue'
  149. import Section from 'picocrank/vue/components/Section.vue'
  150. import { OutputTerminal } from '../../../js/OutputTerminal.js'
  151. import { HugeiconsIcon } from '@hugeicons/vue'
  152. import { WorkoutRunIcon, Cancel02Icon, ArrowLeftIcon, DashboardSquare01Icon, Copy01Icon, ComputerTerminal01Icon } from '@hugeicons/core-free-icons'
  153. import { useRouter } from 'vue-router'
  154. import { buttonResults } from '../stores/buttonResults'
  155. import { requestReconnectNow } from '../../../js/websocket.js'
  156. import {
  157. buildRerunPrefilledArguments,
  158. buildRerunStartActionArgs,
  159. rerunNeedsArgumentForm
  160. } from '../utils/rerunArguments.js'
  161. const router = useRouter()
  162. // Refs for DOM elements
  163. const xtermOutput = ref(null)
  164. const props = defineProps({
  165. executionTrackingId: {
  166. type: String,
  167. required: true
  168. }
  169. })
  170. const executionTrackingId = ref(props.executionTrackingId)
  171. const hideBasics = ref(false)
  172. const hideDetails = ref(false)
  173. const hideDetailsOnResult = ref(false)
  174. const executionSeconds = ref(0)
  175. const icon = ref('')
  176. const title = ref('Waiting for result...')
  177. const titleTooltip = ref('')
  178. const duration = ref('')
  179. const logEntry = ref(null)
  180. const canRerun = ref(false)
  181. const canKill = ref(false)
  182. const actionId = ref('')
  183. const backToDashboards = ref([])
  184. const notFound = ref(false)
  185. const errorMessage = ref('')
  186. let executionTicker = null
  187. let terminal = null
  188. function initializeTerminal () {
  189. terminal = new OutputTerminal(executionTrackingId.value)
  190. terminal.open(xtermOutput.value)
  191. terminal.resize(80, 40)
  192. window.terminal = terminal
  193. }
  194. function toggleSize () {
  195. if (!xtermOutput.value) {
  196. return
  197. }
  198. if (xtermOutput.value.requestFullscreen) {
  199. xtermOutput.value.requestFullscreen()
  200. } else if (xtermOutput.value.webkitRequestFullscreen) {
  201. xtermOutput.value.webkitRequestFullscreen()
  202. } else if (xtermOutput.value.mozRequestFullScreen) {
  203. xtermOutput.value.mozRequestFullScreen()
  204. } else if (xtermOutput.value.msRequestFullscreen) {
  205. xtermOutput.value.msRequestFullscreen()
  206. }
  207. }
  208. async function copyOutput () {
  209. const text = terminal?.getBufferAsString?.() || logEntry.value?.output || ''
  210. if (!text) {
  211. return
  212. }
  213. try {
  214. await navigator.clipboard.writeText(text)
  215. } catch (err) {
  216. console.error('Failed to copy execution output:', err)
  217. }
  218. }
  219. async function reset () {
  220. executionSeconds.value = 0
  221. executionTrackingId.value = 'notset'
  222. hideBasics.value = false
  223. hideDetails.value = false
  224. hideDetailsOnResult.value = false
  225. icon.value = ''
  226. title.value = 'Waiting for result...'
  227. titleTooltip.value = ''
  228. duration.value = ''
  229. canRerun.value = false
  230. canKill.value = false
  231. logEntry.value = null
  232. backToDashboards.value = []
  233. notFound.value = false
  234. errorMessage.value = ''
  235. if (terminal) {
  236. await terminal.reset()
  237. terminal.fit()
  238. }
  239. }
  240. function show (actionButton) {
  241. if (actionButton) {
  242. icon.value = actionButton.glyph ?? ''
  243. }
  244. canKill.value = true
  245. // Clear existing ticker
  246. if (executionTicker) {
  247. clearInterval(executionTicker)
  248. }
  249. executionSeconds.value = 0
  250. executionTick()
  251. executionTicker = setInterval(() => {
  252. executionTick()
  253. }, 1000)
  254. }
  255. async function rerunAction () {
  256. const bindingId = logEntry.value?.bindingId
  257. if (!logEntry.value || !bindingId) {
  258. console.error('Cannot rerun: no action ID available')
  259. return
  260. }
  261. try {
  262. const binding = await window.client.getActionBinding({ bindingId })
  263. if (rerunNeedsArgumentForm(binding.action, logEntry.value)) {
  264. router.push({
  265. path: `/actionBinding/${bindingId}/argumentForm`,
  266. state: { prefilledArguments: buildRerunPrefilledArguments(logEntry.value) }
  267. })
  268. return
  269. }
  270. requestReconnectNow()
  271. const startActionArgs = buildRerunStartActionArgs(bindingId, logEntry.value)
  272. const res = await window.client.startAction(startActionArgs)
  273. router.push(`/logs/${res.executionTrackingId}`)
  274. } catch (err) {
  275. console.error('Failed to rerun action:', err)
  276. window.showBigError('rerun-action', 'rerunning action', err, false)
  277. }
  278. }
  279. async function killAction () {
  280. if (!executionTrackingId.value || executionTrackingId.value === 'notset') {
  281. return
  282. }
  283. const killActionArgs = {
  284. executionTrackingId: executionTrackingId.value
  285. }
  286. try {
  287. await window.client.killAction(killActionArgs)
  288. } catch (err) {
  289. console.error('Failed to kill action:', err)
  290. }
  291. }
  292. function executionTick () {
  293. executionSeconds.value++
  294. updateDuration(null)
  295. }
  296. function hideEverythingApartFromOutput () {
  297. hideDetailsOnResult.value = true
  298. hideBasics.value = true
  299. hideDetailsOnResult.value = true
  300. hideBasics.value = true
  301. }
  302. async function fetchExecutionResult (executionTrackingIdParam) {
  303. console.log('fetchExecutionResult', executionTrackingIdParam)
  304. executionTrackingId.value = executionTrackingIdParam
  305. notFound.value = false
  306. errorMessage.value = ''
  307. backToDashboards.value = []
  308. const executionStatusArgs = {
  309. executionTrackingId: executionTrackingId.value
  310. }
  311. try {
  312. const executionStatusResult = await window.client.executionStatus(executionStatusArgs)
  313. await renderExecutionResult(executionStatusResult)
  314. } catch (err) {
  315. // Check if it's a "not found" error (404 or similar)
  316. if (err.status === 404 || err.code === 'NotFound' || err.message?.includes('not found')) {
  317. notFound.value = true
  318. errorMessage.value = err.message || 'The execution could not be found in the system.'
  319. } else {
  320. renderError(err)
  321. }
  322. throw err
  323. }
  324. }
  325. function formatDurationRange (datetimeStarted, datetimeFinished) {
  326. const startDay = datetimeStarted?.slice(0, 10)
  327. const finishDay = datetimeFinished?.slice(0, 10)
  328. const finishTime = datetimeFinished?.includes(' ')
  329. ? datetimeFinished.slice(datetimeFinished.indexOf(' ') + 1)
  330. : datetimeFinished
  331. if (startDay && finishDay && startDay === finishDay) {
  332. return `${datetimeStarted} → ${finishTime}`
  333. }
  334. return `${datetimeStarted} → ${datetimeFinished}`
  335. }
  336. function updateDuration (logEntryParam) {
  337. logEntry.value = logEntryParam
  338. if (logEntry.value == null) {
  339. duration.value = executionSeconds.value + ' seconds'
  340. } else if (!logEntry.value.executionStarted) {
  341. duration.value = logEntry.value.datetimeStarted + ' (request time). Not executed.'
  342. } else if (logEntry.value.executionStarted && !logEntry.value.executionFinished) {
  343. duration.value = logEntry.value.datetimeStarted
  344. } else {
  345. let delta = ''
  346. try {
  347. delta = (new Date(logEntry.value.datetimeFinished) - new Date(logEntry.value.datetimeStarted)) / 1000
  348. delta = new Intl.RelativeTimeFormat().format(delta, 'seconds').replace('in ', '').replace('ago', '')
  349. } catch (e) {
  350. console.warn('Failed to calculate delta', e)
  351. }
  352. duration.value = formatDurationRange(
  353. logEntry.value.datetimeStarted,
  354. logEntry.value.datetimeFinished
  355. )
  356. if (delta !== '') {
  357. duration.value += ' (' + delta + ')'
  358. }
  359. }
  360. }
  361. async function renderExecutionResult (res) {
  362. logEntry.value = res.logEntry
  363. if (res.backToDashboards) {
  364. backToDashboards.value = res.backToDashboards.slice(0, 3)
  365. }
  366. // Clear ticker
  367. if (executionTicker) {
  368. clearInterval(executionTicker)
  369. }
  370. executionTicker = null
  371. if (hideDetailsOnResult.value) {
  372. hideDetails.value = true
  373. }
  374. executionTrackingId.value = res.logEntry.executionTrackingId
  375. canRerun.value = res.logEntry.executionFinished && !!res.logEntry.bindingId
  376. canKill.value = res.logEntry.canKill
  377. icon.value = res.logEntry.actionIcon
  378. title.value = res.logEntry.actionTitle
  379. titleTooltip.value = 'Action ID: ' + res.logEntry.bindingId + '\nExecution ID: ' + res.logEntry.executionTrackingId
  380. actionId.value = res.logEntry.bindingId
  381. updateDuration(res.logEntry)
  382. if (terminal) {
  383. await terminal.reset()
  384. await terminal.write(res.logEntry.output, () => {
  385. terminal.fit()
  386. })
  387. }
  388. }
  389. function renderError (err) {
  390. window.showBigError('execution-dlg-err', 'in the execution dialog', 'Failed to fetch execution result. ' + err, false)
  391. }
  392. function handleClose () {
  393. if (executionTicker) {
  394. clearInterval(executionTicker)
  395. }
  396. executionTicker = null
  397. }
  398. function cleanup () {
  399. if (executionTicker) {
  400. clearInterval(executionTicker)
  401. }
  402. executionTicker = null
  403. if (terminal != null) {
  404. terminal.close()
  405. }
  406. terminal = null
  407. }
  408. function goBack () {
  409. router.back()
  410. }
  411. function goToDashboard (path) {
  412. router.push(path)
  413. }
  414. onMounted(() => {
  415. document.addEventListener('fullscreenchange', (e) => {
  416. setTimeout(() => { // Wait for the DOM to settle
  417. if (document.fullscreenElement) {
  418. terminal.fit()
  419. } else {
  420. terminal.resize(80, 40)
  421. terminal.fit()
  422. }
  423. }, 100)
  424. })
  425. initializeTerminal()
  426. fetchExecutionResult(props.executionTrackingId)
  427. watch(
  428. () => buttonResults[props.executionTrackingId],
  429. (newResult, oldResult) => {
  430. if (newResult) {
  431. renderExecutionResult({
  432. logEntry: newResult
  433. })
  434. }
  435. }
  436. )
  437. })
  438. onBeforeUnmount(() => {
  439. cleanup()
  440. })
  441. // Expose methods for parent/imperative use
  442. defineExpose({
  443. reset,
  444. show,
  445. rerunAction,
  446. killAction,
  447. fetchExecutionResult,
  448. renderExecutionResult,
  449. hideEverythingApartFromOutput,
  450. handleClose
  451. })
  452. </script>
  453. <style scoped>
  454. .section-title-with-icon {
  455. display: inline-flex;
  456. align-items: center;
  457. gap: 0.5rem;
  458. }
  459. .action-title-icon {
  460. font-size: 1.5rem;
  461. }
  462. .action-details-title-link {
  463. display: inline-flex;
  464. align-items: center;
  465. gap: 0.5rem;
  466. color: inherit;
  467. text-decoration: none;
  468. }
  469. .action-details-title-link:hover {
  470. text-decoration: underline;
  471. }
  472. .xterm-output-container {
  473. position: relative;
  474. }
  475. .xterm-overlay-toolbar {
  476. position: absolute;
  477. top: 0.5rem;
  478. right: 0.5rem;
  479. z-index: 2;
  480. display: flex;
  481. gap: 0.35rem;
  482. }
  483. .xterm-overlay-button {
  484. display: inline-flex;
  485. align-items: center;
  486. justify-content: center;
  487. padding: 0.35rem;
  488. border: 1px solid rgba(255, 255, 255, 0.25);
  489. border-radius: 0.25rem;
  490. background: rgba(30, 30, 30, 0.85);
  491. color: #f0f0f0;
  492. cursor: pointer;
  493. line-height: 1;
  494. }
  495. .xterm-overlay-button:hover {
  496. background: rgba(50, 50, 50, 0.95);
  497. border-color: rgba(255, 255, 255, 0.45);
  498. color: #fff;
  499. }
  500. .xterm-overlay-button:focus-visible {
  501. outline: 2px solid rgba(255, 255, 255, 0.6);
  502. outline-offset: 2px;
  503. }
  504. .action-history-link {
  505. color: var(--link-color, #007bff);
  506. text-decoration: none;
  507. display: inline-block;
  508. font-size: 0.9rem;
  509. }
  510. .error-message {
  511. background-color: #f8d7da;
  512. border: 1px solid #f5c2c7;
  513. border-radius: 0.25rem;
  514. padding: 1.5rem;
  515. margin: 1rem 0;
  516. }
  517. .error-message h3 {
  518. margin: 0 0 0.5rem 0;
  519. color: #721c24;
  520. }
  521. .error-message p {
  522. margin: 0.5rem 0;
  523. color: #721c24;
  524. }
  525. .error-message code {
  526. background-color: #f8d7da;
  527. padding: 0.125rem 0.25rem;
  528. border-radius: 0.125rem;
  529. font-family: monospace;
  530. }
  531. .error-message a {
  532. color: #721c24;
  533. text-decoration: underline;
  534. font-weight: 500;
  535. }
  536. </style>