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

perf(api): bulk store ds data #2478

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
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
Expand Up @@ -86,6 +86,7 @@ export class DemarchesSimplifieesService extends ProviderCore implements Demande
async updateDataByFormId(formId: number) {
let result: DemarchesSimplifieesDto;
let nextCursor: string | undefined = undefined;
demarchesSimplifieesDataRepository.initBulk();
do {
result = await this.sendQuery(GetDossiersByDemarcheId, {
demarcheNumber: formId,
Expand All @@ -98,11 +99,12 @@ export class DemarchesSimplifieesService extends ProviderCore implements Demande
throw new InternalServerError("empty Démarches Simplifiées result (not normal with graphQL)");

const entities = DemarchesSimplifieesDtoAdapter.toEntities(result, formId);
await asyncForEach(entities, async entity => {
await demarchesSimplifieesDataRepository.upsert(entity);
});
for (const entity of entities) {
demarchesSimplifieesDataRepository.stackUpsert(entity);
}
nextCursor = result?.data?.demarche?.dossiers?.pageInfo?.endCursor;
} while (result?.data?.demarche?.dossiers?.pageInfo?.hasNextPage);
await demarchesSimplifieesDataRepository.executeBulk();
}

async sendQuery(query: string, vars: DefaultObject) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,38 @@
import { Siren, Siret } from "dto";
import { UnorderedBulkOperation } from "mongodb";
import MongoRepository from "../../../../shared/MongoRepository";
import DemarchesSimplifieesDataEntity from "../entities/DemarchesSimplifieesDataEntity";

export class DemarchesSimplifieesDataRepository extends MongoRepository<DemarchesSimplifieesDataEntity> {
collectionName = "demarches-simplifiees-data";
private bulk: UnorderedBulkOperation | undefined;

async createIndexes() {
await this.collection.createIndex({ "demande.id": 1 }, { unique: true });
await this.collection.createIndex({ siret: 1 });
}

initBulk() {
this.bulk = this.collection.initializeUnorderedBulkOp();
}

stackUpsert(entity: DemarchesSimplifieesDataEntity) {
if (!this.bulk) throw new Error("please call 'initCall' before calling 'stackUpsert'");
this.bulk
.find({
"demande.id": entity.demande.id,
})
.upsert()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

upsert + updateOne ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moi je comprends pas ce que ça fait ! Je veux bien une explication

.updateOne({ $set: entity as Partial<DemarchesSimplifieesDataEntity> });
}

async executeBulk() {
if (!this.bulk) throw new Error("please call 'initCall' and stack operations before calling 'stackUpsert'");
const res = await this.bulk.execute();
this.bulk = undefined; // bulk can not be executed twice without being reset anyway
return res;
}

async upsert(entity: DemarchesSimplifieesDataEntity) {
await this.collection.updateOne(
{
Expand Down
Loading