feat: ajout de nbFamillesResistantesActuellesParDepartement
parent
1d28784f74
commit
dcc396af82
|
@ -21,6 +21,7 @@ export type Famille = Readonly<{
|
||||||
Sortie: Date | null;
|
Sortie: Date | null;
|
||||||
Penal: StatutPenal;
|
Penal: StatutPenal;
|
||||||
Social: StatutSocial;
|
Social: StatutSocial;
|
||||||
|
Departement: string | null;
|
||||||
// sorted by date asc
|
// sorted by date asc
|
||||||
Evenements: EvenementFamille[];
|
Evenements: EvenementFamille[];
|
||||||
DerniereModification: Date;
|
DerniereModification: Date;
|
||||||
|
|
|
@ -1 +1,2 @@
|
||||||
export const familEventsDbId: string = "c4d434b4603c4481a4d445618ecdf999";
|
export const familEventsDbId: string = "c4d434b4603c4481a4d445618ecdf999";
|
||||||
|
export const departementsDbId: string = "787779be614544069f609bf9d2a7cd15";
|
||||||
|
|
|
@ -12,7 +12,7 @@ import { statusPropertyToText } from "../utils/properties/statusPropertyToText";
|
||||||
import { titlePropertyToText } from "../utils/properties/titlePropertyToText";
|
import { titlePropertyToText } from "../utils/properties/titlePropertyToText";
|
||||||
import { queryAllDbResults } from "../utils/queryAllDbResults";
|
import { queryAllDbResults } from "../utils/queryAllDbResults";
|
||||||
import { richTextPropertyToPlainText } from "../utils/text/richTextPropertyToPlainText";
|
import { richTextPropertyToPlainText } from "../utils/text/richTextPropertyToPlainText";
|
||||||
import { familEventsDbId } from "./dbIds";
|
import { departementsDbId, familEventsDbId } from "./dbIds";
|
||||||
import {
|
import {
|
||||||
propContexteEntree,
|
propContexteEntree,
|
||||||
familiesDbId,
|
familiesDbId,
|
||||||
|
@ -23,6 +23,10 @@ import {
|
||||||
import { StatutPenal } from "../../data/StatutPenal";
|
import { StatutPenal } from "../../data/StatutPenal";
|
||||||
import { StatutSocial } from "../../data/StatutSocial";
|
import { StatutSocial } from "../../data/StatutSocial";
|
||||||
|
|
||||||
|
type Departement = {
|
||||||
|
notionId: string;
|
||||||
|
name: string;
|
||||||
|
};
|
||||||
export async function fetchFamiliesWithEventsFromNotion(
|
export async function fetchFamiliesWithEventsFromNotion(
|
||||||
notionClient: Client
|
notionClient: Client
|
||||||
): Promise<Famille[]> {
|
): Promise<Famille[]> {
|
||||||
|
@ -38,13 +42,24 @@ export async function fetchFamiliesWithEventsFromNotion(
|
||||||
})
|
})
|
||||||
).filter(isFullPage);
|
).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) => {
|
const familyEvents = eventPages.map((pageObjectResponse) => {
|
||||||
return buildFamilyEvent(pageObjectResponse);
|
return buildFamilyEvent(pageObjectResponse);
|
||||||
});
|
});
|
||||||
|
|
||||||
const familles: Famille[] = await Promise.all(
|
const familles: Famille[] = await Promise.all(
|
||||||
familyPages.map((pageObjectResponse) => {
|
familyPages.map((pageObjectResponse) => {
|
||||||
return buildFamily(pageObjectResponse, familyEvents);
|
return buildFamily(pageObjectResponse, familyEvents, departements);
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
return familles;
|
return familles;
|
||||||
|
@ -69,10 +84,15 @@ function buildFamilyEvent(page: PageObjectResponse): EvenementFamille {
|
||||||
|
|
||||||
function buildFamily(
|
function buildFamily(
|
||||||
page: PageObjectResponse,
|
page: PageObjectResponse,
|
||||||
familyEvents: EvenementFamille[]
|
familyEvents: EvenementFamille[],
|
||||||
|
departements: Departement[]
|
||||||
): Famille {
|
): Famille {
|
||||||
const pageProperties = page.properties;
|
const pageProperties = page.properties;
|
||||||
|
|
||||||
|
const departementId = relationPropertyToPageId(pageProperties, "Département");
|
||||||
|
const departement = departementId
|
||||||
|
? departements.find((d) => d.notionId === departementId)
|
||||||
|
: null;
|
||||||
const family: Famille = {
|
const family: Famille = {
|
||||||
notionId: page.id,
|
notionId: page.id,
|
||||||
Titre: titlePropertyToText(pageProperties, ""),
|
Titre: titlePropertyToText(pageProperties, ""),
|
||||||
|
@ -84,6 +104,7 @@ function buildFamily(
|
||||||
Integration: datePropertyToDate(pageProperties, "Intégration"),
|
Integration: datePropertyToDate(pageProperties, "Intégration"),
|
||||||
Sortie: datePropertyToDate(pageProperties, "Sortie"),
|
Sortie: datePropertyToDate(pageProperties, "Sortie"),
|
||||||
Evenements: familyEvents.filter((fe) => fe.notionIdFamille === page.id),
|
Evenements: familyEvents.filter((fe) => fe.notionIdFamille === page.id),
|
||||||
|
Departement: departement?.name || null,
|
||||||
DerniereModification: datePropertyToDate(
|
DerniereModification: datePropertyToDate(
|
||||||
pageProperties,
|
pageProperties,
|
||||||
propDerniereModification
|
propDerniereModification
|
||||||
|
|
|
@ -4,10 +4,14 @@ export const statsGeneralesDesc = {
|
||||||
label: "Stats Générales",
|
label: "Stats Générales",
|
||||||
stats: {
|
stats: {
|
||||||
nbFamillesResistantesActuelles: {
|
nbFamillesResistantesActuelles: {
|
||||||
label: "Nb Familles Résistantes actuelles",
|
label: "Nb Familles actuellement résistantes",
|
||||||
},
|
},
|
||||||
nbFamillesResistantesActuellesOuPassees: {
|
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: {
|
dureeResistanceMoyenne: {
|
||||||
label: "Durée moyenne de résistance",
|
label: "Durée moyenne de résistance",
|
||||||
|
|
|
@ -9,13 +9,13 @@ import {
|
||||||
import { average } from "../../../utils/math/average";
|
import { average } from "../../../utils/math/average";
|
||||||
import { median } from "../../../utils/math/median";
|
import { median } from "../../../utils/math/median";
|
||||||
import { StatsGenerales } from "./StatsGenerales";
|
import { StatsGenerales } from "./StatsGenerales";
|
||||||
import { countBy } from "lodash";
|
import _, { countBy } from "lodash";
|
||||||
|
|
||||||
export function computeStatsGenerales(familles: Famille[]): StatsGenerales {
|
export function computeStatsGenerales(familles: Famille[]): StatsGenerales {
|
||||||
const famillesResistantesOrEx = familles.filter(
|
const famillesResistantesOrEx = familles.filter(
|
||||||
(f) => isResistant(f) || isExResistant(f)
|
(f) => isResistant(f) || isExResistant(f)
|
||||||
);
|
);
|
||||||
const famillesresistantes = familles.filter((f) => isResistant(f));
|
const famillesResistantes = familles.filter((f) => isResistant(f));
|
||||||
|
|
||||||
const dureesResistances = famillesResistantesOrEx.map(
|
const dureesResistances = famillesResistantesOrEx.map(
|
||||||
(f) => dureeResistanceInDays(f)!
|
(f) => dureeResistanceInDays(f)!
|
||||||
|
@ -26,21 +26,30 @@ export function computeStatsGenerales(familles: Famille[]): StatsGenerales {
|
||||||
isIntegration(f) && differenceInDays(now, f.DerniereModification) <= 30
|
isIntegration(f) && differenceInDays(now, f.DerniereModification) <= 30
|
||||||
);
|
);
|
||||||
const statsGenerales: StatsGenerales = {
|
const statsGenerales: StatsGenerales = {
|
||||||
nbFamillesResistantesActuelles: famillesresistantes.length,
|
nbFamillesResistantesActuelles: famillesResistantes.length,
|
||||||
nbFamillesResistantesActuellesOuPassees: famillesResistantesOrEx.length,
|
nbFamillesResistantesActuellesOuPassees: famillesResistantesOrEx.length,
|
||||||
|
|
||||||
|
nbFamillesResistantesActuellesParDepartement: sortByKey(
|
||||||
|
countBy(famillesResistantes, (f) => f.Departement)
|
||||||
|
),
|
||||||
dureeResistanceMedianne: median(dureesResistances),
|
dureeResistanceMedianne: median(dureesResistances),
|
||||||
dureeResistanceMoyenne: average(dureesResistances),
|
dureeResistanceMoyenne: average(dureesResistances),
|
||||||
|
|
||||||
nbFamillesParContexteDEntree: countBy(
|
nbFamillesParContexteDEntree: sortByKey(
|
||||||
famillesResistantesOrEx,
|
countBy(famillesResistantesOrEx, (f) => f.ContexteEntree)
|
||||||
(f) => f.ContexteEntree
|
|
||||||
),
|
),
|
||||||
nbFicheIntegrationActiviteRecente:
|
nbFicheIntegrationActiviteRecente:
|
||||||
fichesIntegrationRecementModifiees.length,
|
fichesIntegrationRecementModifiees.length,
|
||||||
nbFicheIntegrationParStatuts: countBy(
|
nbFicheIntegrationParStatuts: sortByKey(
|
||||||
familles.filter((f) => isIntegration(f)),
|
countBy(
|
||||||
(f) => f.Statut
|
familles.filter((f) => isIntegration(f)),
|
||||||
|
(f) => f.Statut
|
||||||
|
)
|
||||||
),
|
),
|
||||||
};
|
};
|
||||||
return statsGenerales;
|
return statsGenerales;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function sortByKey(record: Record<string, number>): Record<string, number> {
|
||||||
|
return _(record).toPairs().sortBy(0).fromPairs().value();
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue