feat: ajout de nbFamillesResistantesActuellesParDepartement

wip-related-pages
Sébastien Arod 2024-10-14 17:42:57 +02:00
parent 1d28784f74
commit dcc396af82
5 changed files with 50 additions and 14 deletions

View File

@ -21,6 +21,7 @@ export type Famille = Readonly<{
Sortie: Date | null;
Penal: StatutPenal;
Social: StatutSocial;
Departement: string | null;
// sorted by date asc
Evenements: EvenementFamille[];
DerniereModification: Date;

View File

@ -1 +1,2 @@
export const familEventsDbId: string = "c4d434b4603c4481a4d445618ecdf999";
export const departementsDbId: string = "787779be614544069f609bf9d2a7cd15";

View File

@ -12,7 +12,7 @@ import { statusPropertyToText } from "../utils/properties/statusPropertyToText";
import { titlePropertyToText } from "../utils/properties/titlePropertyToText";
import { queryAllDbResults } from "../utils/queryAllDbResults";
import { richTextPropertyToPlainText } from "../utils/text/richTextPropertyToPlainText";
import { familEventsDbId } from "./dbIds";
import { departementsDbId, familEventsDbId } from "./dbIds";
import {
propContexteEntree,
familiesDbId,
@ -23,6 +23,10 @@ import {
import { StatutPenal } from "../../data/StatutPenal";
import { StatutSocial } from "../../data/StatutSocial";
type Departement = {
notionId: string;
name: string;
};
export async function fetchFamiliesWithEventsFromNotion(
notionClient: Client
): Promise<Famille[]> {
@ -38,13 +42,24 @@ export async function fetchFamiliesWithEventsFromNotion(
})
).filter(isFullPage);
const departementPages = (
await queryAllDbResults(notionClient, {
database_id: departementsDbId,
})
).filter(isFullPage);
const departements: Departement[] = departementPages.map((page) => ({
notionId: page.id,
name: titlePropertyToText(page.properties, "Nom"),
}));
const familyEvents = eventPages.map((pageObjectResponse) => {
return buildFamilyEvent(pageObjectResponse);
});
const familles: Famille[] = await Promise.all(
familyPages.map((pageObjectResponse) => {
return buildFamily(pageObjectResponse, familyEvents);
return buildFamily(pageObjectResponse, familyEvents, departements);
})
);
return familles;
@ -69,10 +84,15 @@ function buildFamilyEvent(page: PageObjectResponse): EvenementFamille {
function buildFamily(
page: PageObjectResponse,
familyEvents: EvenementFamille[]
familyEvents: EvenementFamille[],
departements: Departement[]
): Famille {
const pageProperties = page.properties;
const departementId = relationPropertyToPageId(pageProperties, "Département");
const departement = departementId
? departements.find((d) => d.notionId === departementId)
: null;
const family: Famille = {
notionId: page.id,
Titre: titlePropertyToText(pageProperties, ""),
@ -84,6 +104,7 @@ function buildFamily(
Integration: datePropertyToDate(pageProperties, "Intégration"),
Sortie: datePropertyToDate(pageProperties, "Sortie"),
Evenements: familyEvents.filter((fe) => fe.notionIdFamille === page.id),
Departement: departement?.name || null,
DerniereModification: datePropertyToDate(
pageProperties,
propDerniereModification

View File

@ -4,10 +4,14 @@ export const statsGeneralesDesc = {
label: "Stats Générales",
stats: {
nbFamillesResistantesActuelles: {
label: "Nb Familles Résistantes actuelles",
label: "Nb Familles actuellement résistantes",
},
nbFamillesResistantesActuellesOuPassees: {
label: "Nb Familles Résistantes actuelles ou passées",
label: "Nb Familles résistantes actuellement ou par le passé",
},
nbFamillesResistantesActuellesParDepartement: {
label: "Nb familles actuellement résistante par département",
type: "multi",
},
dureeResistanceMoyenne: {
label: "Durée moyenne de résistance",

View File

@ -9,13 +9,13 @@ import {
import { average } from "../../../utils/math/average";
import { median } from "../../../utils/math/median";
import { StatsGenerales } from "./StatsGenerales";
import { countBy } from "lodash";
import _, { countBy } from "lodash";
export function computeStatsGenerales(familles: Famille[]): StatsGenerales {
const famillesResistantesOrEx = familles.filter(
(f) => isResistant(f) || isExResistant(f)
);
const famillesresistantes = familles.filter((f) => isResistant(f));
const famillesResistantes = familles.filter((f) => isResistant(f));
const dureesResistances = famillesResistantesOrEx.map(
(f) => dureeResistanceInDays(f)!
@ -26,21 +26,30 @@ export function computeStatsGenerales(familles: Famille[]): StatsGenerales {
isIntegration(f) && differenceInDays(now, f.DerniereModification) <= 30
);
const statsGenerales: StatsGenerales = {
nbFamillesResistantesActuelles: famillesresistantes.length,
nbFamillesResistantesActuelles: famillesResistantes.length,
nbFamillesResistantesActuellesOuPassees: famillesResistantesOrEx.length,
nbFamillesResistantesActuellesParDepartement: sortByKey(
countBy(famillesResistantes, (f) => f.Departement)
),
dureeResistanceMedianne: median(dureesResistances),
dureeResistanceMoyenne: average(dureesResistances),
nbFamillesParContexteDEntree: countBy(
famillesResistantesOrEx,
(f) => f.ContexteEntree
nbFamillesParContexteDEntree: sortByKey(
countBy(famillesResistantesOrEx, (f) => f.ContexteEntree)
),
nbFicheIntegrationActiviteRecente:
fichesIntegrationRecementModifiees.length,
nbFicheIntegrationParStatuts: countBy(
familles.filter((f) => isIntegration(f)),
(f) => f.Statut
nbFicheIntegrationParStatuts: sortByKey(
countBy(
familles.filter((f) => isIntegration(f)),
(f) => f.Statut
)
),
};
return statsGenerales;
}
function sortByKey(record: Record<string, number>): Record<string, number> {
return _(record).toPairs().sortBy(0).fromPairs().value();
}