@@ -16,7 +16,12 @@ async function movePayloadTimestampToEventLevel(db, collectionName) {
1616
1717 const docsToUpdate = collection . find (
1818 { timestamp : { $exists : false } } ,
19- { projection : { _id : 1 , 'payload.timestamp' : 1 } }
19+ {
20+ projection : {
21+ _id : 1 ,
22+ 'payload.timestamp' : 1 ,
23+ } ,
24+ }
2025 ) . limit ( documentsSelectionLimit ) ;
2126
2227 const batchedOps = [ ] ;
@@ -34,11 +39,11 @@ async function movePayloadTimestampToEventLevel(db, collectionName) {
3439 updateOne : {
3540 filter : { _id : doc . _id } ,
3641 update : {
37- $set : { timestamp : Number ( doc . payload . timestamp ) } ,
38- $unset : { 'payload.timestamp' : '' } ,
39- }
40- }
41- } )
42+ $set : { timestamp : Number ( doc . payload . timestamp ) } ,
43+ $unset : { 'payload.timestamp' : '' } ,
44+ } ,
45+ } ,
46+ } ) ;
4247
4348 currentCount ++ ;
4449 }
@@ -47,7 +52,7 @@ async function movePayloadTimestampToEventLevel(db, collectionName) {
4752 await collection . bulkWrite ( batchedOps ) ;
4853 }
4954
50- return currentCount
55+ return currentCount ;
5156}
5257/**
5358 * @param db - mongo db instance
@@ -58,15 +63,21 @@ async function backfillTimestampsFromEvents(db, repetitionCollectionName, projec
5863 const repetitions = db . collection ( repetitionCollectionName ) ;
5964 const events = db . collection ( `events:${ projectId } ` ) ;
6065
61- let bulkOps = [ ] ;
66+ const bulkOps = [ ] ;
6267 let repetitionCount = 1 ;
6368
6469 const repetitionsList = await repetitions . find (
6570 {
6671 timestamp : { $exists : false } ,
6772 } ,
68- { projection : { _id : 1 , groupHash : 1 } }
69- ) . limit ( documentsSelectionLimit ) . toArray ( ) ;
73+ {
74+ projection : {
75+ _id : 1 ,
76+ groupHash : 1 ,
77+ } ,
78+ }
79+ ) . limit ( documentsSelectionLimit )
80+ . toArray ( ) ;
7081
7182 const groupHashList = [ ] ;
7283
@@ -78,24 +89,29 @@ async function backfillTimestampsFromEvents(db, repetitionCollectionName, projec
7889
7990 const relatedEvents = await events . find (
8091 { groupHash : { $in : groupHashList } } ,
81- { projection : { timestamp : 1 , groupHash : 1 } }
92+ {
93+ projection : {
94+ timestamp : 1 ,
95+ groupHash : 1 ,
96+ } ,
97+ }
8298 ) . toArray ( ) ;
8399
84- const relatedEventsMap = new Map ( )
100+ const relatedEventsMap = new Map ( ) ;
85101
86102 relatedEvents . forEach ( e => {
87103 relatedEventsMap . set ( e . groupHash , e ) ;
88- } )
104+ } ) ;
89105
90106 for ( const repetition of repetitionsList ) {
91107 const relatedEvent = relatedEventsMap . get ( repetition . groupHash ) ;
92108
93109 if ( ! relatedEvent ) {
94110 bulkOps . push ( {
95111 deleteOne : {
96- filter : { _id : repetition . _id }
97- }
98- } )
112+ filter : { _id : repetition . _id } ,
113+ } ,
114+ } ) ;
99115 } else if ( relatedEvent ?. timestamp !== null ) {
100116 bulkOps . push ( {
101117 updateOne : {
@@ -112,11 +128,12 @@ async function backfillTimestampsFromEvents(db, repetitionCollectionName, projec
112128 const result = await repetitions . bulkWrite ( bulkOps ) ;
113129 const updated = result . modifiedCount ;
114130 const deleted = result . deletedCount ;
131+
115132 processed = bulkOps . length ;
116133 console . log ( ` updates (${ processed } processed, ${ updated } updated, ${ deleted } deleted)` ) ;
117134
118135 if ( updated + deleted === 0 ) {
119- repetitionCollectionsToCheck . filter ( collection => collection !== repetition )
136+ repetitionCollectionsToCheck . filter ( collection => collection !== repetition ) ;
120137 }
121138 }
122139
@@ -175,13 +192,13 @@ async function run() {
175192
176193 // Convert events
177194 let i = 1 ;
178- let documentsUpdatedCount = 1
195+ let documentsUpdatedCount = 1 ;
179196
180197 while ( documentsUpdatedCount != 0 ) {
181198 documentsUpdatedCount = 0 ;
182199 i = 1 ;
183200 const collectionsToUpdateCount = eventCollectionsToCheck . length ;
184-
201+
185202 for ( const collectionName of eventCollectionsToCheck ) {
186203 console . log ( `[${ i } /${ collectionsToUpdateCount } ] Processing ${ collectionName } ` ) ;
187204 const updated = await movePayloadTimestampToEventLevel ( db , collectionName ) ;
@@ -190,10 +207,10 @@ async function run() {
190207 eventCollectionsToCheck = eventCollectionsToCheck . filter ( collection => collection !== collectionName ) ;
191208 }
192209
193- documentsUpdatedCount += updated
210+ documentsUpdatedCount += updated ;
194211 i ++ ;
195212 }
196- }
213+ }
197214
198215 // Convert repetitions + backfill from events
199216 documentsUpdatedCount = 1 ;
0 commit comments