-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: importation des indicateurs régionales
- Loading branch information
Showing
12 changed files
with
138 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
server/src/jobs/exposition/importIndicateurPoursuiteRegionale.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { Readable } from "stream"; | ||
import { oleoduc, writeData, transformData, filterData, flattenArray } from "oleoduc"; | ||
import { range } from "lodash-es"; | ||
import { getLoggerWithContext } from "#src/common/logger.js"; | ||
import RawDataRepository, { RawDataType } from "#src/common/repositories/rawData"; | ||
import { ExpositionApi } from "#src/services/exposition/ExpositionApi.js"; | ||
|
||
const logger = getLoggerWithContext("import"); | ||
|
||
export async function importIndicateurPoursuiteRegionale() { | ||
logger.info(`Importation des indicateurs de poursuite (données InserJeunes)`); | ||
const stats = { total: 0, created: 0, updated: 0, failed: 0 }; | ||
const expositionApiOptions = { retry: { retries: 5 } }; | ||
const expositionApi = new ExpositionApi(expositionApiOptions); | ||
|
||
const limitPerPage = 1000; | ||
|
||
function handleError(e, context, msg = `Impossible d'importer les stats régionales`) { | ||
logger.error({ err: e, ...context }, msg); | ||
stats.failed++; | ||
return null; //ignore chunk | ||
} | ||
|
||
await RawDataRepository.deleteAll(RawDataType.EXPOSITION_regionales); | ||
|
||
await oleoduc( | ||
Readable.from([ | ||
await expositionApi | ||
.fetchRegionalesStats(1, 1) | ||
.catch((e) => handleError(e, {}, "Impossible de récupérer le nombre de page des stats régionales")), | ||
]), | ||
transformData((result) => { | ||
const nbPages = Math.ceil(result.pagination.total / limitPerPage); | ||
const pages = range(1, nbPages + 1); | ||
|
||
logger.info(`Récupération des stats régionales pour ${nbPages} pages`); | ||
|
||
return pages; | ||
}), | ||
flattenArray(), | ||
transformData(async (page) => { | ||
try { | ||
logger.info(`Récupération des stats régionales pour la page ${page}`); | ||
const formations = await expositionApi.fetchRegionalesStats(page, limitPerPage); | ||
return formations.regionales; | ||
} catch (err) { | ||
return handleError(err, { page }); | ||
} | ||
}), | ||
flattenArray(), | ||
filterData((data) => { | ||
return data.code_certification_type === "mef11" || data.code_certification_type === "cfd"; | ||
}), | ||
writeData( | ||
async (data) => { | ||
try { | ||
await RawDataRepository.insertRaw(RawDataType.EXPOSITION_regionales, { data }); | ||
|
||
logger.info(`Indicateur de poursuite régionales ${data.region.nom} ajoutée`); | ||
stats.created++; | ||
} catch (e) { | ||
logger.error(e, `Impossible d'ajouter les indicateurs de poursuite régionales ${data.region.nom}`); | ||
stats.failed++; | ||
} | ||
}, | ||
{ parallel: 1 } | ||
) | ||
); | ||
|
||
return stats; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export function pluralize(str: string, count: number): string { | ||
return str + (count > 1 ? "s" : ""); | ||
} |