Skip to content

Commit fe7e99e

Browse files
committed
Fixing the challenge ID to detect if it's a legacy ID and to convert it to a uuid, which the resource api needs.
1 parent de8871c commit fe7e99e

File tree

3 files changed

+87
-56
lines changed

3 files changed

+87
-56
lines changed

config/default.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ module.exports = {
3333
REGISTRANT_RESOURCE_ROLE_ID: process.env.REGISTRANT_RESOURCE_ROLE_ID || '732339e7-8e30-49d7-9198-cccf9451e221',
3434
SUBMISSIONS_API_URL: process.env.SUBMISSIONS_API_URL || 'http://localhost:4000/v5/submissions',
3535
RESOURCES_API_URL: process.env.RESOURCES_API_URL || 'http://localhost:4000/v5/resources',
36+
CHALLENGE_API_URL: process.env.RESOURCES_API_URL || 'http://localhost:4000/v5/challenges',
3637
CONTEST_SUBMISSION_TYPE: process.env.CONTEST_SUBMISSION_TYPE || 'Contest Submission',
3738
CHECKPOINT_SUBMISSION_TYPE: process.env.CHECKPOINT_SUBMISSION_TYPE || 'Checkpoint Submission',
3839

src/common/helper.js

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const AWS = require('aws-sdk')
66
const config = require('config')
77
const elasticsearch = require('elasticsearch')
88
const _ = require('lodash')
9+
const logger = require('./logger')
910
const m2mAuth = require('tc-core-library-js').auth.m2m
1011
const m2m = m2mAuth(_.pick(config, ['AUTH0_URL', 'AUTH0_AUDIENCE', 'TOKEN_CACHE_TIME', 'AUTH0_PROXY_SERVER_URL']))
1112
const superagent = require('superagent')
@@ -106,8 +107,52 @@ function getESClient () {
106107
return esClients['client']
107108
}
108109

110+
/**
111+
* Get v5 challenge uuid if the challenge id is legacyId format form
112+
* @param {String} challengeId Challenge ID
113+
* @returns {String} Legacy Challenge ID of the given challengeId
114+
*/
115+
async function getV5ChallengeId (challengeId) {
116+
if (/^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(challengeId)) {
117+
return challengeId
118+
} else {
119+
logger.debug(`${challengeId} detected as legacyId. Fetching challenge uuid`)
120+
try {
121+
const v5challenge = await getData(`${config.CHALLENGE_API_URL}?legacyId=${challengeId}`)
122+
logger.debug(`Legacy challenge id is ${v5challenge.id} for v5 challenge legacyId ${challengeId}`)
123+
return v5challenge.id
124+
} catch (err) {
125+
logger.error(`Error while accessing ${config.CHALLENGE_API_URL}?legacyId=${challengeId} - ${JSON.stringify(err)}`)
126+
throw err
127+
}
128+
}
129+
}
130+
131+
/**
132+
* Get legacy challenge id if the challenge id is uuid form
133+
* @param {String} challengeId Challenge ID
134+
* @returns {String} Legacy Challenge ID of the given challengeId
135+
*/
136+
async function getLegacyChallengeId (challengeId) {
137+
if (/^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(challengeId)) {
138+
logger.debug(`${challengeId} detected as uuid. Fetching legacy challenge id`)
139+
try {
140+
const v5challenge = await getData(`${config.CHALLENGE_API_URL}/${challengeId}`)
141+
const legacyId = parseInt(v5challenge.legacyId, 10)
142+
logger.debug(`Legacy challenge id is ${legacyId} for v5 challenge id ${challengeId}`)
143+
return legacyId
144+
} catch (err) {
145+
logger.error(`Error while accessing ${config.CHALLENGEAPI_V5_URL}/${challengeId}`)
146+
throw err
147+
}
148+
}
149+
return challengeId
150+
}
151+
109152
module.exports = {
110153
getData,
111154
getAllPagesData,
112-
getESClient
155+
getESClient,
156+
getV5ChallengeId,
157+
getLegacyChallengeId
113158
}

src/services/ProcessorService.js

Lines changed: 40 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -229,22 +229,25 @@ removeResource.schema = createResource.schema
229229
* @param {String} challengeId the challenge id
230230
*/
231231
async function updateSubmissionsData (challengeId) {
232+
// const v5challengeId = await helper.getV5ChallengeId(challengeId)
233+
const legacyId = await helper.getLegacyChallengeId(challengeId)
234+
logger.debug(`Update Submissions Data - Legacy ID ${legacyId}`)
232235
// get all challenge resources
233-
const resources = await helper.getData(config.RESOURCES_API_URL, { challengeId })
236+
// const resources = await helper.getData(config.RESOURCES_API_URL, { challengeId })
234237
// get all challenge submissions, all pages are retrieved
235-
const subs = await helper.getAllPagesData(config.SUBMISSIONS_API_URL, { challengeId })
238+
const subs = await helper.getAllPagesData(config.SUBMISSIONS_API_URL, { legacyId })
236239

237240
// function to find submitter handle by member id among challenge resources
238-
const getSubmitter = (memberId) => {
239-
const resource = _.find(resources, (r) => String(r.memberId) === String(memberId))
240-
if (!resource) {
241-
// there are some rare cases that submitter resource is not found,
242-
// e.g. user unregisters a challenge after uploading a submission,
243-
// in such cases, return null submitter instead of disabling the whole challenge submissions data
244-
return null
245-
}
246-
return resource.memberHandle
247-
}
241+
// const getSubmitter = (memberId) => {
242+
// const resource = _.find(resources, (r) => String(r.memberId) === String(memberId))
243+
// if (!resource) {
244+
// // there are some rare cases that submitter resource is not found,
245+
// // e.g. user unregisters a challenge after uploading a submission,
246+
// // in such cases, return null submitter instead of disabling the whole challenge submissions data
247+
// return null
248+
// }
249+
// return resource.memberHandle
250+
// }
248251

249252
// construct data
250253
const submissions = []
@@ -255,39 +258,40 @@ async function updateSubmissionsData (challengeId) {
255258
const submittersMap = {}
256259

257260
_.forEach(subs, (sub) => {
258-
let target
261+
// let target
259262
if (sub.type === config.CONTEST_SUBMISSION_TYPE) {
260-
target = submissions
263+
// target = submissions
261264
numOfSubmissions += 1
262265
// count number of submitters, only contest submissions are considered
263266
if (!submittersMap[sub.memberId]) {
264267
numOfRegistrants += 1
265268
submittersMap[sub.memberId] = true
266269
}
267270
} else if (sub.type === config.CHECKPOINT_SUBMISSION_TYPE) {
268-
target = checkpoints
271+
// target = checkpoints
269272
numOfCheckpointSubmissions += 1
270-
} else {
271-
// ignore the submission, it is not type of contest submission or checkpoint submission
272-
return
273273
}
274+
// } else {
275+
// ignore the submission, it is not type of contest submission or checkpoint submission
276+
// return
277+
// }
274278
// add submission to submissions or checkpoints
275-
const record = _.find(target, (item) => String(item.submitterId) === String(sub.memberId))
276-
if (record) {
277-
record.submissions.push({
278-
submissionId: sub.id,
279-
submissionTime: sub.created
280-
})
281-
} else {
282-
target.push({
283-
submitter: getSubmitter(sub.memberId),
284-
submitterId: sub.memberId,
285-
submissions: [{
286-
submissionId: sub.id,
287-
submissionTime: sub.created
288-
}]
289-
})
290-
}
279+
// const record = _.find(target, (item) => String(item.submitterId) === String(sub.memberId))
280+
// if (record) {
281+
// record.submissions.push({
282+
// submissionId: sub.id,
283+
// submissionTime: sub.created
284+
// })
285+
// } else {
286+
// target.push({
287+
// submitter: getSubmitter(sub.memberId),
288+
// submitterId: sub.memberId,
289+
// submissions: [{
290+
// submissionId: sub.id,
291+
// submissionTime: sub.created
292+
// }]
293+
// })
294+
// }
291295
})
292296

293297
// update challenge's submissions data, only update changed fields to improve performance
@@ -328,19 +332,7 @@ createSubmission.schema = {
328332
'mime-type': Joi.string().required(),
329333
payload: Joi.object().keys({
330334
resource: Joi.string().required(),
331-
id: Joi.string().uuid().required(),
332-
type: Joi.string().required(),
333-
fileType: Joi.string(),
334-
url: Joi.string().uri(),
335-
memberId: intOrUUID().required(),
336-
challengeId: intOrUUID().required(),
337-
legacySubmissionId: intOrUUID(),
338-
legacyUploadId: intOrUUID(),
339-
submissionPhaseId: intOrUUID(),
340-
created: Joi.date().required(),
341-
updated: Joi.date(),
342-
createdBy: Joi.string().required(),
343-
updatedBy: Joi.string()
335+
challengeId: intOrUUID().required()
344336
}).unknown(true).required()
345337
}).required()
346338
}
@@ -365,14 +357,7 @@ updateSubmission.schema = {
365357
'mime-type': Joi.string().required(),
366358
payload: Joi.object().keys({
367359
resource: Joi.string().required(),
368-
id: Joi.string().uuid().required(),
369-
type: Joi.string().required(),
370-
url: Joi.string().uri(),
371-
memberId: intOrUUID().required(),
372-
challengeId: intOrUUID().required(),
373-
legacySubmissionId: intOrUUID(),
374-
legacyUploadId: intOrUUID(),
375-
submissionPhaseId: intOrUUID()
360+
challengeId: intOrUUID().required()
376361
}).unknown(true).required()
377362
}).required()
378363
}

0 commit comments

Comments
 (0)