ExecutionDialog.js 5.5 KB

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