Skip to content

Commit 62d1260

Browse files
Performance improvement: reduce number of API calls to challenge API
1 parent d267b37 commit 62d1260

File tree

2 files changed

+14
-40
lines changed

2 files changed

+14
-40
lines changed

src/common/helper.js

Lines changed: 10 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -363,30 +363,15 @@ function * getV5ChallengeId (challengeId) {
363363
return challengeId
364364
}
365365

366-
/*
366+
/**
367367
* Get submission phase ID of a challenge from Challenge API
368-
* @param challengeId Challenge ID
368+
* @param challenge Challenge
369369
* @returns {Integer} Submission phase ID of the given challengeId
370370
*/
371-
function * getSubmissionPhaseId (challengeId) {
371+
function getSubmissionPhaseId (challenge) {
372372
let phaseId = null
373-
let response
374-
challengeId = yield getV5ChallengeId(challengeId)
375-
376-
try {
377-
logger.info(`Calling to challenge API to find submission phase Id for ${challengeId}`)
378-
const token = yield getM2Mtoken()
379-
response = yield request.get(`${config.CHALLENGEAPI_V5_URL}/${challengeId}`)
380-
.set('Authorization', `Bearer ${token}`)
381-
.set('Content-Type', 'application/json')
382-
logger.info(`returned from finding submission phase Id for ${challengeId}`)
383-
} catch (ex) {
384-
logger.error(`Error while accessing ${config.CHALLENGEAPI_V5_URL}/${challengeId}`)
385-
logger.debug('Setting submissionPhaseId to Null')
386-
response = null
387-
}
388-
if (response) {
389-
const phases = _.get(response.body, 'phases', [])
373+
if (challenge) {
374+
const phases = _.get(challenge, 'phases', [])
390375
const checkPoint = _.filter(phases, { name: 'Checkpoint Submission', isOpen: true })
391376
const submissionPh = _.filter(phases, { name: 'Submission', isOpen: true })
392377
const finalFixPh = _.filter(phases, { name: 'Final Fix', isOpen: true })
@@ -404,14 +389,14 @@ function * getSubmissionPhaseId (challengeId) {
404389
return phaseId
405390
}
406391

407-
/*
392+
/**
408393
* Function to check user access to create a submission
409394
* @param authUser Authenticated user
410395
* @param subEntity Submission Entity
396+
* @param challengeDetails Challenge
411397
* @returns {Promise}
412398
*/
413-
function * checkCreateAccess (authUser, subEntity) {
414-
let challengeDetails
399+
function * checkCreateAccess (authUser, subEntity, challengeDetails) {
415400
let resources
416401

417402
const challengeId = yield getV5ChallengeId(subEntity.challengeId)
@@ -423,18 +408,6 @@ function * checkCreateAccess (authUser, subEntity) {
423408

424409
const token = yield getM2Mtoken()
425410

426-
try {
427-
logger.info(`Calling to challenge API for fetch phases and winners for ${challengeId}`)
428-
challengeDetails = yield request.get(`${config.CHALLENGEAPI_V5_URL}/${challengeId}`)
429-
.set('Authorization', `Bearer ${token}`)
430-
.set('Content-Type', 'application/json')
431-
logger.info(`returned for ${challengeId} with ${JSON.stringify(challengeDetails)}`)
432-
} catch (ex) {
433-
logger.error(`Error while accessing ${config.CHALLENGEAPI_V5_URL}/${challengeId}`)
434-
logger.error(ex)
435-
throw new errors.HttpStatusError(503, `Could not fetch details of challenge with id ${challengeId}`)
436-
}
437-
438411
try {
439412
resources = yield request.get(`${config.RESOURCEAPI_V5_BASE_URL}/resources?challengeId=${challengeId}`)
440413
.set('Authorization', `Bearer ${token}`)
@@ -462,7 +435,7 @@ function * checkCreateAccess (authUser, subEntity) {
462435
})
463436

464437
// Get phases and winner detail from challengeDetails
465-
const phases = challengeDetails.body.phases
438+
const { phases } = challengeDetails
466439

467440
// Check if the User is assigned as the reviewer for the contest
468441
const reviewers = _.filter(currUserRoles, { role: 'Reviewer' })
@@ -482,7 +455,7 @@ function * checkCreateAccess (authUser, subEntity) {
482455
throw new errors.HttpStatusError(403, `Register for the contest before you can submit`)
483456
}
484457

485-
const submissionPhaseId = yield getSubmissionPhaseId(subEntity.challengeId)
458+
const submissionPhaseId = yield getSubmissionPhaseId(challengeDetails)
486459

487460
if (submissionPhaseId == null) {
488461
throw new errors.HttpStatusError(403, 'You cannot create a submission in the current phase')

src/services/SubmissionService.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,12 +281,13 @@ function * createSubmission (authUser, files, entity) {
281281

282282
// Submission api only works with legacy challenge id
283283
// If it is a v5 challenge id, get the associated legacy challenge id
284+
const challenge = yield helper.getChallenge(entity.challengeId)
284285
const {
285286
id: challengeId,
286287
status,
287288
phases,
288289
legacyId: legacyChallengeId
289-
} = yield helper.getChallenge(entity.challengeId)
290+
} = challenge
290291
const currDate = (new Date()).toISOString()
291292

292293
if (status !== 'Active') {
@@ -321,7 +322,7 @@ function * createSubmission (authUser, files, entity) {
321322
if (entity.submissionPhaseId) {
322323
item.submissionPhaseId = entity.submissionPhaseId
323324
} else {
324-
item.submissionPhaseId = yield helper.getSubmissionPhaseId(entity.challengeId)
325+
item.submissionPhaseId = helper.getSubmissionPhaseId(challenge)
325326
}
326327

327328
if (item.submissionPhaseId) {
@@ -341,7 +342,7 @@ function * createSubmission (authUser, files, entity) {
341342
logger.info('Check User access before creating the submission')
342343
if (_.intersection(authUser.roles, ['Administrator', 'administrator']).length === 0 && !authUser.scopes) {
343344
logger.info(`Calling checkCreateAccess for ${JSON.stringify(authUser)}`)
344-
yield helper.checkCreateAccess(authUser, item)
345+
yield helper.checkCreateAccess(authUser, item, challenge)
345346

346347
if (entity.submittedDate) {
347348
throw new errors.HttpStatusError(403, 'You are not allowed to set the `submittedDate` attribute on a submission')

0 commit comments

Comments
 (0)