LogsQueueView.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. <template>
  2. <Section :title="t('logs.queue-title')" :padding="false">
  3. <template #toolbar>
  4. <router-link to="/logs" class="button neutral">
  5. <svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" viewBox="0 0 24 24">
  6. <path fill="currentColor" d="M20 11H7.83l5.59-5.59L12 4l-8 8l8 8l1.41-1.41L7.83 13H20z"/>
  7. </svg>
  8. {{ t('logs.back-to-list') }}
  9. </router-link>
  10. </template>
  11. <p class="padding">{{ t('logs.queue-page-description') }}</p>
  12. <div v-if="groups.length === 0 && !loading" class="empty-state padding">
  13. <p>{{ t('logs.queue-empty') }}</p>
  14. <router-link to="/logs">{{ t('logs.back-to-list') }}</router-link>
  15. </div>
  16. </Section>
  17. <section
  18. v-for="actionGroup in groups"
  19. :key="actionGroup.name"
  20. class="with-header-and-content queue-action-group-section"
  21. >
  22. <div class="section-header flex-row">
  23. <div class="fg1 queue-action-group-heading">
  24. <ActionIconGlyph class="icon" :glyph="actionGroup.icon" />
  25. <h2>{{ displayActionGroupName(actionGroup.name) }}</h2>
  26. </div>
  27. <ActionGroupLimitsLabel
  28. :max-concurrent="actionGroup.maxConcurrent"
  29. :queue-size="actionGroup.queueSize"
  30. />
  31. </div>
  32. <div class="section-content">
  33. <table class="logs-table row-hover">
  34. <thead>
  35. <tr>
  36. <th>{{ t('logs.timestamp') }}</th>
  37. <th>{{ t('logs.action') }}</th>
  38. <th>{{ t('logs.metadata') }}</th>
  39. <th>{{ t('logs.status') }}</th>
  40. </tr>
  41. </thead>
  42. <tbody>
  43. <template v-for="action in actionGroup.actions" :key="`${actionGroup.name}:${action.bindingId}`">
  44. <tr
  45. v-for="(entry, index) in action.entries"
  46. :key="entry.executionTrackingId"
  47. class="log-row"
  48. :title="action.actionTitle"
  49. >
  50. <td class="timestamp">{{ formatTimestamp(entry.datetimeStarted) }}</td>
  51. <td>
  52. <ActionIconGlyph class="icon" :glyph="action.actionIcon" />
  53. <router-link :to="`/logs/${entry.executionTrackingId}`">
  54. <LogActionTitle
  55. :action-title="action.actionTitle"
  56. :justification="entry.justification"
  57. />
  58. </router-link>
  59. <span v-if="action.entityPrefix" class="queue-entity annotation">
  60. {{ action.entityPrefix }}
  61. </span>
  62. </td>
  63. <td class="tags">
  64. <span class="annotation">
  65. <span class="annotation-key">User:</span>
  66. <span class="annotation-val">{{ entry.user }}</span>
  67. </span>
  68. <span v-if="entry.tags && entry.tags.length > 0" class="tag-list">
  69. <span v-for="tag in entry.tags" :key="tag" class="tag">{{ tag }}</span>
  70. </span>
  71. </td>
  72. <td class="exit-code">
  73. <span v-if="!entry.executionFinished" class="queue-position">
  74. {{ t('logs.queue-position', { position: index + 1 }) }}
  75. </span>
  76. <ActionStatusDisplay :logEntry="entry" link-queued-status />
  77. </td>
  78. </tr>
  79. </template>
  80. </tbody>
  81. </table>
  82. </div>
  83. </section>
  84. </template>
  85. <script setup>
  86. import { ref, onMounted, onUnmounted } from 'vue'
  87. import Section from 'picocrank/vue/components/Section.vue'
  88. import ActionIconGlyph from '../components/ActionIconGlyph.vue'
  89. import ActionStatusDisplay from '../components/ActionStatusDisplay.vue'
  90. import LogActionTitle from '../components/LogActionTitle.vue'
  91. import ActionGroupLimitsLabel from '../components/ActionGroupLimitsLabel.vue'
  92. import { useI18n } from 'vue-i18n'
  93. import { getExecutionLogEntry, cloneLogEntry, updateLogEntryInGroups } from '../utils/executionLogEvents.js'
  94. const defaultActionGroupName = 'default'
  95. const { t } = useI18n()
  96. const groups = ref([])
  97. const loading = ref(false)
  98. function displayActionGroupName (name) {
  99. if (name === defaultActionGroupName) {
  100. return t('logs.queue-default-group')
  101. }
  102. return name
  103. }
  104. function collectCompletedEntries (currentGroups) {
  105. const completed = []
  106. for (const group of currentGroups || []) {
  107. for (const action of group.actions || []) {
  108. for (const entry of action.entries || []) {
  109. if (entry.executionFinished) {
  110. completed.push(cloneLogEntry(entry))
  111. }
  112. }
  113. }
  114. }
  115. return completed
  116. }
  117. function sortGroupEntries (entries) {
  118. entries.sort((left, right) => {
  119. if (left.executionFinished !== right.executionFinished) {
  120. return left.executionFinished ? 1 : -1
  121. }
  122. return (left.datetimeStarted || '').localeCompare(right.datetimeStarted || '')
  123. })
  124. }
  125. function sortGroups (groupList) {
  126. groupList.sort((left, right) => {
  127. if (left.name === defaultActionGroupName) {
  128. return 1
  129. }
  130. if (right.name === defaultActionGroupName) {
  131. return -1
  132. }
  133. return (left.name || '').localeCompare(right.name || '')
  134. })
  135. }
  136. function sumActionEntries (actions) {
  137. return (actions || []).reduce((total, action) => total + (action.entries || []).length, 0)
  138. }
  139. function cloneActionGroup (group) {
  140. return {
  141. ...group,
  142. actions: (group.actions || []).map(action => ({
  143. ...action,
  144. entries: [...(action.entries || [])]
  145. }))
  146. }
  147. }
  148. function findActionInGroups (groupList, bindingId) {
  149. const matches = []
  150. for (const group of groupList) {
  151. for (const action of group.actions || []) {
  152. if (action.bindingId === bindingId) {
  153. matches.push({ group, action })
  154. }
  155. }
  156. }
  157. return matches
  158. }
  159. function mergeCompletedEntries (apiGroups, completedEntries) {
  160. const merged = (apiGroups || []).map(cloneActionGroup)
  161. for (const entry of completedEntries) {
  162. const alreadyPresent = merged.some(group =>
  163. (group.actions || []).some(action =>
  164. (action.entries || []).some(item => item.executionTrackingId === entry.executionTrackingId)
  165. )
  166. )
  167. if (alreadyPresent) {
  168. continue
  169. }
  170. const matches = findActionInGroups(merged, entry.bindingId)
  171. if (matches.length === 0) {
  172. continue
  173. }
  174. for (const { action } of matches) {
  175. action.entries.push(entry)
  176. }
  177. }
  178. for (const group of merged) {
  179. for (const action of group.actions || []) {
  180. sortGroupEntries(action.entries)
  181. action.activeCount = action.entries.length
  182. }
  183. refreshGroupCounts(group)
  184. }
  185. sortGroups(merged)
  186. return merged
  187. }
  188. function forEachActionWithEntry (groupList, executionTrackingId, callback) {
  189. for (const group of groupList) {
  190. for (const action of group.actions || []) {
  191. const hasEntry = (action.entries || []).some(
  192. item => item.executionTrackingId === executionTrackingId
  193. )
  194. if (hasEntry) {
  195. callback(group, action)
  196. }
  197. }
  198. }
  199. }
  200. function refreshGroupCounts (group) {
  201. let queued = 0
  202. for (const action of group.actions || []) {
  203. action.activeCount = (action.entries || []).length
  204. for (const entry of action.entries || []) {
  205. if (entry.queued) {
  206. queued++
  207. }
  208. }
  209. }
  210. group.activeCount = sumActionEntries(group.actions)
  211. group.queuedCount = queued
  212. }
  213. function applyQueueEntryUpdate (logEntry, afterUpdate) {
  214. const result = updateLogEntryInGroups(groups.value, logEntry)
  215. if (!result) {
  216. return false
  217. }
  218. if (afterUpdate) {
  219. afterUpdate(result)
  220. }
  221. forEachActionWithEntry(groups.value, logEntry.executionTrackingId, (group, action) => {
  222. sortGroupEntries(action.entries)
  223. refreshGroupCounts(group)
  224. })
  225. return true
  226. }
  227. function insertActiveQueueEntry (logEntry) {
  228. if (!logEntry?.bindingId || !logEntry.executionTrackingId || logEntry.executionFinished) {
  229. fetchQueue()
  230. return
  231. }
  232. const matches = findActionInGroups(groups.value, logEntry.bindingId)
  233. if (matches.length === 0) {
  234. fetchQueue()
  235. return
  236. }
  237. const touchedGroups = new Set()
  238. for (const { group, action } of matches) {
  239. action.entries.push(cloneLogEntry(logEntry))
  240. touchedGroups.add(group)
  241. sortGroupEntries(action.entries)
  242. }
  243. for (const group of touchedGroups) {
  244. refreshGroupCounts(group)
  245. }
  246. sortGroups(groups.value)
  247. }
  248. function onExecutionStarted (evt) {
  249. const logEntry = getExecutionLogEntry(evt)
  250. if (!logEntry) {
  251. return
  252. }
  253. if (!applyQueueEntryUpdate(logEntry)) {
  254. insertActiveQueueEntry(logEntry)
  255. }
  256. }
  257. function onExecutionFinished (evt) {
  258. const logEntry = getExecutionLogEntry(evt)
  259. if (!logEntry) {
  260. return
  261. }
  262. applyQueueEntryUpdate(logEntry)
  263. }
  264. function formatTimestamp (timestamp) {
  265. if (!timestamp) {
  266. return 'Unknown'
  267. }
  268. try {
  269. return new Date(timestamp).toLocaleString()
  270. } catch (err) {
  271. return timestamp
  272. }
  273. }
  274. async function fetchQueue () {
  275. loading.value = true
  276. try {
  277. const completedEntries = collectCompletedEntries(groups.value)
  278. const response = await window.client.getExecutionQueue({})
  279. groups.value = mergeCompletedEntries(response.groups || [], completedEntries)
  280. } catch (err) {
  281. console.error('Failed to fetch execution queue:', err)
  282. window.showBigError('fetch-queue', 'getting execution queue', err, false)
  283. } finally {
  284. loading.value = false
  285. }
  286. }
  287. onMounted(() => {
  288. fetchQueue()
  289. window.addEventListener('EventExecutionStarted', onExecutionStarted)
  290. window.addEventListener('EventExecutionFinished', onExecutionFinished)
  291. })
  292. onUnmounted(() => {
  293. window.removeEventListener('EventExecutionStarted', onExecutionStarted)
  294. window.removeEventListener('EventExecutionFinished', onExecutionFinished)
  295. })
  296. </script>
  297. <style scoped>
  298. .queue-action-group-heading {
  299. display: flex;
  300. align-items: center;
  301. gap: 0.5rem;
  302. min-width: 0;
  303. }
  304. .queue-action-group-heading h2 {
  305. margin: 0;
  306. }
  307. .timestamp {
  308. font-family: monospace;
  309. font-size: 0.875rem;
  310. color: #666;
  311. }
  312. .icon {
  313. margin-right: 0.5rem;
  314. font-size: 1.2em;
  315. }
  316. .annotation {
  317. font-weight: 500;
  318. font-size: smaller;
  319. }
  320. .queue-entity {
  321. display: block;
  322. margin-top: 0.25rem;
  323. color: #666;
  324. }
  325. .exit-code {
  326. display: flex;
  327. align-items: center;
  328. gap: 0.5rem;
  329. }
  330. .queue-position {
  331. white-space: nowrap;
  332. }
  333. .empty-state {
  334. text-align: center;
  335. padding: 2rem;
  336. color: #666;
  337. }
  338. .empty-state a {
  339. color: #007bff;
  340. text-decoration: none;
  341. }
  342. .empty-state a:hover {
  343. text-decoration: underline;
  344. }
  345. </style>