Skip to content

Commit

Permalink
feat: ajoute stats annuelles et mensuelles du nombre de simulations e…
Browse files Browse the repository at this point in the history
…n fonction des institutions
  • Loading branch information
Shamzic committed Dec 11, 2024
1 parent 35a813d commit d17515b
Showing 1 changed file with 70 additions and 2 deletions.
72 changes: 70 additions & 2 deletions backend/lib/stats/institutions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,16 @@ import { StandardBenefit } from "../../../data/types/benefits.js"

// Note: for now, only EPCI are supported

const sixMonthsAgo = dayjs().subtract(6, "month").toDate()
const oneYearAgo = dayjs().subtract(1, "year").toDate()

interface MonthlyCount {
month: string
count: number
}

interface EPCIStats {
[key: string]: MonthlyCount[]
}

interface Count {
[key: string]: number
Expand All @@ -18,7 +27,7 @@ async function getSimulationCountPerEPCI(): Promise<Count> {
{
$match: {
createdAt: {
$gt: sixMonthsAgo,
$gt: oneYearAgo,
},
},
},
Expand Down Expand Up @@ -80,9 +89,67 @@ function patchEpciList(epci) {
})
}

async function getMonthlySimulationCountPerEPCI(): Promise<EPCIStats> {
const simulationCount = await Simulations.aggregate([
{
$match: {
createdAt: {
$gt: oneYearAgo,
},
},
},
{
$unwind: "$answers.all",
},
{
$match: {
"answers.all.entityName": "menage",
"answers.all.fieldName": "depcom",
},
},
{
$addFields: {
epciCode: "$answers.all.value._epci",
},
},
{
$group: {
_id: {
epci: "$epciCode",
month: { $dateToString: { format: "%Y-%m", date: "$createdAt" } },
},
count: { $sum: 1 },
},
},
{
$match: {
"_id.epci": { $ne: null },
},
},
{
$sort: {
"_id.month": 1,
},
},
])

return simulationCount.reduce((stats: EPCIStats, result) => {
const { epci, month } = result._id
if (!stats[epci]) {
stats[epci] = []
}
stats[epci].push({
month,
count: result.count,
})
return stats
}, {} as EPCIStats)
}

export default async function getInstitutionsData() {
const epciPatched = patchEpciList(epciList)
const simulationNumberPerEPCI = await getSimulationCountPerEPCI()
const monthlySimulationsPerEPCI = await getMonthlySimulationCountPerEPCI()
const benefitCountPerEPCI = getBenefitCountPerEPCI()

return epciPatched.map((epci) => {
Expand All @@ -93,6 +160,7 @@ export default async function getInstitutionsData() {
population: epci.populationTotale,
simulationCount: simulationNumberPerEPCI[epci.code] || 0,
benefitCount: benefitCountPerEPCI[epci.code] || 0,
monthlySimulations: monthlySimulationsPerEPCI[epci.code] || [],
}
})
}

0 comments on commit d17515b

Please sign in to comment.