import { Db } from 'mongodb' ; import { computeTermAggregationByYear } from './generic' ; import { SurveyConfig } from '../types' ; import { Filters } from '../filters' ; export async function computeHappinessByYear ( db , survey , id , filters ? ) { const happinessByYear = await computeTermAggregationByYear ( db , survey , `happiness.${ id }` , { filters , sort : 'id' , order : 1 } ) ; happinessByYear . forEach ( ( bucket ) => { const totalScore = bucket . buckets . reduce ( ( acc , subBucket ) => { return acc + subBucket . id * subBucket . count ; } , 0 ) ; bucket . mean = Math . round ( ( totalScore / bucket . total ) * 10 ) / 10 + 1 ; } ) ; return happinessByYear ; }