Skip to content

Commit f2a1f2a

Browse files
committed
wip
1 parent 68e25cf commit f2a1f2a

File tree

3 files changed

+45
-37
lines changed

3 files changed

+45
-37
lines changed

packages/job-worker/src/ingest/model/implementation/ExpectedPackagesStore.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,9 @@ export class ExpectedPackagesStore<TPackageSource extends { fromPieceType: Expec
110110
rundownId,
111111
partId,
112112
})
113-
setValuesAndTrackChangesFunc(this.#expectedPackagesWithChanges, this.#expectedPackages, (pkg) => {
114-
let changed = false
115-
116-
for (const source of pkg.ingestSources) {
117-
const sourceChanged = updatePackageSource(source)
118-
changed = changed || sourceChanged
119-
}
120-
121-
return changed
122-
})
113+
setValuesAndTrackChangesFunc(this.#expectedPackagesWithChanges, this.#expectedPackages, (pkg) =>
114+
updatePackageSource(pkg.source)
115+
)
123116
}
124117

125118
compareToPreviousData(oldStore: ExpectedPackagesStore<TPackageSource>): void {

packages/job-worker/src/ingest/model/implementation/IngestModelImpl.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -341,14 +341,14 @@ export class IngestModelImpl implements IngestModel, IngestDatabasePersistedMode
341341
findExpectedPackageIngestSources(packageId: ExpectedPackageId): ReadonlyDeep<ExpectedPackageIngestSource>[] {
342342
const sources: ReadonlyDeep<ExpectedPackageIngestSource>[] = []
343343

344-
const baselinePackage = this.#rundownBaselineExpectedPackagesStore.expectedPackages.find(
345-
(pkg) => pkg._id === packageId
346-
)
347-
if (baselinePackage) sources.push(...baselinePackage.ingestSources)
344+
for (const baselinePackage of this.#rundownBaselineExpectedPackagesStore.expectedPackages) {
345+
if (baselinePackage._id === packageId) sources.push(baselinePackage.source)
346+
}
348347

349348
for (const part of this.getAllOrderedParts()) {
350-
const partPackage = part.expectedPackages.find((pkg) => pkg._id === packageId)
351-
if (partPackage) sources.push(...partPackage.ingestSources)
349+
for (const partPackage of part.expectedPackages) {
350+
if (partPackage._id === packageId) sources.push(partPackage.source)
351+
}
352352
}
353353

354354
return sources

packages/job-worker/src/ingest/model/implementation/SaveIngestModel.ts

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import { AdLibAction } from '@sofie-automation/corelib/dist/dataModel/AdlibAction'
22
import { AdLibPiece } from '@sofie-automation/corelib/dist/dataModel/AdLibPiece'
33
import { ExpectedMediaItem } from '@sofie-automation/corelib/dist/dataModel/ExpectedMediaItem'
4-
import { ExpectedPackageDB, ExpectedPackageDBType } from '@sofie-automation/corelib/dist/dataModel/ExpectedPackages'
4+
import {
5+
ExpectedPackageDB,
6+
ExpectedPackageDBType,
7+
getExpectedPackageIdNew,
8+
} from '@sofie-automation/corelib/dist/dataModel/ExpectedPackages'
59
import { ExpectedPlayoutItem } from '@sofie-automation/corelib/dist/dataModel/ExpectedPlayoutItem'
610
import { DBPart } from '@sofie-automation/corelib/dist/dataModel/Part'
711
import { Piece } from '@sofie-automation/corelib/dist/dataModel/Piece'
@@ -15,7 +19,7 @@ import { ProtectedString } from '@sofie-automation/corelib/dist/protectedString'
1519
import { IngestExpectedPackage } from '../IngestExpectedPackage'
1620
import { AnyBulkWriteOperation } from 'mongodb'
1721
import { ExpectedPackageId, RundownId } from '@sofie-automation/corelib/dist/dataModel/Ids'
18-
import { Complete, normalizeArrayToMap } from '@sofie-automation/corelib/dist/lib'
22+
import { normalizeArrayToMap } from '@sofie-automation/corelib/dist/lib'
1923

2024
export class SaveIngestModelHelper {
2125
readonly #rundownId: RundownId
@@ -87,6 +91,8 @@ export class SaveIngestModelHelper {
8791
writeExpectedPackagesChangesForRundown(
8892
context,
8993
this.#rundownId,
94+
// This isn't using the expectedPackages store fully, because we know the content of the documents is immutable
95+
// But it is easier to keep using the store for the sake of consistency
9096
Array.from(this.#expectedPackages.getDocumentsToSave().values())
9197
),
9298
context.directCollections.ExpectedPlayoutItems.bulkWrite(this.#expectedPlayoutItems.generateWriteOps()),
@@ -121,53 +127,62 @@ async function writeExpectedPackagesChangesForRundown(
121127
)) as Pick<ExpectedPackageDB, '_id' | 'created'>[]
122128
const existingDocsMap = normalizeArrayToMap(existingDocs, '_id')
123129

124-
// Generate any insert and update operations
125-
const ops: AnyBulkWriteOperation<ExpectedPackageDB>[] = []
130+
const packagesToSave = new Map<ExpectedPackageId, ExpectedPackageDB>()
126131
for (const doc of documentsToSave) {
127-
const newDbDoc: Complete<Omit<ExpectedPackageDB, '_id'>> = {
132+
const packageId = getExpectedPackageIdNew(context.studioId, doc.package)
133+
const partialDoc = packagesToSave.get(packageId)
134+
135+
if (partialDoc) {
136+
// Add the source to the existing document
137+
partialDoc.ingestSources.push(doc.source)
138+
139+
// TODO - should we check for duplicates entries?
140+
} else {
141+
// Add a new document
128142
// Future: omit 'playoutSources from this doc
129-
studioId: context.studioId,
130-
rundownId: rundownId,
131-
bucketId: null,
132-
created: Date.now(),
133-
package: doc.package,
134-
ingestSources: doc.ingestSources,
143+
packagesToSave.set(packageId, {
144+
_id: packageId,
145+
studioId: context.studioId,
146+
rundownId: rundownId,
147+
bucketId: null,
148+
created: Date.now(),
149+
package: doc.package,
150+
ingestSources: [doc.source],
151+
})
135152
}
153+
}
136154

155+
// Generate any insert and update operations
156+
const ops: AnyBulkWriteOperation<ExpectedPackageDB>[] = []
157+
for (const doc of packagesToSave.values()) {
137158
const existingDoc = existingDocsMap.get(doc._id)
138159
if (existingDoc) {
139160
// Document already exists, perform an update to preserve other fields
140161
ops.push({
141162
updateOne: {
142163
filter: { _id: doc._id },
143164
update: {
144-
$set: {
145-
// Update every field that we want to define
146-
...newDbDoc,
147-
},
165+
// Update every field that we want to define
166+
$set: doc,
148167
},
149168
},
150169
})
151170
} else {
152171
// Insert this new document
153172
ops.push({
154173
insertOne: {
155-
document: {
156-
...newDbDoc,
157-
_id: doc._id,
158-
},
174+
document: doc,
159175
},
160176
})
161177
}
162178
}
163179

164180
// Look over the existing documents, and see is no longer referenced
165-
const documentsToSaveMap = normalizeArrayToMap(documentsToSave, '_id')
166181
const idsToDelete: ExpectedPackageId[] = []
167182

168183
for (const doc of existingDocs) {
169184
// Skip if this document is in the list of documents to save
170-
if (documentsToSaveMap.has(doc._id)) continue
185+
if (packagesToSave.has(doc._id)) continue
171186

172187
// Future: check for playoutSources
173188
idsToDelete.push(doc._id)

0 commit comments

Comments
 (0)