Skip to content

Issue 179 #185

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,6 @@ This script will load the data from `scripts/data` directory into ES
npm run start
```

#### Duplicating the ES Index

To duplicate the existing ES Index (from the `ES_INDEX` to `ES_INDEX_V2` based on the configs in `config/default.js`) run `npm run create-new-index`

#### Linting JS files

```
Expand Down
1 change: 0 additions & 1 deletion config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ module.exports = {
HOST: process.env.ES_HOST || 'localhost:9200',
API_VERSION: process.env.ES_API_VERSION || '6.3',
ES_INDEX: process.env.ES_INDEX || 'submission',
ES_INDEX_V2: process.env.ES_INDEX_V2 || 'new_submission',
ES_TYPE: process.env.ES_TYPE || '_doc' // ES 6.x accepts only 1 Type per index and it's mandatory to define it
},
PAGE_SIZE: process.env.PAGE_SIZE || 20,
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"create-tables": "node scripts/createTables.js",
"init-db": "node scripts/importData.js",
"create-index": "node scripts/createIndex.js",
"create-new-index": "node scripts/createNewIndex.js",
"delete-index": "node scripts/deleteIndex.js",
"init-es": "node scripts/loadES.js",
"db-to-es": "node scripts/migrateFromDBToES.js",
Expand Down
10 changes: 5 additions & 5 deletions scripts/ESloadHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const esClient = helper.getEsClient()
function deleteDatafromES () {
logger.info('Clear data from ES if any')
const filter = {
index: config.get('esConfig.ES_INDEX_V2'),
index: config.get('esConfig.ES_INDEX'),
type: config.get('esConfig.ES_TYPE'),
q: '*'
}
Expand All @@ -34,7 +34,7 @@ function * loadReviewTypes () {
const promises = []
reviewTypes.forEach((reviewType) => {
const record = {
index: config.get('esConfig.ES_INDEX_V2'),
index: config.get('esConfig.ES_INDEX'),
type: config.get('esConfig.ES_TYPE'),
id: reviewType.id,
body: _.extend({ resource: 'reviewType' }, reviewType)
Expand All @@ -51,7 +51,7 @@ function * loadSubmissions () {
const promises = []
submissions.forEach((submission) => {
const record = {
index: config.get('esConfig.ES_INDEX_V2'),
index: config.get('esConfig.ES_INDEX'),
type: config.get('esConfig.ES_TYPE'),
id: submission.id,
body: _.extend({ resource: 'submission' }, submission)
Expand All @@ -68,7 +68,7 @@ function * loadReviews () {
const promises = []
reviews.forEach((review) => {
const record = {
index: config.get('esConfig.ES_INDEX_V2'),
index: config.get('esConfig.ES_INDEX'),
type: config.get('esConfig.ES_TYPE'),
id: review.id,
body: _.extend({ resource: 'review' }, review)
Expand All @@ -85,7 +85,7 @@ function * loadReviewSummations () {
const promises = []
reviewSummations.forEach((reviewSummation) => {
const record = {
index: config.get('esConfig.ES_INDEX_V2'),
index: config.get('esConfig.ES_INDEX'),
type: config.get('esConfig.ES_TYPE'),
id: reviewSummation.id,
body: _.extend({ resource: 'reviewSummation' }, reviewSummation)
Expand Down
4 changes: 2 additions & 2 deletions scripts/createIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ co(function * createIndex () {
// fields not specified below will be 'text' by default
properties: {
resource: { type: 'keyword' },
challengeId: { type: 'keyword' },
challengeId: { type: 'long' },
memberId: { type: 'keyword' },
type: { type: 'keyword' },
isFileSubmission: { type: 'boolean' },
Expand All @@ -44,7 +44,7 @@ co(function * createIndex () {
}
}
yield esClient.indices.create({
index: config.get('esConfig.ES_INDEX_V2'),
index: config.get('esConfig.ES_INDEX'),
body
})
logger.info('ES Index creation succeeded!')
Expand Down
36 changes: 0 additions & 36 deletions scripts/createNewIndex.js

This file was deleted.

5 changes: 1 addition & 4 deletions scripts/deleteIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@ const helper = require('../src/common/helper')
co(function * deleteIndex () {
logger.info('ES Index deletion started!')
const esClient = helper.getEsClient()
// yield esClient.indices.delete({
// index: config.get('esConfig.ES_INDEX')
// })
yield esClient.indices.delete({
index: config.get('esConfig.ES_INDEX_V2')
index: config.get('esConfig.ES_INDEX')
})
logger.info('ES Index deletion succeeded!')
process.exit(0)
Expand Down
2 changes: 1 addition & 1 deletion scripts/migrateFromDBToES.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function * migrateRecords (tableName) {
logger.debug(`Number of ${tableName}s fetched from DB - ` + totalRecords)
for (let i = 0; i < totalRecords; i++) {
const record = {
index: config.get('esConfig.ES_INDEX_V2'),
index: config.get('esConfig.ES_INDEX'),
type: config.get('esConfig.ES_TYPE'),
id: records.Items[i].id,
body: {
Expand Down
39 changes: 18 additions & 21 deletions src/common/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ function prepESFilter (query, actResource) {
}

const searchCriteria = {
index: config.get('esConfig.ES_INDEX_V2'),
index: config.get('esConfig.ES_INDEX'),
type: config.get('esConfig.ES_TYPE'),
size: pageSize,
from: (page - 1) * pageSize, // Es Index starts from 0
Expand Down Expand Up @@ -345,14 +345,18 @@ function * getM2Mtoken (parentSpan) {
* @param {Object} parentSpan the parent Span object
* @returns {String} Legacy Challenge ID of the given challengeId
*/
function * getlegacyChallengeId (challengeId, token, parentSpan) {
function * getLegacyChallengeId (challengeId, parentSpan) {
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)) {
logger.debug(`${challengeId} detected as uuid. Fetching legacy challenge id`)
const getlegacyChallengeIdSpan = tracer.startChildSpans('helper.getlegacyChallengeId', parentSpan)
const token = yield getM2Mtoken(getlegacyChallengeIdSpan)
try {
const response = yield request.get(`${config.CHALLENGEAPI_V5_URL}/${challengeId}`)
.set('Authorization', `Bearer ${token}`)
.set('Content-Type', 'application/json')
return response.body.legacyId
const legacyId = parseInt(response.body.legacyId, 10)
logger.debug(`Legacy challenge id is ${legacyId} for v5 challenge id ${challengeId}`)
return legacyId
} catch (err) {
getlegacyChallengeIdSpan.setTag('error', true)
logger.error(`Error while accessing ${config.CHALLENGEAPI_V5_URL}/${challengeId}`)
Expand Down Expand Up @@ -381,19 +385,17 @@ function * getSubmissionPhaseId (challengeId, parentSpan) {
try {
let phaseId = null
let response
let legacyChallengeId

const token = yield getM2Mtoken(getSubmissionPhaseIdSpan)

const getChallengePhasesSpan = tracer.startChildSpans('getChallengePhases', getSubmissionPhaseIdSpan)
getChallengePhasesSpan.setTag('challengeId', challengeId)
try {
legacyChallengeId = yield getlegacyChallengeId(challengeId, token, getChallengePhasesSpan)
response = yield request.get(`${config.CHALLENGEAPI_URL}/${legacyChallengeId}/phases`)
response = yield request.get(`${config.CHALLENGEAPI_URL}/${challengeId}/phases`)
.set('Authorization', `Bearer ${token}`)
.set('Content-Type', 'application/json')
} catch (ex) {
logger.error(`Error while accessing ${config.CHALLENGEAPI_URL}/${legacyChallengeId}/phases`)
logger.error(`Error while accessing ${config.CHALLENGEAPI_URL}/${challengeId}/phases`)
logger.debug('Setting submissionPhaseId to Null')
response = null
// log error
Expand Down Expand Up @@ -437,7 +439,6 @@ function * checkCreateAccess (authUser, subEntity, parentSpan) {

try {
let response
let legacyChallengeId

// User can only create submission for themselves
if (authUser.userId !== subEntity.memberId) {
Expand All @@ -449,12 +450,11 @@ function * checkCreateAccess (authUser, subEntity, parentSpan) {
const getChallengeDetailSpan = tracer.startChildSpans('getChallengeDetail', checkCreateAccessSpan)
getChallengeDetailSpan.setTag('challengeId', subEntity.challengeId)
try {
legacyChallengeId = yield getlegacyChallengeId(subEntity.challengeId, token, getChallengeDetailSpan)
response = yield request.get(`${config.CHALLENGEAPI_URL}?filter=id=${legacyChallengeId}`)
response = yield request.get(`${config.CHALLENGEAPI_URL}?filter=id=${subEntity.challengeId}`)
.set('Authorization', `Bearer ${token}`)
.set('Content-Type', 'application/json')
} catch (ex) {
logger.error(`Error while accessing ${config.CHALLENGEAPI_URL}?filter=id=${legacyChallengeId}`)
logger.error(`Error while accessing ${config.CHALLENGEAPI_URL}?filter=id=${subEntity.challengeId}`)
logger.error(ex)
// log error
getChallengeDetailSpan.log({
Expand Down Expand Up @@ -506,7 +506,6 @@ function * checkGetAccess (authUser, submission, parentSpan) {
try {
let resources
let challengeDetails
let legacyChallengeId
// Allow downloading Own submission
if (submission.memberId === authUser.userId) {
return true
Expand All @@ -517,12 +516,11 @@ function * checkGetAccess (authUser, submission, parentSpan) {
const getChallengeResourcesSpan = tracer.startChildSpans('getChallengeResources', checkGetAccessSpan)
getChallengeResourcesSpan.setTag('challengeId', submission.challengeId)
try {
legacyChallengeId = yield getlegacyChallengeId(submission.challengeId, token, getChallengeResourcesSpan)
resources = yield request.get(`${config.CHALLENGEAPI_URL}/${legacyChallengeId}/resources`)
resources = yield request.get(`${config.CHALLENGEAPI_URL}/${submission.challengeId}/resources`)
.set('Authorization', `Bearer ${token}`)
.set('Content-Type', 'application/json')
} catch (ex) {
logger.error(`Error while accessing ${config.CHALLENGEAPI_URL}/${legacyChallengeId}/resources`)
logger.error(`Error while accessing ${config.CHALLENGEAPI_URL}/${submission.challengeId}/resources`)
logger.error(ex)
// log error
getChallengeResourcesSpan.log({
Expand All @@ -538,11 +536,11 @@ function * checkGetAccess (authUser, submission, parentSpan) {
const getChallengeDetailSpan = tracer.startChildSpans('getChallengeDetail', checkGetAccessSpan)
getChallengeDetailSpan.setTag('challengeId', submission.challengeId)
try {
challengeDetails = yield request.get(`${config.CHALLENGEAPI_URL}?filter=id=${legacyChallengeId}`)
challengeDetails = yield request.get(`${config.CHALLENGEAPI_URL}?filter=id=${submission.challengeId}`)
.set('Authorization', `Bearer ${token}`)
.set('Content-Type', 'application/json')
} catch (ex) {
logger.error(`Error while accessing ${config.CHALLENGEAPI_URL}?filter=id=${legacyChallengeId}`)
logger.error(`Error while accessing ${config.CHALLENGEAPI_URL}?filter=id=${submission.challengeId}`)
logger.error(ex)
// log error
getChallengeDetailSpan.log({
Expand Down Expand Up @@ -636,18 +634,16 @@ function * checkReviewGetAccess (authUser, submission, parentSpan) {

try {
let challengeDetails
let legacyChallengeId
const token = yield getM2Mtoken(checkReviewGetAccessSpan)

const getChallengeDetailSpan = tracer.startChildSpans('getChallengeDetail', checkReviewGetAccessSpan)
getChallengeDetailSpan.setTag('challengeId', submission.challengeId)
try {
legacyChallengeId = yield getlegacyChallengeId(submission.challengeId, token, getChallengeDetailSpan)
challengeDetails = yield request.get(`${config.CHALLENGEAPI_URL}?filter=id=${legacyChallengeId}`)
challengeDetails = yield request.get(`${config.CHALLENGEAPI_URL}?filter=id=${submission.challengeId}`)
.set('Authorization', `Bearer ${token}`)
.set('Content-Type', 'application/json')
} catch (ex) {
logger.error(`Error while accessing ${config.CHALLENGEAPI_URL}?filter=id=${legacyChallengeId}`)
logger.error(`Error while accessing ${config.CHALLENGEAPI_URL}?filter=id=${submission.challengeId}`)

// log error
getChallengeDetailSpan.log({
Expand Down Expand Up @@ -800,6 +796,7 @@ module.exports = {
fetchFromES,
camelize,
setPaginationHeaders,
getLegacyChallengeId,
getSubmissionPhaseId,
checkCreateAccess,
checkGetAccess,
Expand Down
20 changes: 18 additions & 2 deletions src/services/SubmissionService.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,12 @@ function * listSubmissions (authUser, query, span) {
const listSubmissionsSpan = tracer.startChildSpans('SubmissionService.listSubmissions', span)
let data = []

if (query.challengeId) {
// Submission api only works with legacy challenge id
// If it is a v5 challenge id, get the associated legacy challenge id
query.challengeId = yield helper.getLegacyChallengeId(query.challengeId, listSubmissionsSpan)
}

try {
data = yield helper.fetchFromES(query, helper.camelize(table), listSubmissionsSpan)
logger.info(`listSubmissions: returning ${data.rows.length} submissions for query: ${JSON.stringify(query)}`)
Expand Down Expand Up @@ -341,14 +347,18 @@ function * createSubmission (authUser, files, entity, span) {
throw new errors.HttpStatusError(400, 'The file should be uploaded under the "submission" attribute')
}

// Submission api only works with legacy challenge id
// If it is a v5 challenge id, get the associated legacy challenge id
const challengeId = yield helper.getLegacyChallengeId(entity.challengeId, createSubmissionSpan)

const currDate = (new Date()).toISOString()

const item = {
id: submissionId,
type: entity.type,
url: url,
memberId: entity.memberId,
challengeId: entity.challengeId,
challengeId: challengeId,
created: currDate,
updated: currDate,
createdBy: authUser.handle || authUser.sub,
Expand All @@ -366,7 +376,7 @@ function * createSubmission (authUser, files, entity, span) {
if (entity.submissionPhaseId) {
item.submissionPhaseId = entity.submissionPhaseId
} else {
item.submissionPhaseId = yield helper.getSubmissionPhaseId(entity.challengeId, createSubmissionSpan)
item.submissionPhaseId = yield helper.getSubmissionPhaseId(challengeId, createSubmissionSpan)
}

if (entity.fileType) {
Expand Down Expand Up @@ -457,6 +467,12 @@ function * _updateSubmission (authUser, submissionId, entity, parentSpan) {
throw new errors.HttpStatusError(404, `Submission with ID = ${submissionId} is not found`)
}

if (entity.challengeId) {
// Submission api only works with legacy challenge id
// If it is a v5 challenge id, get the associated legacy challenge id
entity.challengeId = yield helper.getLegacyChallengeId(entity.challengeId, updateSubmissionSpan)
}

const currDate = (new Date()).toISOString()
// Record used for updating in Database
const record = {
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/SubmissionService.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ describe('Submission Service tests', () => {
.attach('submission', './test/common/fileToUpload.zip', 'fileToUpload.zip')
.end((err, res) => {
res.should.have.status(400)
res.body.message.should.be.eql('Either file to be uploaded or URL should be present')
res.body.message.should.be.eql('Either file to be uploaded or URL should be present. Not both.')
done()
})
})
Expand Down
2 changes: 1 addition & 1 deletion test/unit/SubmissionService.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ describe('Submission Service tests', () => {
res.should.have.status(200)
res.body.should.have.keys(Object.keys(_.extend({ fileType: 'zip' }, testSubmission.Item)))
res.body.id.should.not.be.eql(null)
res.body.challengeId.should.be.eql(testChallengeV5APIResponse.id)
res.body.challengeId.should.be.eql(parseInt(testChallengeV5APIResponse.legacyId, 10))
res.body.type.should.be.eql(testSubmission.Item.type)
res.body.url.should.be.eql(testSubmission.Item.url)
res.body.fileType.should.be.eql('zip')
Expand Down