Skip to content

Commit

Permalink
Set "federation:backends" summary on single-back-end collection metad…
Browse files Browse the repository at this point in the history
…ata too

preparation for #95
  • Loading branch information
soxofaan committed Mar 8, 2023
1 parent c02256c commit 29a3756
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/openeo_aggregator/about.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.7.0a1"
__version__ = "0.7.1a1"
5 changes: 4 additions & 1 deletion src/openeo_aggregator/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
merge_collection_metadata,
normalize_collection_metadata,
ProcessMetadataMerger,
single_backend_collection_post_processing,
)
from openeo_aggregator.metadata.reporter import LoggerReporter
from openeo_aggregator.partitionedjobs import PartitionedJob
Expand Down Expand Up @@ -119,6 +120,7 @@ def _get_all_metadata(self) -> Tuple[List[dict], _InternalCollectionMetadata]:
# Simple case: collection is only available on single backend.
_log.debug(f"Accept single backend collection {cid} as is")
(bid, metadata), = by_backend.items()
single_backend_collection_post_processing(metadata, backend_id=bid)
else:
_log.info(f"Merging {cid!r} collection metadata from backends {by_backend.keys()}")
try:
Expand Down Expand Up @@ -210,7 +212,8 @@ def _get_collection_metadata(self, collection_id: str) -> dict:
if len(by_backend) == 0:
raise CollectionNotFoundException(collection_id=collection_id)
elif len(by_backend) == 1:
metadata = by_backend.popitem()[1]
bid, metadata = by_backend.popitem()
single_backend_collection_post_processing(metadata, backend_id=bid)
else:
_log.info(f"Merging metadata for collection {collection_id}.")
metadata = merge_collection_metadata(
Expand Down
15 changes: 14 additions & 1 deletion src/openeo_aggregator/metadata/merging.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import logging
from typing import Dict, Optional, Callable, Any, List

from openeo.util import rfc3339
from openeo.util import rfc3339, deep_get
from openeo_aggregator.metadata import (
STAC_PROPERTY_PROVIDER_BACKEND,
STAC_PROPERTY_FEDERATION_BACKENDS,
Expand Down Expand Up @@ -224,6 +224,19 @@ def merge_collection_metadata(
return result


def single_backend_collection_post_processing(metadata: dict, *, backend_id: str):
"""In-place post-processing of a single backend collection"""
if not deep_get(
metadata, "summaries", STAC_PROPERTY_FEDERATION_BACKENDS, default=None
):
metadata.setdefault("summaries", {})
metadata["summaries"][STAC_PROPERTY_FEDERATION_BACKENDS] = [backend_id]
else:
_log.warning(
f"Summary {STAC_PROPERTY_FEDERATION_BACKENDS} is already set on collection {metadata.get('id', 'n/a')}, which is weird."
)


def set_if_non_empty(d: dict, key: str, value: Any):
"""Helper to compactly set a key in a dictionary if the value is non-empty (aka "truthy")."""
if value:
Expand Down
7 changes: 7 additions & 0 deletions tests/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -916,6 +916,7 @@ def test_get_all_metadata_simple(self, catalog, backend1, backend2, requests_moc
assert metadata == [
{
"id": "S2",
"summaries": {"federation:backends": ["b1"]},
"links": [
{
"href": "http://oeoa.test/openeo/1.1.0/collections",
Expand All @@ -933,6 +934,7 @@ def test_get_all_metadata_simple(self, catalog, backend1, backend2, requests_moc
},
{
"id": "S3",
"summaries": {"federation:backends": ["b2"]},
"links": [
{
"href": "http://oeoa.test/openeo/1.1.0/collections",
Expand Down Expand Up @@ -965,6 +967,7 @@ def test_get_all_metadata_common_collections_minimal(
assert metadata == [
{
"id": "S3",
"summaries": {"federation:backends": ["b1"]},
"links": [
{
"href": "http://oeoa.test/openeo/1.1.0/collections",
Expand Down Expand Up @@ -1012,6 +1015,7 @@ def test_get_all_metadata_common_collections_minimal(
},
{
"id": "S5",
"summaries": {"federation:backends": ["b2"]},
"links": [
{
"href": "http://oeoa.test/openeo/1.1.0/collections",
Expand Down Expand Up @@ -1200,6 +1204,7 @@ def test_get_collection_metadata_basic(
metadata = catalog.get_collection_metadata("S2")
assert metadata == {
'id': 'S2', 'title': "b1's S2",
"summaries": {"federation:backends": ["b1"]},
'links': [
{'href': 'http://oeoa.test/openeo/1.1.0/collections', 'rel': 'root'},
{'href': 'http://oeoa.test/openeo/1.1.0/collections', 'rel': 'parent'},
Expand All @@ -1209,6 +1214,7 @@ def test_get_collection_metadata_basic(
metadata = catalog.get_collection_metadata("S3")
assert metadata == {
"id": "S3", "title": "b2's S3",
"summaries": {"federation:backends": ["b2"]},
'links': [
{'href': 'http://oeoa.test/openeo/1.1.0/collections', 'rel': 'root'},
{'href': 'http://oeoa.test/openeo/1.1.0/collections', 'rel': 'parent'},
Expand Down Expand Up @@ -1862,6 +1868,7 @@ def test_get_collection_metadata_merging_with_error(
assert metadata == {
"id": "S2",
"title": "b2's S2",
"summaries": {"federation:backends": ["b2"]},
"links": [
{"href": "http://oeoa.test/openeo/1.1.0/collections", "rel": "root"},
{"href": "http://oeoa.test/openeo/1.1.0/collections", "rel": "parent"},
Expand Down

0 comments on commit 29a3756

Please sign in to comment.