11require ( 'dotenv' ) . config ( ) ;
22const { MongoClient } = require ( 'mongodb' ) ;
33
4+ /**
5+ * @param db - mongo db instance
6+ * @param collectionName - name of the collection to be updated
7+ */
48async function movePayloadTimestampToEventLevel ( db , collectionName ) {
59 const collection = db . collection ( collectionName ) ;
610 const cursor = collection . find ( { 'payload.timestamp' : { $exists : true } } ) . batchSize ( 500 ) ;
@@ -11,7 +15,10 @@ async function movePayloadTimestampToEventLevel(db, collectionName) {
1115
1216 for await ( const doc of cursor ) {
1317 const timestamp = Number ( doc . payload ?. timestamp ) ;
14- if ( isNaN ( timestamp ) ) continue ;
18+
19+ if ( isNaN ( timestamp ) ) {
20+ continue ;
21+ }
1522
1623 bulkOps . push ( {
1724 updateOne : {
@@ -25,6 +32,7 @@ async function movePayloadTimestampToEventLevel(db, collectionName) {
2532
2633 if ( bulkOps . length === 1000 ) {
2734 const result = await collection . bulkWrite ( bulkOps ) ;
35+
2836 updated += result . modifiedCount ;
2937 processed += bulkOps . length ;
3038 console . log ( ` Flushed 1000 updates (${ processed } processed, ${ updated } updated)` ) ;
@@ -34,6 +42,7 @@ async function movePayloadTimestampToEventLevel(db, collectionName) {
3442
3543 if ( bulkOps . length > 0 ) {
3644 const result = await collection . bulkWrite ( bulkOps ) ;
45+
3746 updated += result . modifiedCount ;
3847 processed += bulkOps . length ;
3948 console . log ( ` Flushed final ${ bulkOps . length } updates (${ processed } processed, ${ updated } updated)` ) ;
@@ -42,6 +51,11 @@ async function movePayloadTimestampToEventLevel(db, collectionName) {
4251 console . log ( ` Done with ${ collectionName } : ${ updated } documents updated` ) ;
4352}
4453
54+ /**
55+ * @param db - mongo db instance
56+ * @param repetitionCollectionName - repetitions collection to be updated
57+ * @param projectId - project id of current repetitions collection
58+ */
4559async function backfillTimestampsFromEvents ( db , repetitionCollectionName , projectId ) {
4660 const collection = db . collection ( repetitionCollectionName ) ;
4761
@@ -64,7 +78,12 @@ async function backfillTimestampsFromEvents(db, repetitionCollectionName, projec
6478 as : 'eventData' ,
6579 } ,
6680 } ,
67- { $unwind : { path : '$eventData' , preserveNullAndEmptyArrays : true } } ,
81+ {
82+ $unwind : {
83+ path : '$eventData' ,
84+ preserveNullAndEmptyArrays : true ,
85+ } ,
86+ } ,
6887 { $match : { 'eventData.timestamp' : { $exists : true } } } ,
6988 {
7089 $project : {
@@ -78,7 +97,6 @@ async function backfillTimestampsFromEvents(db, repetitionCollectionName, projec
7897
7998 let bulkOps = [ ] ;
8099 let processed = 0 ;
81- let flushed = 0 ;
82100
83101 for await ( const doc of cursor ) {
84102 bulkOps . push ( {
@@ -90,27 +108,30 @@ async function backfillTimestampsFromEvents(db, repetitionCollectionName, projec
90108
91109 if ( bulkOps . length === 1000 ) {
92110 const res = await collection . bulkWrite ( bulkOps ) ;
111+
93112 processed += res . modifiedCount ;
94- flushed ++ ;
95113 bulkOps = [ ] ;
96114 }
97115 }
98116
99117 if ( bulkOps . length > 0 ) {
100118 const res = await collection . bulkWrite ( bulkOps ) ;
119+
101120 processed += res . modifiedCount ;
102- flushed ++ ;
103121 }
104122
105123 console . log ( ` Done backfilling ${ repetitionCollectionName } : ${ processed } updated` ) ;
106124}
107125
126+ /**
127+ * Method that runs convertor script
128+ */
108129async function run ( ) {
109130 const fullUri = process . env . MONGO_EVENTS_DATABASE_URI ;
110131
111132 // Parse the Mongo URL manually
112133 const mongoUrl = new URL ( fullUri ) ;
113- const databaseName = " hawk_events"
134+ const databaseName = ' hawk_events' ;
114135
115136 // Extract query parameters
116137 const queryParams = Object . fromEntries ( mongoUrl . searchParams . entries ( ) ) ;
@@ -136,9 +157,9 @@ async function run() {
136157
137158 const client = new MongoClient ( cleanUri , options ) ;
138159
139-
140160 await client . connect ( ) ;
141161 const db = client . db ( databaseName ) ;
162+
142163 console . log ( `Connected to database: ${ databaseName } ` ) ;
143164
144165 const collections = await db . listCollections ( { } , {
@@ -154,6 +175,7 @@ async function run() {
154175
155176 // Convert events
156177 let i = 1 ;
178+
157179 for ( const collectionName of eventCollections ) {
158180 console . log ( `[${ i } /${ eventCollections . length } ] Processing ${ collectionName } ` ) ;
159181 await movePayloadTimestampToEventLevel ( db , collectionName ) ;
0 commit comments