From 5a2e8e634c12b28b9fc5e32e07c5eda8beceadf6 Mon Sep 17 00:00:00 2001 From: Luca Fabbri Date: Fri, 27 Sep 2024 10:16:58 +0200 Subject: [PATCH] Fixed alg. used to retrieve latest available licence (#87) * Fixed alg. used to retrieve latest available licence * Fixed licences vocabulary query Group by was not working properly, not returning just most updated revisions * Fixed SAWarning and mypy deprecation note * Fixing wheel build --- cads_catalogue_api_service/vocabularies.py | 39 ++++++++++++---------- pyproject.toml | 7 ++-- 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/cads_catalogue_api_service/vocabularies.py b/cads_catalogue_api_service/vocabularies.py index 9b30e54..f58c27e 100644 --- a/cads_catalogue_api_service/vocabularies.py +++ b/cads_catalogue_api_service/vocabularies.py @@ -42,14 +42,22 @@ def query_licences( scope: LicenceScopeCriterion, portals: list[str] | None = None, ) -> list[cads_catalogue.database.Licence]: - """Query licences.""" - # NOTE: possible issue here if the title of a licence change from a revision to another + """Query all licences. + + Return the latest revision of each licence. Older revision virtually disappear from API. + """ + # subquery, to select all unique licence_uids and their max revision + subquery = session.query( + cads_catalogue.database.Licence.licence_uid, + sa.func.max(cads_catalogue.database.Licence.revision).label("revision"), + ).group_by(cads_catalogue.database.Licence.licence_uid) + query = session.query( cads_catalogue.database.Licence.licence_uid, cads_catalogue.database.Licence.title, cads_catalogue.database.Licence.md_filename, cads_catalogue.database.Licence.download_filename, - sa.func.max(cads_catalogue.database.Licence.revision).label("revision"), + cads_catalogue.database.Licence.revision, cads_catalogue.database.Licence.scope, cads_catalogue.database.Licence.portal, ) @@ -62,18 +70,14 @@ def query_licences( cads_catalogue.database.Licence.portal.is_(None), ) ) - results = ( - query.group_by( + + # Now retrieve all licences where the tuple (licence_uid, revision) is in the subquery + results = query.filter( + sa.tuple_( cads_catalogue.database.Licence.licence_uid, - cads_catalogue.database.Licence.title, - cads_catalogue.database.Licence.md_filename, - cads_catalogue.database.Licence.download_filename, - cads_catalogue.database.Licence.scope, - cads_catalogue.database.Licence.portal, - ) - .order_by(cads_catalogue.database.Licence.title) - .all() - ) + cads_catalogue.database.Licence.revision, + ).in_(subquery) + ).order_by(cads_catalogue.database.Licence.title) return results # type: ignore @@ -87,7 +91,7 @@ def query_licence( cads_catalogue.database.Licence.title, cads_catalogue.database.Licence.md_filename, cads_catalogue.database.Licence.download_filename, - sa.func.max(cads_catalogue.database.Licence.revision).label("revision"), + cads_catalogue.database.Licence.revision, cads_catalogue.database.Licence.scope, cads_catalogue.database.Licence.portal, ) @@ -99,11 +103,12 @@ def query_licence( cads_catalogue.database.Licence.title, cads_catalogue.database.Licence.md_filename, cads_catalogue.database.Licence.download_filename, + cads_catalogue.database.Licence.revision, cads_catalogue.database.Licence.scope, cads_catalogue.database.Licence.portal, ) - .order_by(cads_catalogue.database.Licence.title) - .one() + .order_by(sa.desc("revision")) + .first() ) except sa.exc.NoResultFound as exc: raise fastapi.HTTPException( diff --git a/pyproject.toml b/pyproject.toml index 29d3e26..17d3472 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -17,11 +17,12 @@ dependencies = [ "cads-catalogue@git+https://github.com/ecmwf-projects/cads-catalogue.git", "cads-common@git+https://github.com/ecmwf-projects/cads-common.git", "fastapi>=0.113.0", + "pydantic<2", "python-dateutil", "sqlalchemy>=2.0.9", - "stac_fastapi.api", - "stac_fastapi.extensions", - "stac_fastapi.types" + "stac_fastapi.api==2.4.1", + "stac_fastapi.extensions==2.4.1", + "stac_fastapi.types==2.4.1" ] description = "STAC based API service for the Climate & Atmosphere Data Store" dynamic = ["version"]