mirror of
https://framagit.org/enfance-libre/statistiques
synced 2025-03-12 22:05:14 +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...");
|
||||
await checkDbSchemas(notionClient);
|
||||
|
||||
console.log("Téléchargement des familles...");
|
||||
const doFetch = true;
|
||||
|
||||
const donneesFamillesBrutes = doFetch
|
||||
? await fetchFamiliesWithEventsFromNotion(notionClient)
|
||||
: [];
|
||||
console.log("Téléchargement des données...");
|
||||
const donneesFamillesBrutes = await fetchFamiliesWithEventsFromNotion(
|
||||
notionClient
|
||||
);
|
||||
|
||||
console.log("Nettoyage des données brutes...");
|
||||
const { familles, messages } = nettoyerDonneesFamilles(donneesFamillesBrutes);
|
||||
|
@ -121,7 +119,7 @@ function buildProcessOptions(): ProcessOptions {
|
|||
);
|
||||
if (options.dryRun) {
|
||||
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(
|
||||
|
|
|
@ -10,7 +10,7 @@ import { relationPropertyToPageId } from "../utils/properties/relationPropertyTo
|
|||
import { selectPropertyToText } from "../utils/properties/selectPropertyToText";
|
||||
import { statusPropertyToText } from "../utils/properties/statusPropertyToText";
|
||||
import { titlePropertyToText } from "../utils/properties/titlePropertyToText";
|
||||
import { queryAllDbResults } from "../utils/queryAllDbResults";
|
||||
import { CacheConfig, queryAllDbResults } from "../utils/queryAllDbResults";
|
||||
import { richTextPropertyToPlainText } from "../utils/text/richTextPropertyToPlainText";
|
||||
import {
|
||||
contactsDbId,
|
||||
|
@ -36,36 +36,57 @@ type Departement = {
|
|||
name: string;
|
||||
};
|
||||
export async function fetchFamiliesWithEventsFromNotion(
|
||||
notionClient: Client
|
||||
notionClient: Client,
|
||||
cacheConfig: CacheConfig = { ttl: 3600 * 1000 }
|
||||
): Promise<Famille[]> {
|
||||
const eventPages = (
|
||||
await queryAllDbResults(notionClient, {
|
||||
database_id: familEventsDbId,
|
||||
sorts: [{ property: "Date", direction: "ascending" }],
|
||||
})
|
||||
await queryAllDbResults(
|
||||
notionClient,
|
||||
{
|
||||
database_id: familEventsDbId,
|
||||
sorts: [{ property: "Date", direction: "ascending" }],
|
||||
},
|
||||
{ cache: cacheConfig }
|
||||
)
|
||||
).filter(isFullPage);
|
||||
const familyPages = (
|
||||
await queryAllDbResults(notionClient, {
|
||||
database_id: familiesDbId,
|
||||
})
|
||||
await queryAllDbResults(
|
||||
notionClient,
|
||||
{
|
||||
database_id: familiesDbId,
|
||||
},
|
||||
{ cache: cacheConfig }
|
||||
)
|
||||
).filter(isFullPage);
|
||||
|
||||
const contactPages = (
|
||||
await queryAllDbResults(notionClient, {
|
||||
database_id: contactsDbId,
|
||||
})
|
||||
await queryAllDbResults(
|
||||
notionClient,
|
||||
{
|
||||
database_id: contactsDbId,
|
||||
},
|
||||
{ cache: cacheConfig }
|
||||
)
|
||||
).filter(isFullPage);
|
||||
|
||||
const missionsPages = (
|
||||
await queryAllDbResults(notionClient, {
|
||||
database_id: missionsDbId,
|
||||
})
|
||||
await queryAllDbResults(
|
||||
notionClient,
|
||||
{
|
||||
database_id: missionsDbId,
|
||||
},
|
||||
{ cache: cacheConfig }
|
||||
)
|
||||
).filter(isFullPage);
|
||||
|
||||
const departementPages = (
|
||||
await queryAllDbResults(notionClient, {
|
||||
database_id: departementsDbId,
|
||||
})
|
||||
await queryAllDbResults(
|
||||
notionClient,
|
||||
{
|
||||
database_id: departementsDbId,
|
||||
},
|
||||
{ cache: cacheConfig }
|
||||
)
|
||||
).filter(isFullPage);
|
||||
|
||||
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}".`
|
||||
);
|
||||
}
|
||||
propValue.re;
|
||||
return propValue.relation.map((v) => v.id);
|
||||
}
|
||||
|
|
|
@ -13,7 +13,7 @@ type QueryExtraOptions = {
|
|||
cache: CacheConfig;
|
||||
};
|
||||
|
||||
type CacheConfig =
|
||||
export type CacheConfig =
|
||||
| boolean
|
||||
| {
|
||||
ttl: number;
|
||||
|
@ -22,7 +22,7 @@ type CacheConfig =
|
|||
export async function queryAllDbResults(
|
||||
notion: Client,
|
||||
dbQuery: QueryDatabaseParameters,
|
||||
{ cache: cacheConfig }: QueryExtraOptions = { cache: { ttl: 3600 * 1000 } }
|
||||
{ cache: cacheConfig }: QueryExtraOptions
|
||||
): Promise<QueryDatabaseResponse["results"]> {
|
||||
const queryHash = createHash("sha1")
|
||||
.update(JSON.stringify(dbQuery))
|
||||
|
|
Loading…
Add table
Reference in a new issue