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

Commit 5c6d7c2

Browse files
author
James Cori
committed
Fixing Metadata Migration
1 parent 09e991e commit 5c6d7c2

File tree

3 files changed

+80
-54
lines changed

3 files changed

+80
-54
lines changed

src/scripts/migrations/010-fix-effort-hours.js

Lines changed: 48 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const _ = require('lodash')
1111
const logger = require('../../util/logger')
1212
const challengeService = require('../../services/challengeService')
1313
const { getESClient } = require('../../util/helper')
14-
const { getEffortHoursFromIfx } = require('../../services/challengeInformixService')
14+
const { execQuery, getEffortHoursFromIfx } = require('../../services/challengeInformixService')
1515

1616
const mapping = {
1717
effortHoursEstimate: 88,
@@ -28,26 +28,41 @@ const migrationFunction = {
2828

2929
while (!finish) {
3030
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) {
31+
const legacyIdRows = await getEffortHoursChallengeIds(page, perPage)
32+
logger.info(`Found ${legacyIdRows.length} legacy challenge ids`)
33+
if (legacyIdRows.length > 0) {
34+
for (const legacyIdRow of legacyIdRows) {
35+
const [challenge] = await challengeService.getChallengeFromV5API(legacyIdRow.legacy_id)
36+
if (!challenge) {
37+
logger.error(`Challenge not found ${legacyIdRow.legacy_id}`)
38+
continue
39+
}
3540
challenge.legacy.migration = 10
36-
const legacyData = await getEffortHoursFromIfx(challenge.legacyId)
41+
const legacyData = await getEffortHoursFromIfx(legacyIdRow.legacy_id)
42+
// logger.debug(`Legacy Data: ${JSON.stringify(legacyData)}`)
3743
if (legacyData.length > 0) {
38-
_.keys(mapping, (key) => {
44+
_.forEach(mapping, (mappingValue, key) => {
45+
// logger.debug(`${JSON.stringify(mappingValue)} -> ${key}`)
3946
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-
})
47+
const legacyIndex = _.findIndex(legacyData, entry => entry.project_info_type_id === mappingValue)
48+
if (legacyIndex > -1) {
49+
if (v5Index === -1) {
50+
const newData = {
51+
name: key,
52+
value: legacyData[legacyIndex].value
53+
}
54+
// logger.debug(`Not found in v5, adding ${JSON.stringify(newData)}`)
55+
challenge.metadata.push(newData)
56+
} else {
57+
challenge.metadata[v5Index].value = legacyData[legacyIndex].value
58+
// logger.debug(`Metadata found in v5, updating v5 index: ${v5Index} ${legacyIndex} V5 Metadata ${JSON.stringify(challenge.metadata[v5Index])} -- Legacy Data ${JSON.stringify(legacyData[legacyIndex])}`)
59+
}
4660
} else {
47-
challenge.metadata[v5Index].value = legacyData[legacyIndex].value
61+
// logger.debug(`Key ${key} not found in legacy array`)
4862
}
4963
})
50-
await challengeService.save(challenge)
64+
logger.debug(`Writing Challenge ${JSON.stringify(challenge)}`)
65+
// await challengeService.save(challenge)
5166
}
5267
}
5368
} else {
@@ -59,38 +74,25 @@ const migrationFunction = {
5974
}
6075
}
6176

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-
}
77+
/**
78+
* Get effort hours for a legacyId
79+
* @param {Number} legacyId the legacy ID
80+
*/
81+
async function getEffortHoursChallengeIds (page, perPage) {
82+
let limitOffset = `first ${perPage}`
83+
if (page > 0) {
84+
limitOffset = `skip ${(page * perPage)} ${limitOffset}`
9185
}
92-
// Extract data from hits
93-
return _.map(docs.hits.hits, item => (item._source))
86+
87+
logger.debug(`getEffortHoursChallengeIds ${page} ${perPage}`)
88+
const sql = `SELECT
89+
${limitOffset}
90+
DISTINCT project_id as legacy_id
91+
FROM project_info
92+
WHERE project_info_type_id in (88, 89, 90)
93+
`
94+
logger.info(`Effort Hours SQL: ${sql}`)
95+
return execQuery(sql)
9496
}
9597

9698
module.exports = migrationFunction

src/services/challengeInformixService.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ async function getEffortHoursFromIfx (legacyId) {
1515
FROM project_info
1616
WHERE project_id = ${legacyId} and project_info_type_id in (88, 89, 90)
1717
`
18-
logger.info(`Effort Hours SQL: ${sql}`)
18+
// logger.info(`Effort Hours SQL: ${sql}`)
1919
return execQuery(sql)
2020
}
2121

src/services/challengeService.js

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -762,14 +762,38 @@ async function buildV5Challenge (legacyId, challengeListing, challengeDetails) {
762762
effortHoursOnshore: 90
763763
}
764764

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+
765776
const legacyEffortHoursData = await challengeInformixService.getEffortHoursFromIfx(legacyId)
766777
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-
})
778+
_.forEach(effortHoursMapping, (mappingValue, key) => {
779+
// logger.debug(`${JSON.stringify(mappingValue)} -> ${key}`)
780+
const v5Index = _.findIndex(metadata, meta => meta.name === key)
781+
const legacyIndex = _.findIndex(legacyEffortHoursData, entry => entry.project_info_type_id === mappingValue)
782+
if (legacyIndex > -1) {
783+
if (v5Index === -1) {
784+
const newData = {
785+
name: key,
786+
value: legacyEffortHoursData[legacyIndex].value
787+
}
788+
// logger.debug(`Not found in v5, adding ${JSON.stringify(newData)}`)
789+
metadata.push(newData)
790+
} else {
791+
metadata[v5Index].value = legacyEffortHoursData[legacyIndex].value
792+
// logger.debug(`Metadata found in v5, updating v5 index: ${v5Index} ${legacyIndex} V5 Metadata ${JSON.stringify(challenge.metadata[v5Index])} -- Legacy Data ${JSON.stringify(legacyData[legacyIndex])}`)
793+
}
794+
} else {
795+
// logger.debug(`Key ${key} not found in legacy array`)
796+
}
773797
})
774798
}
775799

@@ -857,7 +881,7 @@ async function convertGroupIdsToV5UUIDs (oldIds) {
857881
async function getChallengeFromV5API (legacyId) {
858882
const token = await getM2MToken()
859883
const url = `${config.CHALLENGE_API_URL}?legacyId=${legacyId}&perPage=1&page=1`
860-
// logger.debug(`Get Challenge from V5 URL ${url}`)
884+
logger.debug(`Get Challenge from V5 URL ${url}`)
861885
let res = null
862886
try {
863887
res = await axios.get(url, { headers: { Authorization: `Bearer ${token}` } })

0 commit comments

Comments
 (0)