Skip to content

Commit

Permalink
Merge pull request #158 from betagouv/1494-prendre-en-compte-les-tele…
Browse files Browse the repository at this point in the history
…serviceprefill-dans-les-stats

Fusionne les évènements apparentés
  • Loading branch information
baptou12 authored Nov 20, 2023
2 parents 8aad17b + 9d3340c commit 0467d3d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions services/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export const EventTypeCategoryMapping = {
cat: EventCategories.ACTIONABLE,
name: "Téléservice",
color: "#7f7f7f",
relatedCategories: ["teleservicePrefill"],
},
"link-ineligible": {
cat: EventCategories.INELIGIBLE_ACTIONABLE,
Expand Down
36 changes: 35 additions & 1 deletion services/fetch.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { EventTypeCategoryMapping } from "./config.js"

export default class Fetch {
static async getJSON(url) {
const data = await fetch(url)
Expand Down Expand Up @@ -64,7 +66,39 @@ export default class Fetch {

static async getRecorderStatistics(startAt) {
const url = `${process.env.recorderStatisticsURL}/benefits?start_at=${startAt}`
const recorderStatistics = await this.getJSON(url)
const recorderStatistics = this.aggregateRelatedEventStatistics(
await this.getJSON(url),
)

return recorderStatistics
}

static aggregateRelatedEventStatistics(recorderStatistics) {
const initializeEventCount = (events, eventType) => {
events[eventType] = events[eventType] || 0
}

const sumRelatedCategoryEvents = (events, relatedCategories) =>
relatedCategories.reduce((sum, category) => {
initializeEventCount(events, category)
return sum + events[category]
}, 0)

for (const [eventType, eventCategory] of Object.entries(
EventTypeCategoryMapping,
)) {
if (!eventCategory.relatedCategories) {
continue
}

for (const statistic of recorderStatistics) {
initializeEventCount(statistic.events, eventType)
statistic.events[eventType] += sumRelatedCategoryEvents(
statistic.events,
eventCategory.relatedCategories,
)
}
}

return recorderStatistics
}
Expand Down

0 comments on commit 0467d3d

Please sign in to comment.