ExecutionDialog.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // This ExecutionDialog is NOT a custom HTML element, but rather just picks up
  2. // the <dialog /> element out of index.html and just re-uses that - as only
  3. // one dialog can be shown at a time.
  4. export class ExecutionDialog {
  5. constructor () {
  6. this.dlg = document.querySelector('dialog#execution-results-popup')
  7. this.domIcon = document.getElementById('execution-dialog-icon')
  8. this.domTitle = document.getElementById('execution-dialog-title')
  9. this.domOutput = document.getElementById('execution-dialog-xterm')
  10. this.domOutputDetails = document.getElementById('execution-dialog-output-details')
  11. this.domOutputToggleBig = document.getElementById('execution-dialog-toggle-size')
  12. this.domOutputToggleBig.onclick = () => {
  13. this.toggleSize()
  14. }
  15. this.domBtnKill = document.getElementById('execution-dialog-kill-action')
  16. this.domDatetimeStarted = document.getElementById('execution-dialog-datetime-started')
  17. this.domDatetimeFinished = document.getElementById('execution-dialog-datetime-finished')
  18. this.domExitCode = document.getElementById('execution-dialog-exit-code')
  19. this.domStatus = document.getElementById('execution-dialog-status')
  20. this.domExecutionBasics = document.getElementById('execution-dialog-basics')
  21. this.domExecutionDetails = document.getElementById('execution-dialog-details')
  22. this.domExecutionOutput = document.getElementById('execution-dialog-output')
  23. window.terminal.open(this.domOutput)
  24. }
  25. showOutput () {
  26. this.domOutput.hidden = false
  27. this.domOutputDetails.open = true
  28. this.domExecutionOutput.hidden = false
  29. }
  30. toggleSize () {
  31. if (this.dlg.classList.contains('big')) {
  32. this.dlg.classList.remove('big')
  33. this.domOutputDetails.open = false
  34. } else {
  35. this.dlg.classList.add('big')
  36. this.domOutputDetails.open = true
  37. }
  38. }
  39. reset () {
  40. this.executionSeconds = 0
  41. this.executionTrackingId = 'notset'
  42. this.dlg.classList.remove('big')
  43. this.domOutputToggleBig.hidden = false
  44. this.domIcon.innerText = ''
  45. this.domTitle.innerText = 'Waiting for result... '
  46. this.domExitCode.innerText = '?'
  47. this.domStatus.className = ''
  48. this.domDatetimeStarted.innerText = ''
  49. this.domDatetimeFinished.innerText = ''
  50. // window.terminal.close()
  51. this.domBtnKill.disabled = true
  52. this.domBtnKill.onclick = () => {}
  53. this.hideDetailsOnResult = false
  54. this.domExecutionBasics.hidden = false
  55. this.domExecutionDetails.hidden = true
  56. this.domOutputDetails.open = false
  57. window.terminal.reset()
  58. this.domExecutionOutput.hidden = true
  59. }
  60. show (actionButton) {
  61. if (typeof actionButton !== 'undefined' && actionButton != null) {
  62. this.domIcon.innerText = actionButton.domIcon.innerText
  63. }
  64. this.domBtnKill.disabled = false
  65. this.domBtnKill.onclick = () => {
  66. this.killAction()
  67. }
  68. clearInterval(window.executionDialogTicker)
  69. this.executionSeconds = 0
  70. this.executionTick()
  71. window.executionDialogTicker = setInterval(() => {
  72. this.executionTick()
  73. }, 1000)
  74. if (this.dlg.open) {
  75. this.dlg.close()
  76. }
  77. this.dlg.showModal()
  78. }
  79. killAction () {
  80. const killActionArgs = {
  81. executionTrackingId: this.executionTrackingId
  82. }
  83. window.fetch(window.restBaseUrl + 'KillAction', {
  84. method: 'POST',
  85. headers: {
  86. 'Content-Type': 'application/json'
  87. },
  88. body: JSON.stringify(killActionArgs)
  89. }).then((res) => {
  90. console.log(res.json())
  91. }).catch(err => {
  92. throw err
  93. })
  94. }
  95. executionTick () {
  96. this.executionSeconds++
  97. this.domDatetimeStarted.innerText = this.executionSeconds + ' seconds ago'
  98. }
  99. hideEverythingApartFromOutput () {
  100. this.hideDetailsOnResult = true
  101. this.domExecutionBasics.hidden = true
  102. }
  103. fetchExecutionResult (executionTrackingId) {
  104. this.executionTrackingId = executionTrackingId
  105. const executionStatusArgs = {
  106. executionTrackingId: this.executionTrackingId
  107. }
  108. window.fetch(window.restBaseUrl + 'ExecutionStatus', {
  109. method: 'POST',
  110. headers: {
  111. 'Content-Type': 'application/json'
  112. },
  113. body: JSON.stringify(executionStatusArgs)
  114. }).then((res) => {
  115. if (res.ok) {
  116. return res.json()
  117. } else {
  118. throw new Error(res.statusText)
  119. }
  120. }
  121. ).then((json) => {
  122. this.renderExecutionResult(json)
  123. }).catch(err => {
  124. this.renderError(err)
  125. })
  126. }
  127. renderExecutionResult (res) {
  128. clearInterval(window.executionDialogTicker)
  129. this.domExecutionOutput.hidden = false
  130. if (!this.hideDetailsOnResult) {
  131. this.domExecutionDetails.hidden = false
  132. } else {
  133. this.domOutputDetails.open = true
  134. }
  135. this.executionTrackingId = res.logEntry.executionTrackingId
  136. this.domBtnKill.disabled = res.logEntry.executionFinished
  137. if (res.logEntry.executionFinished) {
  138. this.domStatus.innerText = 'Completed'
  139. this.domStatus.classList.add('action-success')
  140. this.domDatetimeFinished.innerText = res.logEntry.datetimeFinished
  141. if (res.logEntry.timedOut) {
  142. this.domExitCode.innerText = 'Timed out'
  143. this.domStatus.classList.add('action-timeout')
  144. } else if (res.logEntry.blocked) {
  145. this.domStatus.innerText = 'Blocked'
  146. this.domStatus.classList.add('action-blocked')
  147. } else if (res.logEntry.exitCode !== 0) {
  148. this.domStatus.innerText = 'Non-Zero Exit'
  149. this.domStatus.classList.add('action-nonzero-exit')
  150. } else {
  151. this.domExitCode.innerText = res.logEntry.exitCode
  152. }
  153. } else {
  154. this.domDatetimeFinished.innerText = 'Still running...'
  155. this.domExitCode.innerText = 'Still running...'
  156. this.domStatus.innerText = 'Still running...'
  157. }
  158. this.domIcon.innerHTML = res.logEntry.actionIcon
  159. this.domTitle.innerText = res.logEntry.actionTitle
  160. this.domDatetimeStarted.innerText = res.logEntry.datetimeStarted
  161. window.terminal.reset()
  162. window.terminal.write(res.logEntry.output)
  163. }
  164. renderError (err) {
  165. this.dlg.querySelector('pre').innerText = JSON.stringify(err)
  166. }
  167. }