ExecutionDialog.js 5.9 KB

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