feat: formate les evol en une seul colonne
parent
962bdcde80
commit
3de3aa2662
|
@ -2,7 +2,7 @@ import { Client, isFullPage } from "@notionhq/client";
|
||||||
import { PageObjectResponse } from "@notionhq/client/build/src/api-endpoints";
|
import { PageObjectResponse } from "@notionhq/client/build/src/api-endpoints";
|
||||||
import { Family, FamilyEvent, StatutFamille } from "../../data/Family";
|
import { Family, FamilyEvent, StatutFamille } from "../../data/Family";
|
||||||
import { datePropertyToDate } from "../utils/properties/datePropertyToDate";
|
import { datePropertyToDate } from "../utils/properties/datePropertyToDate";
|
||||||
import { relationPropertyToPageId } from "../utils/properties/relationPropertyToPAgeId";
|
import { relationPropertyToPageId } from "../utils/properties/relationPropertyToPageId";
|
||||||
import { selectPropertyToText } from "../utils/properties/selectPropertyToText";
|
import { selectPropertyToText } from "../utils/properties/selectPropertyToText";
|
||||||
import { statusPropertyToText } from "../utils/properties/statusPropertyToText";
|
import { statusPropertyToText } from "../utils/properties/statusPropertyToText";
|
||||||
import { titlePropertyToText } from "../utils/properties/titlePropertyToText";
|
import { titlePropertyToText } from "../utils/properties/titlePropertyToText";
|
||||||
|
|
|
@ -1,5 +1,11 @@
|
||||||
import { Client, isFullPage } from "@notionhq/client";
|
import { Client, isFullPage } from "@notionhq/client";
|
||||||
import { ELPeriodStats, ELStats } from "../../statistiques/ELStats";
|
import { PageObjectResponse } from "@notionhq/client/build/src/api-endpoints";
|
||||||
|
import {
|
||||||
|
ELPeriodStats,
|
||||||
|
ELStats,
|
||||||
|
ValueWithEvol,
|
||||||
|
} from "../../statistiques/ELStats";
|
||||||
|
import { titlePropertyToText } from "../utils/properties/titlePropertyToText";
|
||||||
import { queryAllDbResults } from "../utils/queryAllDbResults";
|
import { queryAllDbResults } from "../utils/queryAllDbResults";
|
||||||
import { removeBlocks } from "../utils/removeBlocks";
|
import { removeBlocks } from "../utils/removeBlocks";
|
||||||
import { publishCurrentStats } from "./publishCurrentStats";
|
import { publishCurrentStats } from "./publishCurrentStats";
|
||||||
|
@ -30,42 +36,106 @@ async function publishPeriodStats(
|
||||||
database_id: periodStatsDbId,
|
database_id: periodStatsDbId,
|
||||||
})
|
})
|
||||||
).filter(isFullPage);
|
).filter(isFullPage);
|
||||||
await removeBlocks(
|
|
||||||
notionClient,
|
const indexedPeriodRows: { [period: string]: PageObjectResponse } =
|
||||||
periodRows.map((r) => r.id)
|
Object.fromEntries(
|
||||||
|
periodRows.map((r) => [titlePropertyToText(r.properties, "Période"), r])
|
||||||
|
);
|
||||||
|
|
||||||
|
const indexedPeriodStats = Object.fromEntries(
|
||||||
|
stats.map((stat) => [stat.periodId, stat])
|
||||||
);
|
);
|
||||||
|
|
||||||
for (const stat of stats) {
|
const rowIdsToDelete = Object.entries(indexedPeriodRows)
|
||||||
|
.filter(
|
||||||
|
([periodId]) =>
|
||||||
|
!Object.prototype.hasOwnProperty.call(indexedPeriodStats, periodId)
|
||||||
|
)
|
||||||
|
.map(([, row]) => row.id);
|
||||||
|
|
||||||
|
const periodIdsToUpdate = Object.entries(indexedPeriodRows)
|
||||||
|
.filter(([periodId]) =>
|
||||||
|
Object.prototype.hasOwnProperty.call(indexedPeriodStats, periodId)
|
||||||
|
)
|
||||||
|
.map(([periodId]) => periodId);
|
||||||
|
|
||||||
|
const periodIdsToCreate = Object.entries(indexedPeriodStats)
|
||||||
|
.filter(
|
||||||
|
([periodId]) =>
|
||||||
|
!Object.prototype.hasOwnProperty.call(indexedPeriodRows, periodId)
|
||||||
|
)
|
||||||
|
.map(([periodId]) => periodId);
|
||||||
|
|
||||||
|
// Delete rows to delte
|
||||||
|
await removeBlocks(notionClient, rowIdsToDelete);
|
||||||
|
|
||||||
|
// Create rows to create
|
||||||
|
for (const periodId of periodIdsToCreate) {
|
||||||
|
const stat = indexedPeriodStats[periodId];
|
||||||
await notionClient.pages.create({
|
await notionClient.pages.create({
|
||||||
parent: {
|
parent: {
|
||||||
database_id: periodStatsDbId,
|
database_id: periodStatsDbId,
|
||||||
},
|
},
|
||||||
properties: {
|
properties: buildRowPropertiesForUpsert(stat),
|
||||||
Période: {
|
});
|
||||||
title: [
|
}
|
||||||
{
|
|
||||||
text: {
|
// Update rows
|
||||||
content: stat.periodId,
|
for (const periodId of periodIdsToUpdate) {
|
||||||
},
|
const stat = indexedPeriodStats[periodId];
|
||||||
},
|
const row = indexedPeriodRows[periodId];
|
||||||
],
|
await notionClient.pages.update({
|
||||||
},
|
page_id: row.id,
|
||||||
"Nb Famille Résistante": numberProp(stat.nbFamilleResistantes),
|
properties: buildRowPropertiesForUpsert(stat),
|
||||||
"Nb Famille Résistante - Evol": numberProp(
|
|
||||||
stat.nbFamilleResistantesEvol
|
|
||||||
),
|
|
||||||
"Nb Famille Résistante - Evol %": numberProp(
|
|
||||||
stat.nbFamilleResistantesEvolPercent
|
|
||||||
),
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function numberProp(n: number): {
|
function buildRowPropertiesForUpsert(stat: ELPeriodStats) {
|
||||||
number: number;
|
|
||||||
} {
|
|
||||||
return {
|
return {
|
||||||
number: n,
|
Période: {
|
||||||
|
title: [
|
||||||
|
{
|
||||||
|
text: {
|
||||||
|
content: stat.periodId,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
"Nb Famille Résistante": valueWithEvolProp(stat.nbFamilleResistantes),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function valueWithEvolProp(n: ValueWithEvol) {
|
||||||
|
const formatted = formatValueWithEvol(n);
|
||||||
|
return {
|
||||||
|
rich_text: [
|
||||||
|
{
|
||||||
|
text: {
|
||||||
|
content: formatted,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
function formatValueWithEvol(n: ValueWithEvol): string {
|
||||||
|
const value = n.value.toLocaleString("fr-FR", {
|
||||||
|
useGrouping: false,
|
||||||
|
maximumFractionDigits: 2,
|
||||||
|
});
|
||||||
|
if (isNaN(n.evol)) {
|
||||||
|
return value;
|
||||||
|
} else {
|
||||||
|
const evol = n.evol.toLocaleString("fr-FR", {
|
||||||
|
useGrouping: false,
|
||||||
|
maximumFractionDigits: 2,
|
||||||
|
signDisplay: "always",
|
||||||
|
});
|
||||||
|
const evolPercent = Math.round(n.evolPercent).toLocaleString("fr-FR", {
|
||||||
|
useGrouping: false,
|
||||||
|
signDisplay: "always",
|
||||||
|
});
|
||||||
|
|
||||||
|
return `${value} (${evol} | ${evolPercent}%)`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -13,7 +13,11 @@ export type ELStats = {
|
||||||
|
|
||||||
export type ELPeriodStats = {
|
export type ELPeriodStats = {
|
||||||
periodId: string;
|
periodId: string;
|
||||||
nbFamilleResistantes: number;
|
nbFamilleResistantes: ValueWithEvol;
|
||||||
nbFamilleResistantesEvol: number;
|
};
|
||||||
nbFamilleResistantesEvolPercent: number;
|
|
||||||
|
export type ValueWithEvol = {
|
||||||
|
value: number;
|
||||||
|
evol: number;
|
||||||
|
evolPercent: number;
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import { Family, isResistantOverPeriod } from "../data/Family";
|
import { Family, isResistantOverPeriod } from "../data/Family";
|
||||||
import { IdentifiedPeriod } from "../period/IdentifiedPeriod";
|
import { IdentifiedPeriod } from "../period/IdentifiedPeriod";
|
||||||
import { ELPeriodStats } from "./ELStats";
|
import { ELPeriodStats, ValueWithEvol } from "./ELStats";
|
||||||
|
|
||||||
export function computeELPeriodStats(
|
export function computeELPeriodStats(
|
||||||
familles: Family[],
|
familles: Family[],
|
||||||
|
@ -15,14 +15,9 @@ export function computeELPeriodStats(
|
||||||
|
|
||||||
const stats: ELPeriodStats = {
|
const stats: ELPeriodStats = {
|
||||||
periodId: period.id,
|
periodId: period.id,
|
||||||
nbFamilleResistantes: resistantsCount,
|
nbFamilleResistantes: valueWithEvol(
|
||||||
nbFamilleResistantesEvol: evol(
|
|
||||||
resistantsCount,
|
resistantsCount,
|
||||||
previousELPeriodStats?.nbFamilleResistantes
|
previousELPeriodStats?.nbFamilleResistantes.value
|
||||||
),
|
|
||||||
nbFamilleResistantesEvolPercent: evolPercent(
|
|
||||||
resistantsCount,
|
|
||||||
previousELPeriodStats?.nbFamilleResistantes
|
|
||||||
),
|
),
|
||||||
};
|
};
|
||||||
periodStats.push(stats);
|
periodStats.push(stats);
|
||||||
|
@ -31,10 +26,22 @@ export function computeELPeriodStats(
|
||||||
return periodStats;
|
return periodStats;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function valueWithEvol(
|
||||||
|
value: number,
|
||||||
|
previous: number | undefined
|
||||||
|
): ValueWithEvol {
|
||||||
|
return {
|
||||||
|
value: value,
|
||||||
|
evol: evol(value, previous),
|
||||||
|
evolPercent: evolPercent(value, previous),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
function evolPercent(current: number, previous: number | undefined): number {
|
function evolPercent(current: number, previous: number | undefined): number {
|
||||||
if (previous === undefined) return NaN;
|
if (previous === undefined) return NaN;
|
||||||
return (100 * evol(current, previous)) / previous;
|
return (100 * evol(current, previous)) / previous;
|
||||||
}
|
}
|
||||||
|
|
||||||
function evol(current: number, previous: number | undefined): number {
|
function evol(current: number, previous: number | undefined): number {
|
||||||
if (previous === undefined) return NaN;
|
if (previous === undefined) return NaN;
|
||||||
return current - previous;
|
return current - previous;
|
||||||
|
|
Loading…
Reference in New Issue