Skip to content

Commit 42da375

Browse files
committed
submit codes
1 parent 3aea18b commit 42da375

File tree

3 files changed

+126
-18
lines changed

3 files changed

+126
-18
lines changed

prisma/migrations/20250814211810_add_subtrack_id_to_datasciencehistory/migration.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
66
*/
77
-- AlterTable
8-
ALTER TABLE "members"."memberDataScienceHistoryStats" ADD COLUMN "subTrackId" INTEGER NOT NULL;
8+
ALTER TABLE "memberDataScienceHistoryStats" ADD COLUMN "subTrackId" INTEGER NOT NULL;

src/scripts/migrate-dynamo-data.js

Lines changed: 122 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const path = require('path')
22
const fs = require('fs')
33
const readline = require('readline')
4-
const { concat, isArray, isBoolean, isEmpty, isEqual, isInteger, find, omit, pick, isNumber, forEach, map, uniqBy, isString } = require('lodash')
4+
const { concat, isArray, isBoolean, isEmpty, isEqual, isInteger, find, omit, pick, isNumber, forEach, map, uniqBy, isString, cloneDeep } = require('lodash')
55
const { v4: uuidv4 } = require('uuid')
66
const config = require('./config')
77
const prisma = require('../common/prisma').getClient()
@@ -795,7 +795,7 @@ function fixDynamoMemberStatHistoryData (dataItem) {
795795
let historyItems = item.history.map(item2 => ({
796796
...item2,
797797
ratingDate: _convert2Date(item2.ratingDate),
798-
subTrackId: item.id,
798+
subTrackId: item.id || DEFAULT_SRM_ID,
799799
subTrack: item.name,
800800
createdBy: CREATED_BY
801801
}))
@@ -1161,17 +1161,17 @@ async function updateMembersWithTraitsAndSkills (memberObj) {
11611161
})
11621162

11631163
if (existMaxRating) {
1164-
// update current maxRating
1165-
await tx.memberMaxRating.update({
1166-
where: {
1167-
id: existMaxRating.id
1168-
},
1169-
data: {
1170-
...memberObj.maxRating,
1171-
userId: memberObj.userId,
1172-
updatedBy: CREATED_BY
1173-
}
1174-
})
1164+
// update current maxRating
1165+
await tx.memberMaxRating.update({
1166+
where: {
1167+
id: existMaxRating.id
1168+
},
1169+
data: {
1170+
...memberObj.maxRating,
1171+
userId: memberObj.userId,
1172+
updatedBy: CREATED_BY
1173+
}
1174+
})
11751175
} else {
11761176
// create new maxRating
11771177
await tx.memberMaxRating.create({
@@ -1183,7 +1183,6 @@ async function updateMembersWithTraitsAndSkills (memberObj) {
11831183
}
11841184
})
11851185
}
1186-
11871186
}
11881187

11891188
if (memberObj.addresses && memberObj.addresses.length > 0) {
@@ -2025,6 +2024,107 @@ async function updateMemberStat (data, member, operatorId) {
20252024
})
20262025
}
20272026

2027+
/**
2028+
* import distribution stats.
2029+
*/
2030+
async function importDistributionStats () {
2031+
return prisma.$transaction(async (tx) => {
2032+
const total = await tx.memberMaxRating.count({})
2033+
console.log(`There are ${total} maxRating records`)
2034+
2035+
let current = 0
2036+
const uniqueMap = new Map()
2037+
const distributionStat = {
2038+
ratingRange0To099: 0,
2039+
ratingRange100To199: 0,
2040+
ratingRange200To299: 0,
2041+
ratingRange300To399: 0,
2042+
ratingRange400To499: 0,
2043+
ratingRange500To599: 0,
2044+
ratingRange600To699: 0,
2045+
ratingRange700To799: 0,
2046+
ratingRange800To899: 0,
2047+
ratingRange900To999: 0,
2048+
ratingRange1000To1099: 0,
2049+
ratingRange1100To1199: 0,
2050+
ratingRange1200To1299: 0,
2051+
ratingRange1300To1399: 0,
2052+
ratingRange1400To1499: 0,
2053+
ratingRange1500To1599: 0,
2054+
ratingRange1600To1699: 0,
2055+
ratingRange1700To1799: 0,
2056+
ratingRange1800To1899: 0,
2057+
ratingRange1900To1999: 0,
2058+
ratingRange2000To2099: 0,
2059+
ratingRange2100To2199: 0,
2060+
ratingRange2200To2299: 0,
2061+
ratingRange2300To2399: 0,
2062+
ratingRange2400To2499: 0,
2063+
ratingRange2500To2599: 0,
2064+
ratingRange2600To2699: 0,
2065+
ratingRange2700To2799: 0,
2066+
ratingRange2800To2899: 0,
2067+
ratingRange2900To2999: 0,
2068+
ratingRange3000To3099: 0,
2069+
ratingRange3100To3199: 0,
2070+
ratingRange3200To3299: 0,
2071+
ratingRange3300To3399: 0,
2072+
ratingRange3400To3499: 0,
2073+
ratingRange3500To3599: 0,
2074+
ratingRange3600To3699: 0,
2075+
ratingRange3700To3799: 0,
2076+
ratingRange3800To3899: 0,
2077+
ratingRange3900To3999: 0
2078+
}
2079+
2080+
while (current <= total) {
2081+
const records = await tx.memberMaxRating.findMany({
2082+
where: {},
2083+
orderBy: { id: 'asc' },
2084+
take: BATCH_SIZE,
2085+
skip: current
2086+
})
2087+
console.log(`Counting ${current} maxRating record`)
2088+
2089+
records.forEach(record => {
2090+
const mapKey = record.track.toUpperCase() + '-' + record.subTrack.toUpperCase()
2091+
let distributionValue
2092+
if (uniqueMap.has(mapKey)) {
2093+
distributionValue = uniqueMap.get(mapKey)
2094+
} else {
2095+
distributionValue = cloneDeep(distributionStat)
2096+
}
2097+
2098+
const idxVal = Math.floor(record.rating / 100)
2099+
const ratingKey = idxVal === 0 ? 'ratingRange0To099' : `ratingRange${idxVal}00To${idxVal}99`
2100+
distributionValue[ratingKey] += 1
2101+
uniqueMap.set(mapKey, distributionValue)
2102+
})
2103+
2104+
current += BATCH_SIZE
2105+
}
2106+
2107+
if (uniqueMap.size > 0) {
2108+
const dataArray = []
2109+
uniqueMap.forEach((value, key) => {
2110+
const tracks = key.split('-')
2111+
const data = {
2112+
...value,
2113+
track: tracks[0],
2114+
subTrack: tracks[1],
2115+
createdBy: CREATED_BY
2116+
}
2117+
dataArray.push(data)
2118+
})
2119+
await tx.distributionStats.createMany({
2120+
data: dataArray
2121+
})
2122+
}
2123+
2124+
console.log(`Finished counted ${uniqueMap.size} distributionStats records\n`)
2125+
})
2126+
}
2127+
20282128
async function main () {
20292129
console.log('This script is migrating data into DB')
20302130
console.log('The data number is huge, about 5,000,000 ~ 10,000,000 lines for each file')
@@ -2037,15 +2137,16 @@ async function main () {
20372137
console.log('3. Import Dynamo MemberStat')
20382138
console.log('4. Update ElasticSearch MemberStat')
20392139
console.log('5. Import Dynamo MemberStatHistory')
2140+
console.log('6. Import Distribution Stats')
20402141
console.log('')
20412142

20422143
const rl = readline.createInterface({
20432144
input: process.stdin,
20442145
output: process.stdout
20452146
})
20462147

2047-
rl.question('Please select your step to run (0-5): ', async (step) => {
2048-
if (step !== '0' && step !== '1' && step !== '2' && step !== '3' && step !== '4' && step !== '5') {
2148+
rl.question('Please select your step to run (0-6): ', async (step) => {
2149+
if (step !== '0' && step !== '1' && step !== '2' && step !== '3' && step !== '4' && step !== '5' && step !== '6') {
20492150
rl.close()
20502151
} else {
20512152
console.log(`Running step ${step} ...`)
@@ -2117,6 +2218,11 @@ async function main () {
21172218

21182219
const memberStatePrivateDynamoFilename = 'MemberStatsHistory_Private_dynamo_data.json'
21192220
await importDynamoMemberStatHistoryPrivate(memberStatePrivateDynamoFilename)
2221+
} else if (step === '6') {
2222+
console.log('Clearing distribution stats data...')
2223+
await prisma.distributionStats.deleteMany()
2224+
2225+
await importDistributionStats()
21202226
}
21212227
}
21222228

src/services/StatisticsService.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ const { v4: uuidv4 } = require('uuid')
1515
const DISTRIBUTION_FIELDS = ['track', 'subTrack', 'distribution', 'createdAt', 'updatedAt',
1616
'createdBy', 'updatedBy']
1717

18+
const DISTRIBUTION_FIELDS_NO_DATE = ['track', 'subTrack', 'distribution']
19+
1820
const HISTORY_STATS_FIELDS = ['userId', 'groupId', 'handle', 'handleLower', 'DEVELOP', 'DATA_SCIENCE',
1921
'createdAt', 'updatedAt', 'createdBy', 'updatedBy']
2022

@@ -29,7 +31,7 @@ const MEMBER_STATS_FIELDS = ['userId', 'groupId', 'handle', 'handleLower', 'maxR
2931
*/
3032
async function getDistribution (query) {
3133
// validate and parse query parameter
32-
const fields = helper.parseCommaSeparatedString(query.fields, DISTRIBUTION_FIELDS) || DISTRIBUTION_FIELDS
34+
const fields = helper.parseCommaSeparatedString(query.fields, DISTRIBUTION_FIELDS_NO_DATE) || DISTRIBUTION_FIELDS_NO_DATE
3335

3436
// find matched distribution records
3537
const prismaFilter = { where: {} }

0 commit comments

Comments
 (0)