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

Commit c7c181a

Browse files
authored
Merge pull request #45 from topcoder-platform/effort-hours
Effort hours
2 parents fd2f9f1 + 2b51922 commit c7c181a

File tree

3 files changed

+129
-1
lines changed

3 files changed

+129
-1
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/**
2+
* Populate the following properties on the challenges:
3+
* - metadata.effortHoursEstimate
4+
* - metadata.effortHoursOffshore
5+
* - metadata.effortHoursOnshore
6+
*/
7+
global.Promise = require('bluebird')
8+
9+
const config = require('config')
10+
const _ = require('lodash')
11+
const logger = require('../../util/logger')
12+
const challengeService = require('../../services/challengeService')
13+
const { getESClient } = require('../../util/helper')
14+
const { getEffortHoursFromIfx } = require('../../services/challengeInformixService')
15+
16+
const mapping = {
17+
effortHoursEstimate: 88,
18+
effortHoursOffshore: 89,
19+
effortHoursOnshore: 90
20+
}
21+
22+
const migrationFunction = {
23+
run: async () => {
24+
const perPage = config.get('MIGRATION_SCRIPT_BATCH_SIZE')
25+
let finish = false
26+
let page = 0
27+
let batch = 1
28+
29+
while (!finish) {
30+
logger.info(`Batch-${batch} - Loading challenges`)
31+
const challenges = await getChallengesMissingData(page, perPage)
32+
logger.info(`Found ${challenges.length} challenges`)
33+
if (challenges.length > 0) {
34+
for (const challenge of challenges) {
35+
challenge.legacy.migration = 10
36+
const legacyData = await getEffortHoursFromIfx(challenge.legacyId)
37+
if (legacyData.length > 0) {
38+
_.keys(mapping, (key) => {
39+
const v5Index = _.findIndex(challenge.metadata, meta => meta.name === key)
40+
const legacyIndex = _.findIndex(legacyData, entry => entry.project_info_type_id === mapping[key])
41+
if (v5Index === -1) {
42+
challenge.metadata.push({
43+
name: key,
44+
value: legacyData[legacyIndex].value
45+
})
46+
} else {
47+
challenge.metadata[v5Index].value = legacyData[legacyIndex].value
48+
}
49+
})
50+
await challengeService.save(challenge)
51+
}
52+
}
53+
} else {
54+
finish = true
55+
}
56+
page++
57+
batch++
58+
}
59+
}
60+
}
61+
62+
async function getChallengesMissingData (page = 0, perPage = 10) {
63+
const esQuery = {
64+
index: config.get('ES.CHALLENGE_ES_INDEX'),
65+
type: config.get('ES.CHALLENGE_ES_TYPE'),
66+
size: perPage,
67+
from: page * perPage,
68+
body: {
69+
query: {
70+
range: {
71+
'legacy.migration': {
72+
lt: 10
73+
}
74+
}
75+
}
76+
}
77+
}
78+
// logger.debug(`ES Query ${JSON.stringify(esQuery)}`)
79+
// Search with constructed query
80+
let docs
81+
try {
82+
docs = await getESClient().search(esQuery)
83+
} catch (e) {
84+
// Catch error when the ES is fresh and has no data
85+
docs = {
86+
hits: {
87+
total: 0,
88+
hits: []
89+
}
90+
}
91+
}
92+
// Extract data from hits
93+
return _.map(docs.hits.hits, item => (item._source))
94+
}
95+
96+
module.exports = migrationFunction

src/services/challengeInformixService.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@ const helper = require('../util/helper')
44
// const getErrorService = require('./errorService')
55
const { executeQueryAsync } = require('../util/informixWrapper')
66

7+
/**
8+
* Get effort hours for a legacyId
9+
* @param {Number} legacyId the legacy ID
10+
*/
11+
async function getEffortHoursFromIfx (legacyId) {
12+
const sql = `SELECT LIMIT 1
13+
project_info_type_id,
14+
value,
15+
FROM project_info
16+
WHERE project_id = ${legacyId} and project_info_type_id in (88, 89, 90)
17+
`
18+
return execQuery(sql)
19+
}
20+
721
/**
822
* Gets the copilot payment for a legacyId
923
* @param {Number} legacyId the legacy ID
@@ -683,5 +697,6 @@ module.exports = {
683697
getCopilotPaymentFromIfx,
684698
createCopilotPaymentInIfx,
685699
updateCopilotPaymentInIfx,
686-
deleteCopilotPaymentFromIfx
700+
deleteCopilotPaymentFromIfx,
701+
getEffortHoursFromIfx
687702
}

src/services/challengeService.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,23 @@ async function buildV5Challenge (legacyId, challengeListing, challengeDetails) {
756756
})
757757
metadata.push(..._.compact(allMetadata))
758758

759+
const effortHoursMapping = {
760+
effortHoursEstimate: 88,
761+
effortHoursOffshore: 89,
762+
effortHoursOnshore: 90
763+
}
764+
765+
const legacyEffortHoursData = await challengeInformixService.getEffortHoursFromIfx(legacyId)
766+
if (legacyEffortHoursData.length > 0) {
767+
_.keys(effortHoursMapping, (key) => {
768+
const legacyIndex = _.findIndex(legacyEffortHoursData, entry => entry.project_info_type_id === effortHoursMapping[key])
769+
metadata.push({
770+
name: key,
771+
value: legacyEffortHoursData[legacyIndex].value
772+
})
773+
})
774+
}
775+
759776
const events = []
760777
if (challengeListing.events && challengeListing.events.length > 0) {
761778
for (const event of challengeListing.events) {

0 commit comments

Comments
 (0)