From d17515b63ad313bf9d8b5a5ee347880b6d9a020b Mon Sep 17 00:00:00 2001 From: Shamzic Date: Wed, 11 Dec 2024 16:13:41 +0100 Subject: [PATCH 1/2] feat: ajoute stats annuelles et mensuelles du nombre de simulations en fonction des institutions --- backend/lib/stats/institutions.ts | 72 ++++++++++++++++++++++++++++++- 1 file changed, 70 insertions(+), 2 deletions(-) diff --git a/backend/lib/stats/institutions.ts b/backend/lib/stats/institutions.ts index eccf061ed5..551003032b 100644 --- a/backend/lib/stats/institutions.ts +++ b/backend/lib/stats/institutions.ts @@ -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 @@ -18,7 +27,7 @@ async function getSimulationCountPerEPCI(): Promise { { $match: { createdAt: { - $gt: sixMonthsAgo, + $gt: oneYearAgo, }, }, }, @@ -80,9 +89,67 @@ function patchEpciList(epci) { }) } +async function getMonthlySimulationCountPerEPCI(): Promise { + 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) => { @@ -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] || [], } }) } From 8140325085737af1362f0da9cdd29aa0fe844a57 Mon Sep 17 00:00:00 2001 From: Shamzic Date: Mon, 6 Jan 2025 17:37:32 +0100 Subject: [PATCH 2/2] =?UTF-8?q?fix:=20corrige=20le=20cas=20o=C3=B9=20nb=5F?= =?UTF-8?q?event=20est=20vide=20en=20local=20(funnel-service.ts)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/lib/stats/funnel-service.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/backend/lib/stats/funnel-service.ts b/backend/lib/stats/funnel-service.ts index a652e4aa8b..7dc8f60328 100644 --- a/backend/lib/stats/funnel-service.ts +++ b/backend/lib/stats/funnel-service.ts @@ -250,13 +250,15 @@ const getMatomoEventsData = async ( } const matomoEventData = await callMatomoAPI(piwikParameters) const dateKey = beginRange.format("YYYY-MM") + + const findEventCount = (label: string): number => { + const event = matomoEventData[dateKey]?.find((d) => d.label === label) + return event?.nb_events || 0 + } + return { - showAccompanimentCount: matomoEventData[dateKey].find( - (d) => d.label === "show-accompaniment-link" - )["nb_events"], - clickAccompanimentCount: matomoEventData[dateKey].find( - (d) => d.label === "click-accompaniment-link" - )["nb_events"], + showAccompanimentCount: findEventCount("show-accompaniment-link"), + clickAccompanimentCount: findEventCount("click-accompaniment-link"), } }