Skip to content

Commit 4b0a704

Browse files
author
sachin-maheshwari
authored
Merge pull request #222 from topcoder-platform/Issue_221
Issue 221
2 parents e372de7 + 59d78b1 commit 4b0a704

File tree

4 files changed

+66
-33
lines changed

4 files changed

+66
-33
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ To update the existing challengeId data on submissions in DynamoDB to v5 challen
180180
```bash
181181
SUBMISSION_TABLE_NAME // Table name of the submission records. Defaults to 'Submission'
182182
UPDATE_V5_CHALLENGE_BATCH_SIZE // Number of records that are updated simultaneously. Defaults to 250
183+
FETCH_CREATED_DATE_START // The start day of fetch latest challenges. Defaults to '2021-01-01'
184+
FETCH_PAGE_SIZE // The page size of each api request. Defaults to 500
183185
```
184186

185187

config/default.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ module.exports = {
3737
PAGE_SIZE: process.env.PAGE_SIZE || 20,
3838
MAX_PAGE_SIZE: parseInt(process.env.MAX_PAGE_SIZE) || 100,
3939
ES_BATCH_SIZE: process.env.ES_BATCH_SIZE || 250,
40-
UPDATE_V5_CHALLENGE_BATCH_SIZE: process.env.UPDATE_V5_CHALLENGE_BATCH_SIZE || 250,
40+
UPDATE_V5_CHALLENGE_BATCH_SIZE: process.env.UPDATE_V5_CHALLENGE_BATCH_SIZE || 100,
4141
SUBMISSION_TABLE_NAME: process.env.SUBMISSION_TABLE_NAME || 'Submission',
42-
AUTH0_PROXY_SERVER_URL: process.env.AUTH0_PROXY_SERVER_URL
42+
AUTH0_PROXY_SERVER_URL: process.env.AUTH0_PROXY_SERVER_URL,
43+
FETCH_CREATED_DATE_START: process.env.FETCH_CREATED_DATE_START || '2021-01-01',
44+
FETCH_PAGE_SIZE: process.env.FETCH_PAGE_SIZE || 500
4345
}

scripts/updateToV5ChallengeId.js

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,10 @@ const helper = require('../src/common/helper')
1313
* Update Submission's challenge id to v5
1414
* @param {Object} submission The submission record
1515
* @param {Array} failedContainer The failed records container
16+
* @param {String} v5challengeId The v5 challenge id
1617
* @returns {Promise}
1718
*/
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) {
2720
const record = {
2821
TableName: 'Submission',
2922
Key: {
@@ -35,13 +28,11 @@ function * updateRecord (submission, failedContainer) {
3528
':l': submission.challengeId
3629
}
3730
}
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 {
4432
yield dbhelper.updateRecord(record)
33+
} catch (err) {
34+
logger.error(`update submission record error: ${err.message}`)
35+
failedContainer.push(submission)
4536
}
4637
}
4738

@@ -53,23 +44,34 @@ function * updateRecords () {
5344
const tableName = config.SUBMISSION_TABLE_NAME
5445
const promises = []
5546
const failedRecords = []
56-
const params = {
57-
TableName: tableName
58-
}
5947
// 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
6760
}
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+
}
7375
}
7476
}
7577
logger.debug(`All records fetched. Proceeding to update them in batches of ${config.UPDATE_V5_CHALLENGE_BATCH_SIZE}`)

src/common/helper.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,32 @@ function adjustSubmissionChallengeId (submission) {
769769
}
770770
}
771771

772+
/**
773+
* Get all latest challenges
774+
* @param {Number} page page index
775+
* @returns {Array} an array of challenge
776+
*/
777+
function * getLatestChallenges (page) {
778+
page = page || 1
779+
const token = yield getM2Mtoken()
780+
const url = `${config.CHALLENGEAPI_V5_URL}?createdDateStart=${config.FETCH_CREATED_DATE_START}&page=${page}&perPage=${config.FETCH_PAGE_SIZE}&isLightweight=true`
781+
try {
782+
const response = yield request.get(url)
783+
.set('Authorization', `Bearer ${token}`)
784+
.set('Content-Type', 'application/json')
785+
const challenges = _.map(_.filter(_.get(response, 'body'), 'legacyId'), c => _.pick(c, 'id', 'legacyId'))
786+
logger.debug(`Fetched ${challenges.length} challenges in this iteration. More may follow...`)
787+
if (_.get(response, 'headers.x-total-pages') > page) {
788+
const leftChallenges = yield getLatestChallenges(page + 1)
789+
challenges.push(...leftChallenges)
790+
}
791+
return challenges
792+
} catch (err) {
793+
logger.error(`Error while accessing ${url}, message: ${err.message}`)
794+
return []
795+
}
796+
}
797+
772798
module.exports = {
773799
wrapExpress,
774800
autoWrapExpress,
@@ -786,5 +812,6 @@ module.exports = {
786812
cleanseReviews,
787813
getRoleIdToRoleNameMap,
788814
getV5ChallengeId,
789-
adjustSubmissionChallengeId
815+
adjustSubmissionChallengeId,
816+
getLatestChallenges
790817
}

0 commit comments

Comments
 (0)