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

import in synthese by batch and report progress #491

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions backend/gn_module_import/conf_schema_toml.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,4 @@ class GnModuleSchemaConf(Schema):
ID_LIST_TAXA_RESTRICTION = fields.Integer(load_default=None)
MODULE_URL = fields.String(load_default="/import")
DATAFRAME_BATCH_SIZE = fields.Integer(load_default=10000)
INSERT_BATCH_SIZE = fields.Integer(load_default=1000)
3 changes: 2 additions & 1 deletion backend/gn_module_import/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ def do_import_in_synthese(self, import_id):
entity_source_pk_field=entity_source_pk_field.synthese_field,
module=TModules.query.filter_by(module_code="IMPORT").one(),
)
import_data_to_synthese(imprt)
for progress in import_data_to_synthese(imprt):
self.update_state(state="PROGRESS", meta={"progress": progress})
ImportSyntheseData.query.filter_by(imprt=imprt).delete()
imprt = TImports.query.with_for_update(of=TImports).get(import_id)
if imprt is None or imprt.task_id != self.request.id:
Expand Down
24 changes: 19 additions & 5 deletions backend/gn_module_import/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import json
from enum import IntEnum
from datetime import datetime, timedelta
from math import ceil

from flask import current_app, render_template
from sqlalchemy import func
Expand Down Expand Up @@ -268,6 +269,10 @@ def update_import_data_from_dataframe(imprt, fields, df):


def import_data_to_synthese(imprt):
"""
Import prepared data in synthese.
Operate on batches, and yeld progress between each batch.
"""
generated_fields = {
"datetime_min",
"datetime_max",
Expand Down Expand Up @@ -302,11 +307,20 @@ def import_data_to_synthese(imprt):
"id_dataset",
"last_action",
]
insert_stmt = insert(Synthese).from_select(
names=names,
select=select_stmt,
)
db.session.execute(insert_stmt)
batch_size = current_app.config["IMPORT"]["INSERT_BATCH_SIZE"]
batch_count = ceil(imprt.source_count / batch_size)
for batch in range(batch_count):
min_line_no = batch * batch_size
max_line_no = (batch + 1) * batch_size
insert_stmt = insert(Synthese).from_select(
names=names,
select=select_stmt.filter(
ImportSyntheseData.line_no >= min_line_no,
ImportSyntheseData.line_no < max_line_no,
),
)
db.session.execute(insert_stmt)
yield (batch + 1) / batch_count


def generate_pdf_from_template(template, data):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ <h5 class="card-title mb-0">
class="d-flex flex-column justify-content-center align-items-center w-100 mb-3"
>
<p> Import des données en cours</p>
<mat-spinner [color]="color" [diameter]="50" class="upload-spinner"></mat-spinner>
<mat-progress-bar [value]="progress"></mat-progress-bar>
</div>

<div class="d-flex flex-row justify-content-between progressBar" *ngIf="!importRunning">
Expand Down
Loading