@@ -13,17 +13,10 @@ const helper = require('../src/common/helper')
13
13
* Update Submission's challenge id to v5
14
14
* @param {Object } submission The submission record
15
15
* @param {Array } failedContainer The failed records container
16
+ * @param {String } v5challengeId The v5 challenge id
16
17
* @returns {Promise }
17
18
*/
18
- function * updateRecord ( submission , failedContainer ) {
19
- let v5challengeId
20
- try {
21
- v5challengeId = yield helper . getV5ChallengeId ( submission . challengeId )
22
- } catch ( err ) {
23
- logger . error ( `fetching the details of the challenge(${ submission . challengeId } ) failed, ${ err . message } ` )
24
- failedContainer . push ( submission )
25
- return
26
- }
19
+ function * updateRecord ( submission , failedContainer , v5challengeId ) {
27
20
const record = {
28
21
TableName : 'Submission' ,
29
22
Key : {
@@ -35,13 +28,11 @@ function * updateRecord (submission, failedContainer) {
35
28
':l' : submission . challengeId
36
29
}
37
30
}
38
- if ( ! v5challengeId ) {
39
- logger . warn ( `the challengeId: ${ submission . challengeId } is not having a v5 challengeId` )
40
- failedContainer . push ( submission )
41
- } else if ( v5challengeId === submission . challengeId ) {
42
- logger . info ( `the challengeId: ${ submission . challengeId } is already a v5 challengeId` )
43
- } else {
31
+ try {
44
32
yield dbhelper . updateRecord ( record )
33
+ } catch ( err ) {
34
+ logger . error ( `update submission record error: ${ err . message } ` )
35
+ failedContainer . push ( submission )
45
36
}
46
37
}
47
38
@@ -53,23 +44,34 @@ function * updateRecords () {
53
44
const tableName = config . SUBMISSION_TABLE_NAME
54
45
const promises = [ ]
55
46
const failedRecords = [ ]
56
- const params = {
57
- TableName : tableName
58
- }
59
47
// Process until all the records from DB is fetched
60
- while ( true ) {
61
- const records = yield dbhelper . scanRecords ( params )
62
- const totalRecords = records . Items . length
63
- logger . debug ( `Number of ${ tableName } s fetched from DB - ${ totalRecords } . More fetch iterations may follow (pagination in progress)` )
64
- for ( let i = 0 ; i < totalRecords ; i ++ ) {
65
- const record = records . Items [ i ]
66
- promises . push ( updateRecord ( record , failedRecords ) )
48
+ const challengeIds = yield helper . getLatestChallenges ( )
49
+ logger . debug ( `Total number of challenges fetched from api - ${ challengeIds . length } .` )
50
+ const batchIds = _ . chunk ( challengeIds , config . UPDATE_V5_CHALLENGE_BATCH_SIZE )
51
+ for ( const cId of batchIds ) {
52
+ const queryParams = _ . fromPairs ( _ . map ( cId , ( c , i ) => [ `:challengeId${ i } ` , c . legacyId ] ) )
53
+ const params = {
54
+ TableName : tableName ,
55
+ FilterExpression : `#challengeId IN (${ _ . join ( _ . keys ( queryParams ) , ',' ) } )` ,
56
+ ExpressionAttributeNames : {
57
+ '#challengeId' : 'challengeId'
58
+ } ,
59
+ ExpressionAttributeValues : queryParams
67
60
}
68
- // Continue fetching the remaining records from Database
69
- if ( typeof records . LastEvaluatedKey !== 'undefined' ) {
70
- params . ExclusiveStartKey = records . LastEvaluatedKey
71
- } else {
72
- break // If there are no more records to process, exit the loop
61
+ while ( true ) {
62
+ const records = yield dbhelper . scanRecords ( params )
63
+ const totalRecords = records . Items . length
64
+ logger . debug ( `Number of ${ tableName } s fetched from DB - ${ totalRecords } . More fetch iterations may follow (pagination in progress)` )
65
+ for ( let i = 0 ; i < totalRecords ; i ++ ) {
66
+ const record = records . Items [ i ]
67
+ promises . push ( updateRecord ( record , failedRecords , _ . find ( cId , [ 'legacyId' , record . challengeId ] ) . id ) )
68
+ }
69
+ // Continue fetching the remaining records from Database
70
+ if ( typeof records . LastEvaluatedKey !== 'undefined' ) {
71
+ params . ExclusiveStartKey = records . LastEvaluatedKey
72
+ } else {
73
+ break // If there are no more records to process, exit the loop
74
+ }
73
75
}
74
76
}
75
77
logger . debug ( `All records fetched. Proceeding to update them in batches of ${ config . UPDATE_V5_CHALLENGE_BATCH_SIZE } ` )
0 commit comments