ExecutionDialog.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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.domStdout = document.getElementById('execution-dialog-stdout')
  10. this.domStderr = document.getElementById('execution-dialog-stderr')
  11. this.domStdoutToggleBig = document.getElementById('execution-dialog-toggle-size')
  12. this.domStdoutToggleBig.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. }
  24. toggleSize () {
  25. if (this.dlg.classList.contains('big')) {
  26. this.dlg.classList.remove('big')
  27. this.domStdout.parentElement.open = false
  28. } else {
  29. this.dlg.classList.add('big')
  30. this.domStdout.parentElement.open = true
  31. }
  32. }
  33. reset () {
  34. this.executionSeconds = 0
  35. this.executionTrackingId = 'notset'
  36. this.dlg.classList.remove('big')
  37. this.dlg.style.maxWidth = 'calc(100vw - 2em)'
  38. this.dlg.style.width = ''
  39. this.dlg.style.height = ''
  40. this.dlg.style.border = ''
  41. this.domStdoutToggleBig.hidden = false
  42. this.domIcon.innerText = ''
  43. this.domTitle.innerText = 'Waiting for result... '
  44. this.domExitCode.innerText = '?'
  45. this.domStatus.className = ''
  46. this.domDatetimeStarted.innerText = ''
  47. this.domDatetimeFinished.innerText = ''
  48. this.domStdout.innerText = ''
  49. this.domStderr.innerText = ''
  50. this.domBtnKill.disabled = true
  51. this.domBtnKill.onclick = () => {}
  52. this.hideDetailsOnResult = false
  53. this.domExecutionBasics.hidden = false
  54. this.domExecutionDetails.hidden = true
  55. this.domStdout.parentElement.open = false
  56. this.domExecutionOutput.hidden = true
  57. }
  58. show (actionButton) {
  59. if (typeof actionButton !== 'undefined' && actionButton != null) {
  60. this.domIcon.innerText = actionButton.domIcon.innerText
  61. }
  62. this.domBtnKill.disabled = false
  63. this.domBtnKill.onclick = () => {
  64. this.killAction()
  65. }
  66. clearInterval(window.executionDialogTicker)
  67. this.executionSeconds = 0
  68. this.executionTick()
  69. window.executionDialogTicker = setInterval(() => {
  70. this.executionTick()
  71. }, 1000)
  72. if (this.dlg.open) {
  73. this.dlg.close()
  74. }
  75. this.dlg.showModal()
  76. }
  77. killAction () {
  78. const killActionArgs = {
  79. executionTrackingId: this.executionTrackingId
  80. }
  81. window.fetch(window.restBaseUrl + 'KillAction', {
  82. method: 'POST',
  83. headers: {
  84. 'Content-Type': 'application/json'
  85. },
  86. body: JSON.stringify(killActionArgs)
  87. }).then((res) => {
  88. console.log(res)
  89. }).catch(err => {
  90. throw err
  91. })
  92. }
  93. executionTick () {
  94. this.executionSeconds++
  95. this.domDatetimeStarted.innerText = this.executionSeconds + ' seconds ago'
  96. }
  97. hideEverythingApartFromOutput () {
  98. this.hideDetailsOnResult = true
  99. this.domExecutionBasics.hidden = true
  100. }
  101. fetchExecutionResult (executionTrackingId) {
  102. this.executionTrackingId = executionTrackingId
  103. const executionStatusArgs = {
  104. executionTrackingId: this.executionTrackingId
  105. }
  106. window.fetch(window.restBaseUrl + 'ExecutionStatus', {
  107. method: 'POST',
  108. headers: {
  109. 'Content-Type': 'application/json'
  110. },
  111. body: JSON.stringify(executionStatusArgs)
  112. }).then((res) => {
  113. if (res.ok) {
  114. return res.json()
  115. } else {
  116. throw new Error(res.statusText)
  117. }
  118. }
  119. ).then((json) => {
  120. this.renderExecutionResult(json)
  121. }).catch(err => {
  122. this.renderError(err)
  123. })
  124. }
  125. renderExecutionResult (res) {
  126. clearInterval(window.executionDialogTicker)
  127. this.domExecutionOutput.hidden = false
  128. if (!this.hideDetailsOnResult) {
  129. this.domExecutionDetails.hidden = false
  130. } else {
  131. this.domStdout.parentElement.open = true
  132. }
  133. this.executionTrackingId = res.logEntry.executionTrackingId
  134. this.domBtnKill.disabled = res.logEntry.executionFinished
  135. if (res.logEntry.executionFinished) {
  136. this.domStatus.innerText = 'Completed'
  137. this.domStatus.classList.add('action-success')
  138. this.domDatetimeFinished.innerText = res.logEntry.datetimeFinished
  139. if (res.logEntry.timedOut) {
  140. this.domExitCode.innerText = 'Timed out'
  141. this.domStatus.classList.add('action-timeout')
  142. } else if (res.logEntry.blocked) {
  143. this.domStatus.innerText = 'Blocked'
  144. this.domStatus.classList.add('action-blocked')
  145. } else if (res.logEntry.exitCode !== 0) {
  146. this.domStatus.innerText = 'Non-Zero Exit'
  147. this.domStatus.classList.add('action-nonzero-exit')
  148. } else {
  149. this.domExitCode.innerText = res.logEntry.exitCode
  150. }
  151. } else {
  152. this.domDatetimeFinished.innerText = 'Still running...'
  153. this.domExitCode.innerText = 'Still running...'
  154. this.domStatus.innerText = 'Still running...'
  155. }
  156. this.domIcon.innerHTML = res.logEntry.actionIcon
  157. this.domTitle.innerText = res.logEntry.actionTitle
  158. this.domStdout.innerText = res.logEntry.stdout
  159. this.domStdout.innerText = res.logEntry.stdout
  160. if (res.logEntry.stderr === '(empty)') {
  161. this.domStderr.parentElement.style.display = 'none'
  162. this.domStderr.innerText = res.logEntry.stderr
  163. } else {
  164. this.domStderr.parentElement.style.display = 'block'
  165. this.domStderr.innerText = res.logEntry.stderr
  166. }
  167. this.domDatetimeStarted.innerText = res.logEntry.datetimeStarted
  168. }
  169. renderError (err) {
  170. this.dlg.querySelector('pre').innerText = JSON.stringify(err)
  171. }
  172. }