ExecutionDialog.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. this.dlg.showModal()
  65. }
  66. executionTick () {
  67. this.executionSeconds++
  68. this.domDatetimeStarted.innerText = this.executionSeconds + ' seconds ago'
  69. }
  70. hideEverythingApartFromOutput () {
  71. this.hideDetailsOnResult = true
  72. this.domExecutionBasics.hidden = true
  73. }
  74. fetchExecutionResult (uuid) {
  75. this.executionTrackingId = uuid
  76. const executionStatusArgs = {
  77. executionTrackingId: this.executionTrackingId
  78. }
  79. window.fetch(window.restBaseUrl + 'ExecutionStatus', {
  80. method: 'POST',
  81. headers: {
  82. 'Content-Type': 'application/json'
  83. },
  84. body: JSON.stringify(executionStatusArgs)
  85. }).then((res) => {
  86. if (res.ok) {
  87. return res.json()
  88. } else {
  89. throw new Error(res.statusText)
  90. }
  91. }
  92. ).then((json) => {
  93. this.renderExecutionResult(json)
  94. }).catch(err => {
  95. this.renderError(err)
  96. })
  97. }
  98. renderExecutionResult (res) {
  99. clearInterval(window.executionDialogTicker)
  100. this.domExecutionOutput.hidden = false
  101. if (!this.hideDetailsOnResult) {
  102. this.domExecutionDetails.hidden = false
  103. } else {
  104. this.domStdout.parentElement.open = true
  105. }
  106. this.executionTrackingId = res.logEntry.executionTrackingId
  107. if (res.logEntry.executionFinished) {
  108. this.domStatus.innerText = 'Completed'
  109. this.domStatus.classList.add('action-success')
  110. this.domDatetimeFinished.innerText = res.logEntry.datetimeFinished
  111. if (res.logEntry.timedOut) {
  112. this.domExitCode.innerText = 'Timed out'
  113. this.domStatus.classList.add('action-timeout')
  114. } else if (res.logEntry.blocked) {
  115. this.domStatus.innerText = 'Blocked'
  116. this.domStatus.classList.add('action-blocked')
  117. } else if (res.logEntry.exitCode !== 0) {
  118. this.domStatus.innerText = 'Non-Zero Exit'
  119. this.domStatus.classList.add('action-nonzero-exit')
  120. } else {
  121. this.domExitCode.innerText = res.logEntry.exitCode
  122. }
  123. } else {
  124. this.domDatetimeFinished.innerText = 'Still running...'
  125. this.domExitCode.innerText = 'Still running...'
  126. this.domStatus.innerText = 'Still running...'
  127. }
  128. this.domIcon.innerHTML = res.logEntry.actionIcon
  129. this.domTitle.innerText = res.logEntry.actionTitle
  130. this.domStdout.innerText = res.logEntry.stdout
  131. this.domStdout.innerText = res.logEntry.stdout
  132. if (res.logEntry.stderr === '(empty)') {
  133. this.domStderr.parentElement.style.display = 'none'
  134. this.domStderr.innerText = res.logEntry.stderr
  135. } else {
  136. this.domStderr.parentElement.style.display = 'block'
  137. this.domStderr.innerText = res.logEntry.stderr
  138. }
  139. this.domDatetimeStarted.innerText = res.logEntry.datetimeStarted
  140. }
  141. renderError (err) {
  142. this.dlg.querySelector('pre').innerText = JSON.stringify(err)
  143. }
  144. }