Skip to content

Commit ee90324

Browse files
committed
pre-commit.
1 parent 3a75b68 commit ee90324

File tree

2 files changed

+142
-48
lines changed

2 files changed

+142
-48
lines changed

stac_fastapi/opensearch/stac_fastapi/opensearch/database_logic.py

Lines changed: 77 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,9 @@ async def create_item_index(collection_id: str):
273273
}
274274

275275
try:
276-
await client.indices.create(index=f"{index_by_collection_id(collection_id)}-000001", body=search_body)
276+
await client.indices.create(
277+
index=f"{index_by_collection_id(collection_id)}-000001", body=search_body
278+
)
277279
except TransportError as e:
278280
if e.status_code == 400:
279281
pass # Ignore 400 status codes
@@ -354,8 +356,12 @@ class DatabaseLogic:
354356
client = AsyncSearchSettings().create_client
355357
sync_client = SyncSearchSettings().create_client
356358

357-
item_serializer: Type[serializers.ItemSerializer] = attr.ib(default=serializers.ItemSerializer)
358-
collection_serializer: Type[serializers.CollectionSerializer] = attr.ib(default=serializers.CollectionSerializer)
359+
item_serializer: Type[serializers.ItemSerializer] = attr.ib(
360+
default=serializers.ItemSerializer
361+
)
362+
collection_serializer: Type[serializers.CollectionSerializer] = attr.ib(
363+
default=serializers.CollectionSerializer
364+
)
359365

360366
extensions: List[str] = attr.ib(default=attr.Factory(list))
361367

@@ -389,9 +395,15 @@ class DatabaseLogic:
389395
"size": 10000,
390396
}
391397
},
392-
"sun_elevation_frequency": {"histogram": {"field": "properties.view:sun_elevation", "interval": 5}},
393-
"sun_azimuth_frequency": {"histogram": {"field": "properties.view:sun_azimuth", "interval": 5}},
394-
"off_nadir_frequency": {"histogram": {"field": "properties.view:off_nadir", "interval": 5}},
398+
"sun_elevation_frequency": {
399+
"histogram": {"field": "properties.view:sun_elevation", "interval": 5}
400+
},
401+
"sun_azimuth_frequency": {
402+
"histogram": {"field": "properties.view:sun_azimuth", "interval": 5}
403+
},
404+
"off_nadir_frequency": {
405+
"histogram": {"field": "properties.view:off_nadir", "interval": 5}
406+
},
395407
"centroid_geohash_grid_frequency": {
396408
"geohash_grid": {
397409
"field": "properties.proj:centroid",
@@ -494,7 +506,9 @@ async def get_one_item(self, collection_id: str, item_id: str) -> Dict:
494506
id=mk_item_id(item_id, collection_id),
495507
)
496508
except exceptions.NotFoundError:
497-
raise NotFoundError(f"Item {item_id} does not exist in Collection {collection_id}")
509+
raise NotFoundError(
510+
f"Item {item_id} does not exist in Collection {collection_id}"
511+
)
498512
return item["_source"]
499513

500514
@staticmethod
@@ -517,7 +531,9 @@ def apply_free_text_filter(search: Search, free_text_queries: Optional[List[str]
517531
"""Database logic to perform query for search endpoint."""
518532
if free_text_queries is not None:
519533
free_text_query_string = '" OR properties.\\*:"'.join(free_text_queries)
520-
search = search.query("query_string", query=f'properties.\\*:"{free_text_query_string}"')
534+
search = search.query(
535+
"query_string", query=f'properties.\\*:"{free_text_query_string}"'
536+
)
521537

522538
return search
523539

@@ -533,10 +549,16 @@ def apply_datetime_filter(search: Search, datetime_search):
533549
Search: The filtered search object.
534550
"""
535551
if "eq" in datetime_search:
536-
search = search.filter("term", **{"properties__datetime": datetime_search["eq"]})
552+
search = search.filter(
553+
"term", **{"properties__datetime": datetime_search["eq"]}
554+
)
537555
else:
538-
search = search.filter("range", properties__datetime={"lte": datetime_search["lte"]})
539-
search = search.filter("range", properties__datetime={"gte": datetime_search["gte"]})
556+
search = search.filter(
557+
"range", properties__datetime={"lte": datetime_search["lte"]}
558+
)
559+
search = search.filter(
560+
"range", properties__datetime={"gte": datetime_search["gte"]}
561+
)
540562
return search
541563

542564
@staticmethod
@@ -739,7 +761,11 @@ async def execute_search(
739761
if hits and (sort_array := hits[limit - 1].get("sort")):
740762
next_token = urlsafe_b64encode(json.dumps(sort_array).encode()).decode()
741763

742-
matched = es_response["hits"]["total"]["value"] if es_response["hits"]["total"]["relation"] == "eq" else None
764+
matched = (
765+
es_response["hits"]["total"]["value"]
766+
if es_response["hits"]["total"]["relation"] == "eq"
767+
else None
768+
)
743769
if count_task.done():
744770
try:
745771
matched = count_task.result().get("count")
@@ -817,7 +843,9 @@ async def check_collection_exists(self, collection_id: str):
817843
if not await self.client.exists(index=COLLECTIONS_INDEX, id=collection_id):
818844
raise NotFoundError(f"Collection {collection_id} does not exist")
819845

820-
async def prep_create_item(self, item: Item, base_url: str, exist_ok: bool = False) -> Item:
846+
async def prep_create_item(
847+
self, item: Item, base_url: str, exist_ok: bool = False
848+
) -> Item:
821849
"""
822850
Preps an item for insertion into the database.
823851
@@ -839,11 +867,15 @@ async def prep_create_item(self, item: Item, base_url: str, exist_ok: bool = Fal
839867
index=index_alias_by_collection_id(item["collection"]),
840868
id=mk_item_id(item["id"], item["collection"]),
841869
):
842-
raise ConflictError(f"Item {item['id']} in collection {item['collection']} already exists")
870+
raise ConflictError(
871+
f"Item {item['id']} in collection {item['collection']} already exists"
872+
)
843873

844874
return self.item_serializer.stac_to_db(item, base_url)
845875

846-
def sync_prep_create_item(self, item: Item, base_url: str, exist_ok: bool = False) -> Item:
876+
def sync_prep_create_item(
877+
self, item: Item, base_url: str, exist_ok: bool = False
878+
) -> Item:
847879
"""
848880
Prepare an item for insertion into the database.
849881
@@ -872,7 +904,9 @@ def sync_prep_create_item(self, item: Item, base_url: str, exist_ok: bool = Fals
872904
index=index_alias_by_collection_id(collection_id),
873905
id=mk_item_id(item_id, collection_id),
874906
):
875-
raise ConflictError(f"Item {item_id} in collection {collection_id} already exists")
907+
raise ConflictError(
908+
f"Item {item_id} in collection {collection_id} already exists"
909+
)
876910

877911
return self.item_serializer.stac_to_db(item, base_url)
878912

@@ -900,7 +934,9 @@ async def create_item(self, item: Item, refresh: bool = False):
900934
)
901935

902936
if (meta := es_resp.get("meta")) and meta.get("status") == 409:
903-
raise ConflictError(f"Item {item_id} in collection {collection_id} already exists")
937+
raise ConflictError(
938+
f"Item {item_id} in collection {collection_id} already exists"
939+
)
904940

905941
async def merge_patch_item(
906942
self,
@@ -1023,7 +1059,9 @@ async def json_patch_item(
10231059

10241060
return item
10251061

1026-
async def delete_item(self, item_id: str, collection_id: str, refresh: bool = False):
1062+
async def delete_item(
1063+
self, item_id: str, collection_id: str, refresh: bool = False
1064+
):
10271065
"""Delete a single item from the database.
10281066
10291067
Args:
@@ -1041,7 +1079,9 @@ async def delete_item(self, item_id: str, collection_id: str, refresh: bool = Fa
10411079
refresh=refresh,
10421080
)
10431081
except exceptions.NotFoundError:
1044-
raise NotFoundError(f"Item {item_id} in collection {collection_id} not found")
1082+
raise NotFoundError(
1083+
f"Item {item_id} in collection {collection_id} not found"
1084+
)
10451085

10461086
async def create_collection(self, collection: Collection, refresh: bool = False):
10471087
"""Create a single collection in the database.
@@ -1088,13 +1128,17 @@ async def find_collection(self, collection_id: str) -> Collection:
10881128
collection as a `Collection` object. If the collection is not found, a `NotFoundError` is raised.
10891129
"""
10901130
try:
1091-
collection = await self.client.get(index=COLLECTIONS_INDEX, id=collection_id)
1131+
collection = await self.client.get(
1132+
index=COLLECTIONS_INDEX, id=collection_id
1133+
)
10921134
except exceptions.NotFoundError:
10931135
raise NotFoundError(f"Collection {collection_id} not found")
10941136

10951137
return collection["_source"]
10961138

1097-
async def update_collection(self, collection_id: str, collection: Collection, refresh: bool = False):
1139+
async def update_collection(
1140+
self, collection_id: str, collection: Collection, refresh: bool = False
1141+
):
10981142
"""Update a collection from the database.
10991143
11001144
Args:
@@ -1214,7 +1258,9 @@ async def json_patch_collection(
12141258
if new_collection_id:
12151259
collection["id"] = new_collection_id
12161260
collection["links"] = resolve_links([], base_url)
1217-
await self.update_collection(collection_id=collection_id, collection=collection, refresh=False)
1261+
await self.update_collection(
1262+
collection_id=collection_id, collection=collection, refresh=False
1263+
)
12181264

12191265
return collection
12201266

@@ -1235,10 +1281,14 @@ async def delete_collection(self, collection_id: str, refresh: bool = False):
12351281
function also calls `delete_item_index` to delete the index for the items in the collection.
12361282
"""
12371283
await self.find_collection(collection_id=collection_id)
1238-
await self.client.delete(index=COLLECTIONS_INDEX, id=collection_id, refresh=refresh)
1284+
await self.client.delete(
1285+
index=COLLECTIONS_INDEX, id=collection_id, refresh=refresh
1286+
)
12391287
await delete_item_index(collection_id)
12401288

1241-
async def bulk_async(self, collection_id: str, processed_items: List[Item], refresh: bool = False) -> None:
1289+
async def bulk_async(
1290+
self, collection_id: str, processed_items: List[Item], refresh: bool = False
1291+
) -> None:
12421292
"""Perform a bulk insert of items into the database asynchronously.
12431293
12441294
Args:
@@ -1260,7 +1310,9 @@ async def bulk_async(self, collection_id: str, processed_items: List[Item], refr
12601310
raise_on_error=False,
12611311
)
12621312

1263-
def bulk_sync(self, collection_id: str, processed_items: List[Item], refresh: bool = False) -> None:
1313+
def bulk_sync(
1314+
self, collection_id: str, processed_items: List[Item], refresh: bool = False
1315+
) -> None:
12641316
"""Perform a bulk insert of items into the database synchronously.
12651317
12661318
Args:

0 commit comments

Comments
 (0)