|
| 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