ExecutionDialog.js 6.5 KB

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