mirror of
https://framagit.org/enfance-libre/statistiques
synced 2025-03-13 19:05:02 +00:00
refacotr: cache Config
This commit is contained in:
parent
a64557c5b4
commit
26592d44bc
4 changed files with 47 additions and 27 deletions
12
src/index.ts
12
src/index.ts
|
@ -50,12 +50,10 @@ function buildProcessOptions(): ProcessOptions {
|
||||||
console.log("Vérification schéma des bases Notion...");
|
console.log("Vérification schéma des bases Notion...");
|
||||||
await checkDbSchemas(notionClient);
|
await checkDbSchemas(notionClient);
|
||||||
|
|
||||||
console.log("Téléchargement des familles...");
|
console.log("Téléchargement des données...");
|
||||||
const doFetch = true;
|
const donneesFamillesBrutes = await fetchFamiliesWithEventsFromNotion(
|
||||||
|
notionClient
|
||||||
const donneesFamillesBrutes = doFetch
|
);
|
||||||
? await fetchFamiliesWithEventsFromNotion(notionClient)
|
|
||||||
: [];
|
|
||||||
|
|
||||||
console.log("Nettoyage des données brutes...");
|
console.log("Nettoyage des données brutes...");
|
||||||
const { familles, messages } = nettoyerDonneesFamilles(donneesFamillesBrutes);
|
const { familles, messages } = nettoyerDonneesFamilles(donneesFamillesBrutes);
|
||||||
|
@ -121,7 +119,7 @@ function buildProcessOptions(): ProcessOptions {
|
||||||
);
|
);
|
||||||
if (options.dryRun) {
|
if (options.dryRun) {
|
||||||
console.log(
|
console.log(
|
||||||
"Dry run => Pas de publication. Les stats sont écrite localement dans el-stats-xxx"
|
"Dry run => Pas de publication. Les stats sont écrites localement dans el-stats-xxx"
|
||||||
);
|
);
|
||||||
|
|
||||||
writeFileSync(
|
writeFileSync(
|
||||||
|
|
|
@ -10,7 +10,7 @@ import { relationPropertyToPageId } from "../utils/properties/relationPropertyTo
|
||||||
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";
|
||||||
import { queryAllDbResults } from "../utils/queryAllDbResults";
|
import { CacheConfig, queryAllDbResults } from "../utils/queryAllDbResults";
|
||||||
import { richTextPropertyToPlainText } from "../utils/text/richTextPropertyToPlainText";
|
import { richTextPropertyToPlainText } from "../utils/text/richTextPropertyToPlainText";
|
||||||
import {
|
import {
|
||||||
contactsDbId,
|
contactsDbId,
|
||||||
|
@ -36,36 +36,57 @@ type Departement = {
|
||||||
name: string;
|
name: string;
|
||||||
};
|
};
|
||||||
export async function fetchFamiliesWithEventsFromNotion(
|
export async function fetchFamiliesWithEventsFromNotion(
|
||||||
notionClient: Client
|
notionClient: Client,
|
||||||
|
cacheConfig: CacheConfig = { ttl: 3600 * 1000 }
|
||||||
): Promise<Famille[]> {
|
): Promise<Famille[]> {
|
||||||
const eventPages = (
|
const eventPages = (
|
||||||
await queryAllDbResults(notionClient, {
|
await queryAllDbResults(
|
||||||
database_id: familEventsDbId,
|
notionClient,
|
||||||
sorts: [{ property: "Date", direction: "ascending" }],
|
{
|
||||||
})
|
database_id: familEventsDbId,
|
||||||
|
sorts: [{ property: "Date", direction: "ascending" }],
|
||||||
|
},
|
||||||
|
{ cache: cacheConfig }
|
||||||
|
)
|
||||||
).filter(isFullPage);
|
).filter(isFullPage);
|
||||||
const familyPages = (
|
const familyPages = (
|
||||||
await queryAllDbResults(notionClient, {
|
await queryAllDbResults(
|
||||||
database_id: familiesDbId,
|
notionClient,
|
||||||
})
|
{
|
||||||
|
database_id: familiesDbId,
|
||||||
|
},
|
||||||
|
{ cache: cacheConfig }
|
||||||
|
)
|
||||||
).filter(isFullPage);
|
).filter(isFullPage);
|
||||||
|
|
||||||
const contactPages = (
|
const contactPages = (
|
||||||
await queryAllDbResults(notionClient, {
|
await queryAllDbResults(
|
||||||
database_id: contactsDbId,
|
notionClient,
|
||||||
})
|
{
|
||||||
|
database_id: contactsDbId,
|
||||||
|
},
|
||||||
|
{ cache: cacheConfig }
|
||||||
|
)
|
||||||
).filter(isFullPage);
|
).filter(isFullPage);
|
||||||
|
|
||||||
const missionsPages = (
|
const missionsPages = (
|
||||||
await queryAllDbResults(notionClient, {
|
await queryAllDbResults(
|
||||||
database_id: missionsDbId,
|
notionClient,
|
||||||
})
|
{
|
||||||
|
database_id: missionsDbId,
|
||||||
|
},
|
||||||
|
{ cache: cacheConfig }
|
||||||
|
)
|
||||||
).filter(isFullPage);
|
).filter(isFullPage);
|
||||||
|
|
||||||
const departementPages = (
|
const departementPages = (
|
||||||
await queryAllDbResults(notionClient, {
|
await queryAllDbResults(
|
||||||
database_id: departementsDbId,
|
notionClient,
|
||||||
})
|
{
|
||||||
|
database_id: departementsDbId,
|
||||||
|
},
|
||||||
|
{ cache: cacheConfig }
|
||||||
|
)
|
||||||
).filter(isFullPage);
|
).filter(isFullPage);
|
||||||
|
|
||||||
const departements: Departement[] = departementPages.map((page) => ({
|
const departements: Departement[] = departementPages.map((page) => ({
|
||||||
|
|
|
@ -11,5 +11,6 @@ export function relationPropertyToPageIds(
|
||||||
`Property ${propName} was expected to have type "relation" but got "${propValue.type}".`
|
`Property ${propName} was expected to have type "relation" but got "${propValue.type}".`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
propValue.re;
|
||||||
return propValue.relation.map((v) => v.id);
|
return propValue.relation.map((v) => v.id);
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,7 +13,7 @@ type QueryExtraOptions = {
|
||||||
cache: CacheConfig;
|
cache: CacheConfig;
|
||||||
};
|
};
|
||||||
|
|
||||||
type CacheConfig =
|
export type CacheConfig =
|
||||||
| boolean
|
| boolean
|
||||||
| {
|
| {
|
||||||
ttl: number;
|
ttl: number;
|
||||||
|
@ -22,7 +22,7 @@ type CacheConfig =
|
||||||
export async function queryAllDbResults(
|
export async function queryAllDbResults(
|
||||||
notion: Client,
|
notion: Client,
|
||||||
dbQuery: QueryDatabaseParameters,
|
dbQuery: QueryDatabaseParameters,
|
||||||
{ cache: cacheConfig }: QueryExtraOptions = { cache: { ttl: 3600 * 1000 } }
|
{ cache: cacheConfig }: QueryExtraOptions
|
||||||
): Promise<QueryDatabaseResponse["results"]> {
|
): Promise<QueryDatabaseResponse["results"]> {
|
||||||
const queryHash = createHash("sha1")
|
const queryHash = createHash("sha1")
|
||||||
.update(JSON.stringify(dbQuery))
|
.update(JSON.stringify(dbQuery))
|
||||||
|
|
Loading…
Add table
Reference in a new issue