Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prepare compose all generate id on assemblage #460

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
#!/usr/bin/env node
/* eslint no-await-in-loop: off */
require('dotenv').config()
const argv = require('minimist')(process.argv.slice(2), {string: '_'})
const mongo = require('../util/mongo.cjs')
const {askComposition} = require('../models/commune.cjs')
const {getCommunes} = require('../util/cog.cjs')

async function main() {
const force = argv.force || false
if (force) {
console.info('option "force" activée')
}

await mongo.connect()

const codesCommunesBAN = await mongo.db.collection('communes').distinct('codeCommune')
const currentCodes = new Set(codesCommunesBAN)

const communes = getCommunes().filter(c => currentCodes.has(c.code))
console.log(`Communes à composer: ${communes.length}`)

for (const commune of communes) {
await askComposition(commune.code, {force: true})
await askComposition(commune.code, {force})
}

console.log('Composition forcée demandée')
console.log('Composition France entière demandée')
console.log('Faire un `yarn compose` pour lancer la composition')
await mongo.disconnect()
process.exit(0)
}
Expand Down
3 changes: 2 additions & 1 deletion lib/compose/cli.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ async function main() {

// Check if --ignoreIdConfig flag is provided
const ignoreIdConfig = argv.ignoreIdConfig || false
const force = argv.force || false

await runInParallel(
require.resolve('./worker.cjs'),
communes.map(codeCommune => ({codeCommune, ignoreIdConfig})),
communes.map(codeCommune => ({codeCommune, force, ignoreIdConfig})),
{maxWorkerMemory: 3072, maxRetries: 5}
)

Expand Down
7 changes: 7 additions & 0 deletions lib/compose/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ async function composeCommune(codeCommune, ignoreIdConfig) {

const isBAL = Boolean(currentRevision)

// Si la commune est déjà composée avec un identifiant BAN :
// On ne la recompose plus pour ne pas écraser les identifiants BAN existants
if (!isBAL && !compositionOptions.force && commune?.banId) {
console.log(`${codeCommune} | commune 'assemblage' avec identifiant BAN déjà présent => composition ignorée`)
return false
}

// La bloc suivant garantit qu'il n'y a pas de retour en arrière.
// Ne sera plus nécessaire quand l'API de dépôt contiendra 100% des BAL contenues dans la BAN
if (!isBAL && commune?.typeComposition === 'bal') {
Expand Down
9 changes: 8 additions & 1 deletion lib/compose/worker.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@ const composeCommune = require('./index.cjs')

async function main(options) {
await mongo.connect()
const {codeCommune, ignoreIdConfig} = options
const {codeCommune, force, ignoreIdConfig} = options

console.time(`commune ${codeCommune}`)

if (force) {
await mongo.db.collection('communes').updateOne(
{codeCommune},
{$set: {compositionOptions: {force: true}}}
)
}

await composeCommune(codeCommune, ignoreIdConfig)
await finishComposition(codeCommune)

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"import:ftth": "node lib/import/cli.cjs ftth",
"dist": "node lib/distribute/cli.cjs",
"compose": "node lib/compose/cli.cjs",
"composeAllForce": "node lib/compose/compose-all-force.cjs",
"askComposeAll": "node lib/compose/ask-compose-all.cjs",
"lint": "xo",
"test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest",
"worker": "node worker.js",
Expand Down
Loading