Line data Source code
1 : import type { JobListItem } from '@comfyorg/api-client/jobTypes'
2 : import { reportError } from '@comfyorg/telemetry/reportError'
3 : import { createSharedComposable, useEventListener } from '@vueuse/core'
4 : import { computed, ref } from 'vue'
5 :
6 : import { mapTaskOutputToAssetItem } from '@/platform/assets/composables/media/assetMappers'
7 : import type { AssetItem } from '@/platform/assets/schemas/assetSchema'
8 : import { api } from '@/scripts/api'
9 : import { TaskItemImpl } from '@/stores/queueStore'
10 : import type { PagedList } from '@/utils/pagedList'
11 :
12 5012 : function mapHistoryToAssets(historyItems: JobListItem[]): AssetItem[] {
13 5012 : const assetItems: AssetItem[] = []
14 :
15 5012 : for (const job of historyItems) {
16 4585 : if (job.status !== 'completed' || !job.preview_output) continue
17 :
18 3724 : const task = new TaskItemImpl(job)
19 4585 : if (!task.previewOutput) continue
20 :
21 3724 : const assetItem = mapTaskOutputToAssetItem(task, task.previewOutput)
22 4585 : assetItem.user_metadata = {
23 : ...assetItem.user_metadata,
24 : outputCount:
25 : task.previewableOutputsCount ??
26 : task.outputsCount ??
27 : task.previewableOutputs.length,
28 : allOutputs: task.previewableOutputs
29 : }
30 4585 : assetItems.push(assetItem)
31 : }
32 :
33 5012 : return assetItems.sort(
34 : (a, b) =>
35 : new Date(b.created_at).getTime() - new Date(a.created_at).getTime()
36 : )
37 : }
38 :
39 2356 : const BATCH_SIZE = 200
40 2356 : const MAX_HISTORY_ITEMS = 1000
41 :
42 2356 : export const useJobOutputHistory = createSharedComposable(
43 : (): PagedList<AssetItem> => {
44 2355 : const historyOffset = ref(0)
45 2355 : const hasMoreHistory = ref(true)
46 2355 : const isLoadingMore = ref(false)
47 2355 : const historyAssets = ref<AssetItem[]>([])
48 :
49 2355 : const fetchHistoryAssets = async (
50 : loadMore = false
51 : ): Promise<AssetItem[]> => {
52 5042 : const offset = loadMore ? historyOffset.value : 0
53 5042 : const history = await api.getHistory(BATCH_SIZE, {
54 : offset,
55 : throwOnError: true
56 : })
57 5012 : const newAssets = mapHistoryToAssets(history)
58 5042 : const items = loadMore ? [...historyAssets.value] : []
59 5042 : const loadedIds = new Set(items.map(({ id }) => id))
60 :
61 5042 : for (const asset of newAssets) {
62 3724 : if (loadedIds.has(asset.id)) continue
63 3724 : loadedIds.add(asset.id)
64 :
65 3724 : const assetTime = new Date(asset.created_at).getTime()
66 3724 : const insertIndex = items.findIndex(
67 : (item) => new Date(item.created_at).getTime() < assetTime
68 : )
69 3724 : if (insertIndex === -1) items.push(asset)
70 0 : else items.splice(insertIndex, 0, asset)
71 : }
72 :
73 5012 : historyOffset.value = offset + BATCH_SIZE
74 5012 : hasMoreHistory.value = history.length === BATCH_SIZE
75 5012 : return items.slice(0, MAX_HISTORY_ITEMS)
76 : }
77 :
78 2355 : const historyLoading = ref(false)
79 2355 : let historyQueue = Promise.resolve()
80 2355 : let refreshPromise: Promise<void> | undefined
81 2355 : let loadMorePromise: Promise<boolean> | undefined
82 2355 : let refreshRequest = 0
83 :
84 2355 : const doUpdateHistory = async () => {
85 2556 : historyLoading.value = true
86 2556 : try {
87 2556 : let refreshedRequest: number
88 2556 : do {
89 2687 : refreshedRequest = refreshRequest
90 2687 : try {
91 2687 : historyAssets.value = await fetchHistoryAssets(false)
92 : } catch (err) {
93 0 : reportError(err, {
94 : errorType: 'failure_loading_job_history',
95 : surface: 'assets'
96 : })
97 : }
98 : } while (refreshedRequest !== refreshRequest)
99 : } finally {
100 2526 : historyLoading.value = false
101 : }
102 : }
103 :
104 2355 : const updateHistory = () => {
105 2859 : refreshRequest++
106 2859 : if (!refreshPromise) {
107 2556 : refreshPromise = historyQueue.then(doUpdateHistory).finally(() => {
108 2527 : refreshPromise = undefined
109 : })
110 2556 : historyQueue = refreshPromise
111 : }
112 2859 : return refreshPromise
113 : }
114 :
115 2355 : const doLoadMoreHistory = async () => {
116 2355 : isLoadingMore.value = true
117 2355 : try {
118 2355 : historyAssets.value = await fetchHistoryAssets(true)
119 2355 : return true
120 : } catch (err) {
121 0 : reportError(err, {
122 : errorType: 'failure_loading_job_history',
123 : surface: 'assets'
124 : })
125 0 : return false
126 : } finally {
127 2355 : isLoadingMore.value = false
128 : }
129 : }
130 :
131 2355 : const loadMoreHistory = () => {
132 2355 : if (!loadMorePromise) {
133 2355 : const operation = historyQueue.then(() =>
134 : hasMoreHistory.value ? doLoadMoreHistory() : false
135 : )
136 2355 : loadMorePromise = operation.finally(() => {
137 2355 : loadMorePromise = undefined
138 : })
139 2355 : historyQueue = operation.then(() => undefined)
140 : }
141 2355 : return loadMorePromise
142 : }
143 :
144 2355 : const outputAssets = {
145 : hasMore: hasMoreHistory,
146 : invalidate: updateHistory,
147 : isLoading: computed(() => historyLoading.value || isLoadingMore.value),
148 : items: historyAssets,
149 : loadMore: loadMoreHistory,
150 : loadNew: updateHistory
151 : }
152 2355 : useEventListener(api, 'execution_success', updateHistory)
153 2355 : void loadMoreHistory()
154 2355 : return outputAssets
155 : }
156 : )
|