@@ -338,11 +338,11 @@ export const getUserSeasonRanking = async function(prisma: PrismaClient, userId:
338338 return userRanking + 1 ;
339339} ;
340340
341- export interface NormalDistribution {
341+ export interface ScoreStatistics {
342342 dataPoints : number [ ] ;
343+ sum : number ;
343344 mean : number ;
344345 median : number ;
345- stdDev : number ;
346346 min : number ;
347347 max : number ;
348348} ;
@@ -424,21 +424,21 @@ export const getUserScores = async function(prisma: PrismaClient, userId: number
424424 return { userScores, totalScore } ;
425425} ;
426426
427- const getEmptyNormalDistribution = function ( ) : NormalDistribution {
427+ const getEmptyScoreStatistics = function ( ) : ScoreStatistics {
428428 return {
429429 dataPoints : [ ] ,
430+ sum : 0 ,
430431 mean : 0 ,
431432 median : 0 ,
432- stdDev : 0 ,
433433 min : 0 ,
434434 max : 0 ,
435435 } ;
436436} ;
437437
438- export const getScoresNormalDistribution = async function ( prisma : PrismaClient , coalitionId : number , untilDate : Date = new Date ( ) ) : Promise < NormalDistribution > {
438+ export const getScoreStatistics = async function ( prisma : PrismaClient , coalitionId : number , untilDate : Date = new Date ( ) ) : Promise < ScoreStatistics > {
439439 const bloc = await getBlocAtDate ( prisma , untilDate ) ;
440440 if ( ! bloc ) { // No season currently ongoing
441- return getEmptyNormalDistribution ( ) ;
441+ return getEmptyScoreStatistics ( ) ;
442442 }
443443 const scores = await prisma . codamCoalitionScore . groupBy ( {
444444 by : [ 'user_id' ] ,
@@ -459,22 +459,20 @@ export const getScoresNormalDistribution = async function(prisma: PrismaClient,
459459 } ,
460460 } ) ;
461461 if ( scores . length === 0 ) { // No scores for this coalition
462- return getEmptyNormalDistribution ( ) ;
462+ return getEmptyScoreStatistics ( ) ;
463463 }
464464 // console.log(scores);
465465 const scoresArray = scores . map ( s => s . _sum . amount ? s . _sum . amount : 0 ) ;
466466 const scoresSum = scoresArray . reduce ( ( a , b ) => a + b , 0 ) ;
467467 const scoresMean = scoresSum / scoresArray . length ;
468468 const scoresMedian = scoresArray [ Math . floor ( scoresArray . length / 2 ) ] ;
469- const scoresVariance = scoresArray . reduce ( ( a , b ) => a + Math . pow ( b - scoresMean , 2 ) , 0 ) / scoresArray . length ;
470- const scoresStdDev = Math . sqrt ( scoresVariance ) ;
471469 const scoresMin = Math . min ( ...scoresArray ) ;
472470 const scoresMax = Math . max ( ...scoresArray ) ;
473471 return {
474472 dataPoints : scoresArray ,
473+ sum : scoresSum ,
475474 mean : scoresMean ,
476475 median : scoresMedian ,
477- stdDev : scoresStdDev ,
478476 min : scoresMin ,
479477 max : scoresMax ,
480478 } ;
@@ -486,19 +484,14 @@ export interface CoalitionScore {
486484 totalPoints : number ;
487485 avgPoints : number ;
488486 medianPoints : number ;
489- stdDevPoints : number ;
490- minActivePoints : number ; // Minimum score for a user to be considered active
487+ totalMembers : number ;
491488 totalContributors : number ;
492- activeContributors : number ;
493489} ;
494490
495491export const getCoalitionScore = async function ( prisma : PrismaClient , coalitionId : number , atDateTime : Date = new Date ( ) ) : Promise < CoalitionScore > {
496- const normalDist = await getScoresNormalDistribution ( prisma , coalitionId , atDateTime ) ;
497- const minScore = Math . floor ( normalDist . mean - normalDist . stdDev ) ;
498- const activeScores = normalDist . dataPoints . filter ( s => s >= minScore ) ;
499- const fairScore = Math . floor ( activeScores . reduce ( ( a , b ) => a + b , 0 ) / activeScores . length ) ;
492+ const scoreStatistics = await getScoreStatistics ( prisma , coalitionId , atDateTime ) ;
500493
501- const totalActiveMembers = await prisma . intraCoalitionUser . count ( {
494+ const totalMembers = await prisma . intraCoalitionUser . count ( {
502495 where : {
503496 coalition_id : coalitionId ,
504497 user : {
@@ -513,16 +506,16 @@ export const getCoalitionScore = async function(prisma: PrismaClient, coalitionI
513506 } ,
514507 } ) ;
515508
509+ const totalContributors = Math . min ( scoreStatistics . dataPoints . length , totalMembers ) ;
510+
516511 return {
517512 coalition_id : coalitionId ,
518- totalPoints : normalDist . dataPoints . reduce ( ( a , b ) => a + b , 0 ) ,
519- avgPoints : normalDist . mean ,
520- medianPoints : normalDist . median ,
521- stdDevPoints : normalDist . stdDev ,
522- minActivePoints : minScore ,
523- score : Math . floor ( normalDist . mean ) , // fairScore can jump down too easily when there are a couple of really well scoring students on top of the leaderboard (scores dataset is not a normal distribution)
524- totalContributors : Math . min ( normalDist . dataPoints . length , totalActiveMembers ) ,
525- activeContributors : Math . min ( activeScores . length , totalActiveMembers ) ,
513+ totalPoints : scoreStatistics . sum ,
514+ avgPoints : scoreStatistics . mean ,
515+ medianPoints : scoreStatistics . median ,
516+ score : Math . floor ( scoreStatistics . sum / totalContributors ) ,
517+ totalMembers : totalMembers ,
518+ totalContributors : totalContributors ,
526519 } ;
527520} ;
528521
0 commit comments