ExecutionDialog.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. import { ActionStatusDisplay } from './ActionStatusDisplay.js'
  2. import { OutputTerminal } from './OutputTerminal.js'
  3. // This ExecutionDialog is NOT a custom HTML element, but rather just picks up
  4. // the <dialog /> element out of index.html and just re-uses that - as only
  5. // one dialog can be shown at a time.
  6. export class ExecutionDialog {
  7. constructor () {
  8. this.dlg = document.querySelector('dialog#execution-results-popup')
  9. this.domIcon = document.getElementById('execution-dialog-icon')
  10. this.domTitle = document.getElementById('execution-dialog-title')
  11. this.domOutput = document.getElementById('execution-dialog-xterm')
  12. this.domOutputHtml = document.getElementById('execution-dialog-output-html')
  13. this.domOutputToggleBig = document.getElementById('execution-dialog-toggle-size')
  14. this.domOutputToggleBig.onclick = () => {
  15. this.toggleSize()
  16. }
  17. this.domBtnRerun = document.getElementById('execution-dialog-rerun-action')
  18. this.domBtnKill = document.getElementById('execution-dialog-kill-action')
  19. this.domDuration = document.getElementById('execution-dialog-duration')
  20. this.domStatus = new ActionStatusDisplay(document.getElementById('execution-dialog-status'))
  21. this.domExecutionBasics = document.getElementById('execution-dialog-basics')
  22. this.domExecutionDetails = document.getElementById('execution-dialog-details')
  23. window.terminal = new OutputTerminal()
  24. window.terminal.open(this.domOutput)
  25. window.terminal.resize(80, 24)
  26. }
  27. toggleSize () {
  28. if (this.dlg.classList.contains('big')) {
  29. this.dlg.classList.remove('big')
  30. window.terminal.resize(80, 24)
  31. } else {
  32. this.dlg.classList.add('big')
  33. window.terminal.fit()
  34. }
  35. }
  36. async reset () {
  37. this.executionSeconds = 0
  38. this.executionTrackingId = 'notset'
  39. this.dlg.classList.remove('big')
  40. this.domOutputToggleBig.hidden = false
  41. this.domIcon.innerText = ''
  42. this.domTitle.innerText = 'Waiting for result... '
  43. this.domDuration.innerText = ''
  44. // window.terminal.close()
  45. this.domBtnRerun.disabled = true
  46. this.domBtnRerun.onclick = () => {}
  47. this.domBtnKill.disabled = true
  48. this.domBtnKill.onclick = () => {}
  49. this.hideDetailsOnResult = false
  50. this.domExecutionBasics.hidden = false
  51. this.domExecutionDetails.hidden = true
  52. await window.terminal.reset()
  53. window.terminal.fit()
  54. }
  55. show (actionButton) {
  56. if (typeof actionButton !== 'undefined' && actionButton != null) {
  57. this.domIcon.innerText = actionButton.domIcon.innerText
  58. }
  59. this.domBtnKill.disabled = false
  60. this.domBtnKill.onclick = () => {
  61. this.killAction()
  62. }
  63. clearInterval(window.executionDialogTicker)
  64. this.executionSeconds = 0
  65. this.executionTick()
  66. window.executionDialogTicker = setInterval(() => {
  67. this.executionTick()
  68. }, 1000)
  69. if (this.dlg.open) {
  70. this.dlg.close()
  71. }
  72. this.dlg.showModal()
  73. }
  74. rerunAction (actionId) {
  75. const actionButton = document.getElementById('actionButton-' + actionId)
  76. if (actionButton !== undefined) {
  77. actionButton.btn.click()
  78. }
  79. this.dlg.close()
  80. }
  81. killAction () {
  82. const killActionArgs = {
  83. executionTrackingId: this.executionTrackingId
  84. }
  85. window.fetch(window.restBaseUrl + 'KillAction', {
  86. cors: 'cors',
  87. method: 'POST',
  88. headers: {
  89. 'Content-Type': 'application/json'
  90. },
  91. body: JSON.stringify(killActionArgs)
  92. }).then((res) => {
  93. return res.json() // This isn't used by anything. UI is updated by OnExecutionFinished like normal.
  94. }).catch(err => {
  95. throw err
  96. })
  97. }
  98. executionTick () {
  99. this.executionSeconds++
  100. this.updateDuration(null)
  101. }
  102. hideEverythingApartFromOutput () {
  103. this.hideDetailsOnResult = true
  104. this.domExecutionBasics.hidden = true
  105. }
  106. fetchExecutionResult (executionTrackingId) {
  107. this.executionTrackingId = executionTrackingId
  108. const executionStatusArgs = {
  109. executionTrackingId: this.executionTrackingId
  110. }
  111. window.fetch(window.restBaseUrl + 'ExecutionStatus', {
  112. cors: 'cors',
  113. method: 'POST',
  114. headers: {
  115. 'Content-Type': 'application/json'
  116. },
  117. body: JSON.stringify(executionStatusArgs)
  118. }).then((res) => {
  119. if (res.ok) {
  120. return res.json()
  121. } else if (res.status === 404) {
  122. throw new Error('Execution not found: ' + executionTrackingId)
  123. } else {
  124. throw new Error(res.statusText)
  125. }
  126. }
  127. ).then((json) => {
  128. this.renderExecutionResult(json)
  129. }).catch(err => {
  130. console.log(err)
  131. this.renderError(err)
  132. })
  133. }
  134. updateDuration (logEntry) {
  135. if (logEntry == null) {
  136. this.domDuration.innerHTML = this.executionSeconds + ' seconds'
  137. } else if (!logEntry.executionStarted) {
  138. this.domDuration.innerHTML = logEntry.datetimeStarted + ' (request time). Not executed.'
  139. } else if (logEntry.executionStarted && !logEntry.executionFinished) {
  140. this.domDuration.innerHTML = logEntry.datetimeStarted
  141. } else {
  142. let delta = ''
  143. try {
  144. delta = (new Date(logEntry.datetimeStarted) - new Date(logEntry.datetimeStarted)) / 1000
  145. delta = new Intl.RelativeTimeFormat().format(delta, 'seconds').replace('in ', '').replace('ago', '')
  146. } catch (e) {
  147. console.warn('Failed to calculate delta', e)
  148. }
  149. this.domDuration.innerHTML = logEntry.datetimeStarted + ' &rarr; ' + logEntry.datetimeFinished
  150. if (delta !== '') {
  151. this.domDuration.innerHTML += ' (' + delta + ')'
  152. }
  153. }
  154. }
  155. async renderExecutionResult (res) {
  156. this.res = res
  157. clearInterval(window.executionDialogTicker)
  158. if ('type' in res && res.type === 'execution-dialog-output-html') {
  159. this.domOutputHtml.hidden = false
  160. this.domOutput.hidden = true
  161. this.domOutputHtml.innerHTML = res.logEntry.output
  162. this.domOutputHtml.hidden = false
  163. this.hideDetailsonResult = true
  164. } else {
  165. this.domOutput.hidden = false
  166. this.domOutputHtml.innerHTML = ''
  167. this.domOutputHtml.hidden = true
  168. }
  169. if (this.hideDetailsOnResult) {
  170. this.domExecutionDetails.hidden = true
  171. }
  172. this.executionTrackingId = res.logEntry.executionTrackingId
  173. this.domBtnRerun.disabled = !res.logEntry.executionFinished
  174. this.domBtnRerun.onclick = () => { this.rerunAction(res.logEntry.actionId) }
  175. this.domBtnKill.disabled = !res.logEntry.canKill
  176. this.domStatus.update(res.logEntry)
  177. this.domIcon.innerHTML = res.logEntry.actionIcon
  178. this.domTitle.innerText = res.logEntry.actionTitle
  179. this.domTitle.title = 'Action ID: ' + res.logEntry.actionId + '\nExecution ID: ' + res.logEntry.executionTrackingId
  180. this.updateDuration(res.logEntry)
  181. await window.terminal.reset()
  182. await window.terminal.write(res.logEntry.output, () => {
  183. window.terminal.fit()
  184. })
  185. }
  186. renderError (err) {
  187. window.showBigError('execution-dlg-err', 'in the execution dialog', 'Failed to fetch execution result. ' + err, false)
  188. }
  189. }