Skip to content

Commit

Permalink
feat(home): optimize general_stats route (#3313)
Browse files Browse the repository at this point in the history
* feat(home): optimize `general_stats` route
* feat(synthese): add index on gn_synthese.observers to increase perf of `general_stats` computation

Co-authored-by: dba-sig-sfepm <[email protected]>
  • Loading branch information
jacquesfize and dba-sig-sfepm authored Jan 13, 2025
1 parent 6f6b91a commit 9a01786
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 13 deletions.
34 changes: 21 additions & 13 deletions backend/geonature/core/gn_synthese/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -939,21 +939,29 @@ def general_stats(permissions):
.select_from(TDatasets)
.where(TDatasets.filter_by_readable().whereclause)
)
query = select(
func.count(Synthese.id_synthese),
func.count(func.distinct(Synthese.cd_nom)),
func.count(func.distinct(Synthese.observers)),
)
synthese_query_obj = SyntheseQuery(Synthese, query, {})
synthese_query_obj.filter_query_with_cruved(g.current_user, permissions)
result = DB.session.execute(synthese_query_obj.query)
synthese_counts = result.fetchone()
results = {"nb_allowed_datasets": nb_allowed_datasets}

queries = {
"nb_obs": select(
func.count(Synthese.id_synthese),
),
"nb_distinct_species": select(
func.count(func.distinct(Synthese.cd_nom)),
),
"nb_distinct_observer": select(
func.count(func.distinct(Synthese.observers)),
),
}
for key, query in queries.items():
synthese_query = SyntheseQuery(Synthese, query, {})
synthese_query.filter_query_with_cruved(g.current_user, permissions)
results[key] = db.session.scalar(synthese_query.query)

data = {
"nb_data": synthese_counts[0],
"nb_species": synthese_counts[1],
"nb_observers": synthese_counts[2],
"nb_dataset": nb_allowed_datasets,
"nb_data": results["nb_obs"],
"nb_species": results["nb_distinct_species"],
"nb_observers": results["nb_distinct_observer"],
"nb_dataset": results["nb_allowed_datasets"],
}
return data

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""create_index_on_synthese_observers
Revision ID: 5cf0ce9e669c
Revises: 9df933cc3c7a
Create Date: 2025-01-13 14:14:30.085725
"""

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = "5cf0ce9e669c"
down_revision = "9df933cc3c7a"
branch_labels = None
depends_on = None


def upgrade():
op.create_index(
"synthese_observers_idx",
"synthese",
["observers"],
if_not_exists=True,
schema="gn_synthese",
)


def downgrade():
op.drop_index(
"synthese_observers_idx",
table_name="synthese",
schema="gn_synthese",
)
24 changes: 24 additions & 0 deletions backend/geonature/tests/benchmarks/test_benchmark_home.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import logging
import pytest
from geonature.tests.benchmarks import *
from geonature.tests.test_pr_occhab import stations

from .benchmark_generator import BenchmarkTest, CLater
from .utils import activate_profiling_sql

logging.basicConfig()
logger = logging.getLogger("logger-name")
logger.setLevel(logging.DEBUG)

from .utils import CLIENT_GET, CLIENT_POST


@pytest.mark.benchmark(group="home")
@pytest.mark.usefixtures("client_class", "temporary_transaction", "activate_profiling_sql")
class TestBenchmarkHome:

test_general_stats = BenchmarkTest(
CLIENT_GET,
[CLater("""url_for("gn_synthese.general_stats")""")],
dict(user_profile="admin_user", fixtures=[]),
)()

0 comments on commit 9a01786

Please sign in to comment.