ExecutionView.vue 13 KB

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