Line data Source code
1 : import { getOutputAssetMetadata } from '@/platform/assets/schemas/assetMetadataSchema'
2 : import type { AssetItem } from '@/platform/assets/schemas/assetSchema'
3 : import { getAssetType } from '@/platform/assets/utils/assetTypeUtil'
4 : import { getOutputKey } from '@/platform/assets/utils/outputKeyUtil'
5 :
6 : type SelectedJobOutputs = {
7 : expectedCount: number
8 : outputKeys: Set<string>
9 : }
10 :
11 1628 : export function getAssetOutputCount(
12 : asset: Pick<AssetItem, 'user_metadata'>
13 : ): number {
14 1628 : const count = asset.user_metadata?.outputCount
15 1628 : return typeof count === 'number' && count > 0 ? count : 1
16 : }
17 :
18 : /**
19 : * Counts the unique outputs a selection stands for.
20 : *
21 : * An expanded output stack puts a job-level parent and its children in the
22 : * same selection. The parent stands for every output of its job
23 : * (`outputCount`); each child stands for exactly one, and the child set never
24 : * includes the parent's own output. Summing them counts the job twice, so
25 : * outputs are grouped by job and each job contributes the larger of its
26 : * parent's `outputCount` and the number of distinct output identities
27 : * selected from it — the first covers a job-level selection whose children
28 : * were never expanded, the second a child-only selection.
29 : *
30 : * Assets that are not job outputs, and outputs whose metadata names no job,
31 : * count once each under their own id.
32 : */
33 76 : export function getTotalAssetOutputCount(assets: readonly AssetItem[]): number {
34 76 : const individualCounts = new Map<string, number>()
35 76 : const outputsByJobId = new Map<string, SelectedJobOutputs>()
36 :
37 76 : for (const asset of assets) {
38 109 : const assetType = getAssetType(asset)
39 109 : const isJobOutput = assetType === 'output' || assetType === 'temp'
40 109 : const metadata = isJobOutput
41 : ? getOutputAssetMetadata(asset.user_metadata)
42 : : null
43 :
44 109 : if (!metadata) {
45 1 : individualCounts.set(
46 : asset.id,
47 : isJobOutput ? getAssetOutputCount(asset) : 1
48 : )
49 1 : continue
50 : }
51 :
52 108 : let selectedOutputs = outputsByJobId.get(metadata.jobId)
53 108 : if (!selectedOutputs) {
54 108 : selectedOutputs = { expectedCount: 0, outputKeys: new Set<string>() }
55 108 : outputsByJobId.set(metadata.jobId, selectedOutputs)
56 : }
57 :
58 108 : selectedOutputs.expectedCount = Math.max(
59 : selectedOutputs.expectedCount,
60 : getAssetOutputCount(asset)
61 : )
62 109 : selectedOutputs.outputKeys.add(
63 : getOutputKey({
64 : nodeId: metadata.nodeId,
65 : subfolder: metadata.subfolder,
66 : filename: asset.name
67 : }) ?? `asset:${asset.id}`
68 : )
69 : }
70 :
71 76 : let total = 0
72 76 : for (const count of individualCounts.values()) total += count
73 76 : for (const { expectedCount, outputKeys } of outputsByJobId.values()) {
74 108 : total += Math.max(expectedCount, outputKeys.size)
75 : }
76 76 : return total
77 : }
|