Skip to content

Commit 8a0223a

Browse files
authored
refactor: optimise adding providers to collection (#88)
- avoid calling `dag.providers.filter` for each collection because it slows down the collections endpoint - remove unnecessary check if collection exists for /items (check is anyway done in search_base)
1 parent e458c69 commit 8a0223a

2 files changed

Lines changed: 31 additions & 11 deletions

File tree

stac_fastapi/eodag/core.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,10 @@ class EodagCoreClient(CustomCoreClient):
8989
post_request_model: type[BaseModel] = attr.ib(default=BaseSearchPostRequest)
9090
stac_metadata_model: type[CommonStacMetadata] = attr.ib(default=CommonStacMetadata)
9191

92-
def _get_collection(self, collection: EodagCollection, request: Request) -> Collection:
92+
def _get_collection(
93+
self, collection: EodagCollection, request: Request, collections_providers: dict[str, set]
94+
) -> Collection:
9395
"""Convert a EODAG produt type to a STAC collection."""
94-
9596
# extend collection with external stac collection if any
9697
extended_collection = Collection(deepcopy(request.app.state.ext_stac_collections.get(collection.id, {})))
9798
extended_collection["type"] = "Collection"
@@ -100,8 +101,7 @@ def _get_collection(self, collection: EodagCollection, request: Request) -> Coll
100101
constellation = [c for c in (collection.constellation or "").split(",") if c]
101102
processing_level = [pl for pl in (collection.processing_level or "").split(",") if pl]
102103
instruments = collection.instruments or []
103-
104-
federation_backends = request.app.state.dag.providers.filter(collection._id).names
104+
federation_backends = collections_providers.get(collection._id, set())
105105

106106
summaries: dict[str, Any] = {
107107
"platform": platform_value,
@@ -294,7 +294,16 @@ async def all_collections(
294294
else:
295295
collections = all_colls
296296

297-
formatted_collections = [self._get_collection(coll, request) for coll in collections]
297+
providers = request.app.state.dag.providers
298+
collections_providers: dict[str, set] = {}
299+
for p_name, p in providers.items():
300+
if getattr(p.config, "products", None):
301+
for coll in p.config.products:
302+
if coll not in collections_providers:
303+
collections_providers[coll] = set()
304+
collections_providers[coll].add(p_name)
305+
306+
formatted_collections = [self._get_collection(coll, request, collections_providers) for coll in collections]
298307

299308
# bbox filter
300309
if bbox:
@@ -371,7 +380,13 @@ async def get_collection(self, collection_id: str, request: Request, **kwargs: A
371380
if collection is None:
372381
raise NotFoundError(f"Collection {collection_id} does not exist.")
373382

374-
return self._get_collection(collection, request)
383+
providers = request.app.state.dag.providers
384+
collection_providers: dict[str, set] = {collection._id: set()}
385+
for p_name, p in providers.items():
386+
if getattr(p.config, "products", None) and collection._id in p.config.products:
387+
collection_providers[collection._id].add(p_name)
388+
389+
return self._get_collection(collection, request, collection_providers)
375390

376391
async def item_collection(
377392
self,
@@ -406,8 +421,6 @@ async def item_collection(
406421
:returns: An ItemCollection.
407422
:raises NotFoundError: If the collection does not exist.
408423
"""
409-
# If collection does not exist, NotFoundError wil be raised
410-
await self.get_collection(collection_id, request=request)
411424

412425
base_args = {"collections": [collection_id], "bbox": bbox, "datetime": datetime, "limit": limit, "token": token}
413426

@@ -509,8 +522,6 @@ async def get_item(self, item_id: str, collection_id: str, request: Request, **k
509522
:returns: The item.
510523
:raises NotFoundError: If the item does not exist.
511524
"""
512-
# If collection does not exist, NotFoundError wil be raised
513-
await self.get_collection(collection_id, request=request)
514525

515526
search_request = self.post_request_model(ids=[item_id], collections=[collection_id], limit=1)
516527
item_collection = await self._search_base(search_request, request)

tests/conftest.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,16 @@ def app() -> Iterator[FastAPI]:
124124
)
125125
),
126126
"creodias": Provider(
127-
ProviderConfig.from_mapping({"name": "creodias", "search": {"type": "ODataV4Search"}})
127+
ProviderConfig.from_mapping(
128+
{
129+
"name": "creodias",
130+
"search": {"type": "ODataV4Search"},
131+
"products": {
132+
"S2_MSI_L2A": {"_collection": "SENTINEL-2"},
133+
"S1_SAR_GRD": {"_collection": "SENTINEL-1"},
134+
},
135+
}
136+
)
128137
),
129138
}
130139
)

0 commit comments

Comments
 (0)