-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: mise a jour des computed organisme des effectifs (#3901)
- Loading branch information
Showing
6 changed files
with
82 additions
and
3 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
5 changes: 5 additions & 0 deletions
5
server/src/db/migrations/20241114091932-tmp:patches:update_effectifs_computed_organisme.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,5 @@ | ||
import { addJob } from "job-processor"; | ||
|
||
export const up = async () => { | ||
await addJob({ name: "tmp:patches:update_effectifs_computed_organisme", queued: true }); | ||
}; |
45 changes: 45 additions & 0 deletions
45
server/src/jobs/hydrate/effectifs/hydrate-effectifs-computed-organisme.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,45 @@ | ||
import { ObjectId } from "mongodb"; | ||
import { IOrganisme } from "shared/models"; | ||
|
||
import { generateOrganismeComputed } from "@/common/actions/organismes/organismes.actions"; | ||
import logger from "@/common/logger"; | ||
import { organismesDb, effectifsDb } from "@/common/model/collections"; | ||
|
||
export async function hydrateEffectifsComputedOrganisme() { | ||
const batchSize = 100; | ||
let processedCount = 0; | ||
|
||
const cursor = organismesDb().find({}); | ||
while (await cursor.hasNext()) { | ||
const batch: IOrganisme[] = []; | ||
for (let i = 0; i < batchSize; i++) { | ||
if (await cursor.hasNext()) { | ||
const organisme = await cursor.next(); | ||
if (organisme) { | ||
batch.push(organisme); | ||
} | ||
} else { | ||
break; | ||
} | ||
} | ||
|
||
await Promise.all( | ||
batch.map(async (organisme) => { | ||
const computedResult = generateOrganismeComputed(organisme); | ||
await effectifsDb().updateMany( | ||
{ organisme_id: new ObjectId(organisme._id) }, | ||
{ | ||
$set: { | ||
"_computed.organisme": computedResult, | ||
}, | ||
} | ||
); | ||
}) | ||
); | ||
|
||
processedCount += batch.length; | ||
logger.info(`${processedCount} organismes traités jusqu'à présent.`); | ||
} | ||
|
||
logger.info(`Traitement de ${processedCount} organismes terminé.`); | ||
} |
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