import { Cache, caching } from 'cache-manager'; import { EkepHttpEmddClient } from '../../../src/'; import { EmddExtendedProcess, Id } from '@digigov-oss/emdd-api-client'; import fs from 'fs'; describe('load all updated processes ', () => { let client: EkepHttpEmddClient; let cache: Cache; beforeAll(async () => { cache = await caching('memory', { max: 100000, ttl: 2 * 60 * 1000 /*2 hours*/, }); cache.reset(); const cacheConfig = { cache, ttl: 2 * 60 * 1000 }; client = new EkepHttpEmddClient({ cacheConfig }); }); it.only( 'Load all updated processes using cache', async () => { const processesForUpdate = Array(); const kepProcesses = await client.getProcesses({ filter: 'kep' }); console.log(`kepProcesses ${kepProcesses.length}`); const eugoProcesses = (await client.getProcesses({ filter: 'eu-go' })) ?? []; console.log(`eugoProcesses ${eugoProcesses.length}`); const allProcesses = kepProcesses.concat(eugoProcesses); for (const p of allProcesses) { const fname = `${__dirname}/../../emd/el/emd-${p.id}.json`; if (fs.existsSync(fname)) { const ps = JSON.parse( fs.readFileSync(fname).toString(), ) as EmddExtendedProcess; const psTime = new Date(ps.last_updated).getTime(); const pTime = new Date(p.last_updated).getTime(); if (pTime > psTime) { processesForUpdate.push(p.id); console.log( `UPDATE old ${ps.last_updated} current ${p.last_updated}`, ); } } else { processesForUpdate.push(p.id); console.log(`INSERT ${p.id}`); } } const chunkSize = processesForUpdate.length; const runChunk = async (chunk: Id[], i: number) => { console.time(`Fetched chunk ${i}`); const promises: Array> = []; for (const pid of chunk) { promises.push( client.getProcess(pid).then((r) => { console.log(`fetched process ${r.id}`); return r; }), ); } const res = await Promise.all(promises); console.timeEnd(`Fetched chunk ${i}`); return res; }; console.log( `fetching ${processesForUpdate.length} in chunks of ${chunkSize}`, ); const chunks: Array = []; for (let i = 0; i < processesForUpdate.length; i += chunkSize) { chunks.push(processesForUpdate.slice(i, i + chunkSize)); } console.log(`processing ${chunks.length} chunks`); let j = 0; console.time(`Fetching lasted`); for (const chunk of chunks) { await runChunk(chunk, ++j); } console.timeEnd(`Fetching lasted`); console.log(`cacheHits ${client.processTransformer.cacheHits}`); }, 1 * 60 * 60 * 1000, ); });