Skip to content

Commit

Permalink
fix(synthese): order by descending date_min and id_synthese
Browse files Browse the repository at this point in the history
For Synthese queries involving blurring:
- Add first order_by on descending date_min > this allows to retrieve up-to-50000 more-recently-dated observations from the up-to-100000 observations of the 2 unioned queries
- Set (now) second order_by on id_synthese from ascending to descending > this allows, for several observations with same date_min, to prioritize more-recently-added-in-database observations

Notes:
- We want to preserve the final order_by on `allowed_geom_cte.c.priority` and the distinct clause on id_synthese, to keep only precise geom for obs retrieved in both precise and blurred subqueries: this requires to:
  - Keep the order_by on id_synthese
- The wish to retrieve "more recent" observations may be understood in two ways:
  - Meant as "more-recently-dated": operated by the first order_by > it is thus assumed this is the preferred meaning given to "more recent"
  - Meant as "more-recently-added-in-database": operated by the second order_by > it is thus assumed that for two observations with the same date_min we qualify as "more recent" the one with the greater id_synthese

Solves #3249

Co-authored-by: VincentCauchois <vincent.cauchois@mnhn.fr>
Co-authored-by: Christophe Ramet <christophe.ramet@mnhn.fr>
  • Loading branch information
2 people authored and jacquesfize committed Feb 3, 2025
1 parent 3f1948c commit d0dfcce
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions backend/geonature/core/gn_synthese/utils/blurring.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def build_blurred_precise_geom_queries(
columns.append(sa.literal(0).label("size_hierarchy"))
precise_geom_query = SyntheseQuery(
Synthese,
sa.select(*columns).where(sa.and_(*where_clauses)).order_by(Synthese.id_synthese.desc()),
sa.select(*columns).where(sa.and_(*where_clauses)).order_by(Synthese.date_min.desc()),
filters=dict(filters), # not to edit the actual filter object
)

Expand Down Expand Up @@ -100,7 +100,7 @@ def build_blurred_precise_geom_queries(
== Synthese.id_nomenclature_sensitivity
)
.where(sa.and_(*where_clauses))
.order_by(Synthese.id_synthese.desc()),
.order_by(Synthese.date_min.desc()),
filters=dict(filters),
query_joins=sa.join(
Synthese,
Expand Down Expand Up @@ -182,8 +182,12 @@ def build_synthese_obs_query(observations, allowed_geom_cte, limit):
allowed_geom_cte, allowed_geom_cte.c.id_synthese == VSyntheseForWebApp.id_synthese
)
)
.order_by(VSyntheseForWebApp.id_synthese, allowed_geom_cte.c.priority)
.distinct(VSyntheseForWebApp.id_synthese)
.order_by(
VSyntheseForWebApp.date_min.desc(),
VSyntheseForWebApp.id_synthese.desc(),
allowed_geom_cte.c.priority,
)
.distinct(VSyntheseForWebApp.date_min, VSyntheseForWebApp.id_synthese)
.limit(limit)
)
return obs_query

0 comments on commit d0dfcce

Please sign in to comment.