Skip to content

Commit

Permalink
Fixed alg. used to retrieve latest available licence (#87)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
keul authored Sep 27, 2024
1 parent f3bf646 commit 5a2e8e6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
39 changes: 22 additions & 17 deletions cads_catalogue_api_service/vocabularies.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand All @@ -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


Expand All @@ -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,
)
Expand All @@ -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(
Expand Down
7 changes: 4 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down

0 comments on commit 5a2e8e6

Please sign in to comment.