Skip to content

Commit 11b1f89

Browse files
authored
Adding paggination and logs for supporting doc migrations (#4494)
1 parent b8d5385 commit 11b1f89

File tree

2 files changed

+82
-31
lines changed

2 files changed

+82
-31
lines changed

packages/migration/migrations/hearth/20221017133354-restore-supporting-document.js

Lines changed: 62 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,40 +9,75 @@
99
* Copyright (C) The OpenCRVS Authors. OpenCRVS and the OpenCRVS
1010
* graphic logo are (registered/a) trademark(s) of Plan International.
1111
*/
12-
import { getBirthEncounterCompositionCursor } from './../../utils/hearth-helper.js'
12+
import {
13+
getBirthEncounterCompositionCursor,
14+
getBirthEncounterCompositionCount
15+
// eslint-disable-next-line import/no-relative-parent-imports
16+
} from './../../utils/hearth-helper.js'
1317

1418
export const up = async (db, client) => {
1519
const session = client.startSession()
20+
const limit = 50
21+
let skip = 0
22+
let processedDocCount = 0
1623
try {
1724
await session.withTransaction(async () => {
18-
const compositionCursor = await getBirthEncounterCompositionCursor(db)
19-
while (await compositionCursor.hasNext()) {
20-
const composition = await compositionCursor.next()
21-
const compositionHistory = await db
22-
.collection('Composition_history')
23-
.find({
24-
id: composition.id
25-
})
26-
.toArray()
27-
compositionHistory.push(composition)
28-
const correctionIndex = compositionHistory.findIndex((composition) => {
29-
return composition.section.find(
30-
(section) =>
31-
section.code.coding[0].code === 'birth-correction-encounters'
32-
)
33-
})
34-
const immediatePrevComp = compositionHistory[correctionIndex - 1]
35-
const hasDocumentSection = immediatePrevComp?.section.find(
36-
(section) => section.code.coding[0].code === 'supporting-documents'
25+
const totalCompositionCount = await getBirthEncounterCompositionCount(db)
26+
while (totalCompositionCount > processedDocCount) {
27+
const compositionCursor = await getBirthEncounterCompositionCursor(
28+
db,
29+
limit,
30+
skip
31+
)
32+
const count = await compositionCursor.count()
33+
// eslint-disable-next-line no-console
34+
console.log(
35+
`Migration - SupportingDocuments :: Processing ${
36+
processedDocCount + 1
37+
} - ${
38+
processedDocCount + count
39+
} of ${totalCompositionCount} compositions...`
3740
)
38-
if (hasDocumentSection) {
39-
await db
40-
.collection('Composition')
41-
.updateOne(
42-
{ id: compositionHistory[correctionIndex].id },
43-
{ $push: { section: hasDocumentSection } }
44-
)
41+
while (await compositionCursor.hasNext()) {
42+
const composition = await compositionCursor.next()
43+
const compositionHistory = await db
44+
.collection('Composition_history')
45+
.find({
46+
id: composition.id
47+
})
48+
.toArray()
49+
compositionHistory.push(composition)
50+
const correctionIndex = compositionHistory.findIndex(
51+
(composition) => {
52+
return composition.section.find(
53+
(section) =>
54+
section.code.coding[0].code === 'birth-correction-encounters'
55+
)
56+
}
57+
)
58+
const immediatePrevComp = compositionHistory[correctionIndex - 1]
59+
const hasDocumentSection = immediatePrevComp?.section.find(
60+
(section) => section.code.coding[0].code === 'supporting-documents'
61+
)
62+
if (hasDocumentSection) {
63+
await db
64+
.collection('Composition')
65+
.updateOne(
66+
{ id: compositionHistory[correctionIndex].id },
67+
{ $push: { section: hasDocumentSection } }
68+
)
69+
}
4570
}
71+
skip += limit
72+
processedDocCount += count
73+
const percentage = (
74+
(processedDocCount / totalCompositionCount) *
75+
100
76+
).toFixed(2)
77+
// eslint-disable-next-line no-console
78+
console.log(
79+
`Migration - SupportingDocuments :: Processing done ${percentage}%`
80+
)
4681
}
4782
})
4883
} finally {

packages/migration/utils/hearth-helper.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,26 @@ export const COLLECTION_NAMES = {
2020
TASK: 'Task'
2121
}
2222

23-
export async function getBirthEncounterCompositionCursor(db) {
24-
return db.collection(COLLECTION_NAMES.COMPOSITION).find({
25-
'section.code.coding': { $elemMatch: { code: 'birth-encounter' } }
26-
})
23+
export async function getBirthEncounterCompositionCursor(
24+
db,
25+
limit = 50,
26+
skip = 0
27+
) {
28+
return db.collection(COLLECTION_NAMES.COMPOSITION).find(
29+
{
30+
'section.code.coding': { $elemMatch: { code: 'birth-encounter' } }
31+
},
32+
{ limit, skip }
33+
)
34+
}
35+
36+
export async function getBirthEncounterCompositionCount(db) {
37+
return db
38+
.collection(COLLECTION_NAMES.COMPOSITION)
39+
.find({
40+
'section.code.coding': { $elemMatch: { code: 'birth-encounter' } }
41+
})
42+
.count()
2743
}
2844

2945
export async function getCompositionCursor(db, limit = 50, skip = 0) {

0 commit comments

Comments
 (0)