Skip to content
This repository was archived by the owner on Jan 23, 2025. It is now read-only.

Commit 9dadac7

Browse files
author
James Cori
committed
Merge branch 'develop'
2 parents 206994e + 3796e1c commit 9dadac7

File tree

7 files changed

+143
-5
lines changed

7 files changed

+143
-5
lines changed

README.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,16 @@ npm i
6464
- Inside the docker container, start the express server: `npm start`
6565

6666
## Migrations
67+
### For local migrations
68+
There are two parts need to be updated for local migrations,
69+
- `./src/models/challenge.js`
70+
`throughput: 'ON_DEMAND',` should be updated to `throughput:{ read: 4, write: 2 },`
71+
- `./config/default.js`
72+
Two aws config should be uncommented
73+
74+
and env variable `IS_LOCAL_DB` should be set to true before you continue to the next steps.
75+
76+
### Run a migration
6777
- To run a migration, the command should be:
6878
`MIGRATION=<name-of-migration-file> nf run npm run migrate`
6979
Example:
@@ -76,4 +86,4 @@ Example:
7686
- TBD
7787

7888
## Running tests in CI
79-
- TBD
89+
- TBD

config/default.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ module.exports = {
5656

5757
AMAZON: {
5858
// Uncomment for local deployment
59-
// AWS_ACCESS_KEY_ID: process.env.AWS_FAKE_ID,
60-
// AWS_SECRET_ACCESS_KEY: process.env.AWS_FAKE_KEY,
59+
// AWS_ACCESS_KEY_ID: process.env.AWS_FAKE_ID || 'FAKE_ACCESS_KEY',
60+
// AWS_SECRET_ACCESS_KEY: process.env.AWS_FAKE_KEY || 'FAKE_SECRET_ACCESS_KEY',
6161
AWS_REGION: process.env.AWS_REGION || 'ap-northeast-1', // aws region
6262
IS_LOCAL_DB: process.env.IS_LOCAL_DB ? process.env.IS_LOCAL_DB === 'true' : false, // true or uninitialize if we use local instance
6363
DYNAMODB_URL: process.env.DYNAMODB_URL || 'http://localhost:8000', // just for local development

src/constants.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* App constants
3+
*/
4+
5+
const prizeSetTypes = {
6+
ChallengePrizes: 'placement',
7+
CopilotPayment: 'copilot',
8+
ReviewerPayment: 'reviewer',
9+
CheckpointPrizes: 'checkpoint'
10+
}
11+
12+
module.exports = {
13+
prizeSetTypes
14+
}

src/models/challenge.js

+4
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ const schema = new Schema({
108108
type: Array,
109109
required: false
110110
},
111+
overview: {
112+
type: Object,
113+
required: false
114+
},
111115
created: {
112116
type: Date,
113117
required: true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/**
2+
* Populate the following properties on the challenges:
3+
* - overview.totalPrizes
4+
*/
5+
global.Promise = require('bluebird')
6+
7+
const config = require('config')
8+
const _ = require('lodash')
9+
const logger = require('../../util/logger')
10+
const helper = require('../../util/helper')
11+
const constants = require('../../constants')
12+
const challengeService = require('../../services/challengeService')
13+
const { getESClient } = require('../../util/helper')
14+
15+
const migrationFunction = {
16+
run: async () => {
17+
const perPage = config.get('MIGRATION_SCRIPT_BATCH_SIZE')
18+
let finish = false
19+
let page = 0
20+
let batch = 1
21+
22+
while (!finish) {
23+
logger.info(`Batch-${batch} - Loading challenges`)
24+
const challenges = await getChallengesMissingData(page, perPage)
25+
logger.info(`Found ${challenges.length} challenges`)
26+
if (challenges.length > 0) {
27+
for (const challenge of challenges) {
28+
if (challenge.prizeSets) {
29+
const prizeSetsGroup = _.groupBy(challenge.prizeSets, 'type')
30+
if (prizeSetsGroup[constants.prizeSetTypes.ChallengePrizes]) {
31+
const totalPrizes = helper.sumOfPrizes(prizeSetsGroup[constants.prizeSetTypes.ChallengePrizes][0].prizes)
32+
_.set(challenge, 'overview.totalPrizes', totalPrizes)
33+
// logger.debug(`Updating Challenge ${challenge.id} - ${JSON.stringify(challenge.overview)}`)
34+
await challengeService.save(challenge)
35+
} else {
36+
logger.debug(`No prizeSetGroup ${challenge.id}`)
37+
}
38+
} else {
39+
logger.debug(`No prizeSet ${challenge.id}`)
40+
}
41+
}
42+
} else {
43+
finish = true
44+
}
45+
page++
46+
batch++
47+
}
48+
}
49+
}
50+
51+
async function getChallengesMissingData (page = 0, perPage = 10) {
52+
const esQuery = {
53+
index: config.get('ES.CHALLENGE_ES_INDEX'),
54+
type: config.get('ES.CHALLENGE_ES_TYPE'),
55+
size: perPage,
56+
from: page * perPage,
57+
body: {
58+
query: {
59+
bool: {
60+
must_not: {
61+
exists: {
62+
field: 'overview.totalPrizes'
63+
}
64+
}
65+
}
66+
}
67+
}
68+
}
69+
// logger.debug(`ES Query ${JSON.stringify(esQuery)}`)
70+
// Search with constructed query
71+
let docs
72+
try {
73+
docs = await getESClient().search(esQuery)
74+
} catch (e) {
75+
// Catch error when the ES is fresh and has no data
76+
docs = {
77+
hits: {
78+
total: 0,
79+
hits: []
80+
}
81+
}
82+
}
83+
// Extract data from hits
84+
return _.map(docs.hits.hits, item => (item._source))
85+
}
86+
87+
module.exports = migrationFunction

src/services/challengeService.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const resourceInformixService = require('./resourceInformixService')
1616
const translationService = require('./translationService')
1717
const { V4_TRACKS } = require('../util/conversionMappings')
1818
const { getCopilotPaymentFromIfx } = require('./challengeInformixService')
19+
const constants = require('../constants')
1920

2021
let allV5Terms
2122

@@ -760,7 +761,11 @@ async function buildV5Challenge (legacyId, challengeListing, challengeDetails) {
760761
}
761762

762763
// console.log(JSON.stringify(metadata))
763-
764+
const prizeSetsGroup = _.groupBy(prizeSets, 'type')
765+
if (prizeSetsGroup[constants.prizeSetTypes.ChallengePrizes]) {
766+
const totalPrizes = helper.sumOfPrizes(prizeSetsGroup[constants.prizeSetTypes.ChallengePrizes][0].prizes)
767+
_.set(newChallenge, 'overview.totalPrizes', totalPrizes)
768+
}
764769
return _.assign(newChallenge, { prizeSets, tags, groups, winners, phases, metadata, terms, events })
765770
}
766771

src/util/helper.js

+19-1
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,23 @@ function setResHeaders (req, res, result) {
239239
}
240240
}
241241

242+
/**
243+
* Calculate the sum of prizes.
244+
*
245+
* @param {Array} prizes the list of prize
246+
* @returns {Object} the result prize
247+
*/
248+
function sumOfPrizes (prizes) {
249+
let sum = 0
250+
if (!prizes.length) {
251+
return sum
252+
}
253+
for (const prize of prizes) {
254+
sum += prize.value
255+
}
256+
return sum
257+
}
258+
242259
module.exports = {
243260
forceV4ESFeeder,
244261
wrapExpress,
@@ -249,5 +266,6 @@ module.exports = {
249266
scanDynamoModelByProperty,
250267
generateInformxDate,
251268
getM2MToken,
252-
setResHeaders
269+
setResHeaders,
270+
sumOfPrizes
253271
}

0 commit comments

Comments
 (0)