Skip to content

Commit 11f64d0

Browse files
author
sachin-maheshwari
authored
Merge pull request #224 from topcoder-platform/Issue_223
Issue 223
2 parents 4b0a704 + b0328d9 commit 11f64d0

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"init-es": "node scripts/loadES.js",
1616
"db-to-es": "node scripts/migrateFromDBToES.js",
1717
"update-to-v5-challengeId": "node scripts/updateToV5ChallengeId.js",
18+
"update-to-v5-challengeId-v2": "node scripts/updateToV5ChallengeIdV2.js",
1819
"test": "mocha test/unit/*.test.js --require test/unit/prepare.js --exit",
1920
"e2e": "mocha test/e2e/*.test.js --require test/e2e/prepare.js --exit",
2021
"cov": "nyc --reporter=html --reporter=text mocha test/unit/*.test.js --require test/unit/prepare.js --exit",

scripts/updateToV5ChallengeIdV2.js

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/**
2+
* Store v5 challenge id for current records
3+
*/
4+
5+
const _ = require('lodash')
6+
const co = require('co')
7+
const config = require('config')
8+
const logger = require('../src/common/logger')
9+
const dbhelper = require('../src/common/dbhelper')
10+
const helper = require('../src/common/helper')
11+
12+
/**
13+
* Update Submission's challenge id to v5
14+
* @param {Object} submission The submission record
15+
* @param {Array} failedContainer The failed records container
16+
* @param {String} v5challengeId The v5 challenge id
17+
* @returns {Promise}
18+
*/
19+
function * updateRecord (submission, failedContainer, v5challengeId) {
20+
const record = {
21+
TableName: 'Submission',
22+
Key: {
23+
id: submission.id
24+
},
25+
UpdateExpression: `set challengeId = :c, legacyChallengeId = :l`,
26+
ExpressionAttributeValues: {
27+
':c': v5challengeId,
28+
':l': submission.challengeId
29+
}
30+
}
31+
try {
32+
yield dbhelper.updateRecord(record)
33+
} catch (err) {
34+
logger.error(`update submission record error: ${err.message}`)
35+
failedContainer.push(submission)
36+
}
37+
}
38+
39+
/*
40+
* Update all submission's challenge id to v5
41+
* @returns {Promise}
42+
*/
43+
function * updateRecords () {
44+
const tableName = config.SUBMISSION_TABLE_NAME
45+
const promises = []
46+
const failedRecords = []
47+
// Process until all the records from DB is fetched
48+
const challengeIds = yield helper.getLatestChallenges()
49+
logger.debug(`Number of challenges fetched from api - ${challengeIds.length}.`)
50+
const params = {
51+
TableName: tableName
52+
}
53+
while (true) {
54+
const records = yield dbhelper.scanRecords(params)
55+
const totalRecords = records.Items.length
56+
logger.debug(`Number of ${tableName}s fetched from DB - ${totalRecords}. More fetch iterations may follow (pagination in progress)`)
57+
for (let i = 0; i < totalRecords; i++) {
58+
const record = records.Items[i]
59+
const v5ChallengeId = _.get(_.find(challengeIds, ['legacyId', record.challengeId]), 'id')
60+
if (v5ChallengeId) {
61+
promises.push(updateRecord(record, failedRecords, v5ChallengeId))
62+
}
63+
}
64+
// Continue fetching the remaining records from Database
65+
if (typeof records.LastEvaluatedKey !== 'undefined') {
66+
params.ExclusiveStartKey = records.LastEvaluatedKey
67+
} else {
68+
break // If there are no more records to process, exit the loop
69+
}
70+
}
71+
logger.debug(`All records fetched. Proceeding to update them in batches of ${config.UPDATE_V5_CHALLENGE_BATCH_SIZE}`)
72+
const paraRecords = _.chunk(promises, config.UPDATE_V5_CHALLENGE_BATCH_SIZE)
73+
for (const rs of paraRecords) {
74+
yield rs
75+
}
76+
logger.info(`Processed ${promises.length - failedRecords.length} records successfully`)
77+
if (failedRecords.length > 0) {
78+
logger.warn(`Processing of ${failedRecords.length} records failed`)
79+
logger.info(`Failed records: ${_.join(_.map(failedRecords, f => JSON.stringify(_.pick(f, ['id', 'challengeId'])), ','))}`)
80+
}
81+
}
82+
83+
co(function * () {
84+
yield updateRecords()
85+
}).catch((err) => {
86+
logger.logFullError(err)
87+
})

0 commit comments

Comments
 (0)