Skip to content

Commit

Permalink
Biblistes : performance récupération nombre de taxons (#589)
Browse files Browse the repository at this point in the history
* feat: improve perf route biblistes

- Load only necessary fields
- Remove useless parameter id=None
- Fix expression "nb_taxons" in BibListes model

Reviewed-by: andriacap

* Optimisation interface admin + tests

---------

Co-authored-by: Andria Capai <[email protected]>
  • Loading branch information
amandine-sahl and andriacap authored Dec 6, 2024
1 parent d02c8c3 commit 6c68e5d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 30 deletions.
10 changes: 7 additions & 3 deletions apptax/taxonomie/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,13 +261,17 @@ class BibListes(db.Model):

@hybrid_property
def nb_taxons(self):
return len(self.noms)
return db.session.scalar(
select([db.func.count(cor_nom_liste.c.cd_nom)]).where(
cor_nom_liste.c.id_liste == self.id_liste
)
)

@nb_taxons.expression
def nb_taxons(cls):
return (
db.select([db.func.count(cor_nom_liste.id_liste)])
.where(BibListes.id_liste == cls.id_liste)
db.select([db.func.count(cor_nom_liste.c.cd_nom)])
.where(cor_nom_liste.c.id_liste == cls.id_liste)
.label("nb_taxons")
)

Expand Down
37 changes: 23 additions & 14 deletions apptax/taxonomie/routesbiblistes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@
import os
import logging

from flask import Blueprint, request, current_app
from sqlalchemy import func, or_
from sqlalchemy.orm import joinedload
from flask import Blueprint
from sqlalchemy import select

from pypnusershub import routes as fnauth
from utils_flask_sqla.response import json_resp

from . import filemanager
from . import db
from .models import BibListes, Taxref
from .models import BibListes
from apptax.taxonomie.schemas import BibListesSchema

adresses = Blueprint("bib_listes", __name__)
Expand All @@ -24,26 +21,38 @@

@adresses.route("/", methods=["GET"])
@json_resp
def get_biblistes(id=None):
def get_biblistes():
"""
retourne les contenu de bib_listes dans "data"
et le nombre d'enregistrements dans "count"
"""
data = db.session.query(BibListes).all()
biblistes_records = db.session.execute(
select(
BibListes.id_liste,
BibListes.code_liste,
BibListes.nom_liste,
BibListes.desc_liste,
BibListes.nb_taxons,
BibListes.regne,
BibListes.group2_inpn,
)
).all()
biblistes_schema = BibListesSchema()
maliste = {"data": [], "count": 0}
maliste["count"] = len(data)
maliste["data"] = biblistes_schema.dump(data, many=True)
return maliste
biblistes_infos = {
"data": biblistes_schema.dump(biblistes_records, many=True),
"count": len(biblistes_records),
}

return biblistes_infos


@adresses.route("/<regne>", methods=["GET"], defaults={"group2_inpn": None})
@adresses.route("/<regne>/<group2_inpn>", methods=["GET"])
def get_biblistesbyTaxref(regne, group2_inpn):
q = db.session.query(BibListes)
q = select(BibListes)
if regne:
q = q.where(BibListes.regne == regne)
if group2_inpn:
q = q.where(BibListes.group2_inpn == group2_inpn)
results = q.all()
results = db.session.scalars(q).all()
return BibListesSchema().dump(results, many=True)
9 changes: 8 additions & 1 deletion apptax/tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,14 @@ def liste():
"code_liste": "TEST_LIST_Plantae",
"nom_liste": "Liste test Plantae",
"desc_liste": "Liste description",
"regne": "Plantea",
"regne": "Plantae",
},
{
"code_liste": "TEST_LIST_Mousses",
"nom_liste": "Liste test Mousses",
"desc_liste": "Liste description",
"regne": "Plantae",
"group2_inpn": "Mousses",
},
]

Expand Down
22 changes: 10 additions & 12 deletions apptax/tests/test_biblistes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,21 @@

@pytest.mark.usefixtures("client_class", "temporary_transaction")
class TestApiBibListe:
schema_cor_nom_liste = Schema(
{
"items": [{"cd_nom": int, "id_liste": int}],
"total": int,
"limit": int,
"page": int,
}
)

schema_allnamebyListe = Schema(
[
{
"id_liste": int,
"code_liste": str,
"nom_liste": str,
"desc_liste": str,
"desc_liste": Or(str, None),
"regne": Or(str, None),
"group2_inpn": Or(str, None),
"nb_taxons": int,
}
]
)

def test_get_biblistes(self):
def test_get_biblistes(self, listes):
query_string = {"limit": 10}
response = self.client.get(
url_for(
Expand All @@ -45,11 +36,18 @@ def test_get_biblistes(self):
assert self.schema_allnamebyListe.is_valid(data["data"])

def test_get_biblistesbyTaxref(self, listes):

response = self.client.get(
url_for("bib_listes.get_biblistesbyTaxref", regne="Animalia", group2_inpn=None),
)
# Filter test list only
data = [d for d in response.json if d["desc_liste"] == "Liste description"]
self.schema_allnamebyListe.validate(data)
assert len(data) == 1

response = self.client.get(
url_for("bib_listes.get_biblistesbyTaxref", regne="Plantae", group2_inpn="Mousses"),
)
# Filter test list only
data = [d for d in response.json if d["desc_liste"] == "Liste description"]
self.schema_allnamebyListe.validate(data)
assert len(data) == 1

0 comments on commit 6c68e5d

Please sign in to comment.