Skip to content

Commit b956bae

Browse files
committed
move logic to utilities
1 parent e674381 commit b956bae

File tree

6 files changed

+106
-96
lines changed

6 files changed

+106
-96
lines changed

stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/database_logic.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,15 @@
3535
ITEM_INDICES,
3636
ITEMS_INDEX_PREFIX,
3737
Geometry,
38+
)
39+
from stac_fastapi.sfeos_helpers.utilities import (
3840
index_alias_by_collection_id,
3941
index_by_collection_id,
4042
indices,
4143
mk_actions,
4244
mk_item_id,
45+
validate_refresh,
4346
)
44-
from stac_fastapi.sfeos_helpers.utilities import validate_refresh
4547
from stac_fastapi.types.errors import ConflictError, NotFoundError
4648
from stac_fastapi.types.stac import Collection, Item
4749

stac_fastapi/opensearch/stac_fastapi/opensearch/database_logic.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,15 @@
3838
ITEM_INDICES,
3939
ITEMS_INDEX_PREFIX,
4040
Geometry,
41+
)
42+
from stac_fastapi.sfeos_helpers.utilities import (
4143
index_alias_by_collection_id,
4244
index_by_collection_id,
4345
indices,
4446
mk_actions,
4547
mk_item_id,
48+
validate_refresh,
4649
)
47-
from stac_fastapi.sfeos_helpers.utilities import validate_refresh
4850
from stac_fastapi.types.errors import ConflictError, NotFoundError
4951
from stac_fastapi.types.stac import Collection, Item
5052

stac_fastapi/sfeos_helpers/stac_fastapi/sfeos_helpers/database_logic_helpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
ES_ITEMS_SETTINGS,
1010
ITEMS_INDEX_PREFIX,
1111
Geometry,
12-
index_alias_by_collection_id,
1312
)
13+
from stac_fastapi.sfeos_helpers.utilities import index_alias_by_collection_id
1414

1515

1616
async def create_index_templates_shared(settings: Any) -> None:

stac_fastapi/sfeos_helpers/stac_fastapi/sfeos_helpers/mappings.py

Lines changed: 1 addition & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
"""Shared mappings for stac-fastapi elasticsearch and opensearch backends."""
22

33
import os
4-
from functools import lru_cache
5-
from typing import Any, Dict, List, Literal, Optional, Protocol
6-
7-
from stac_fastapi.types.stac import Item
4+
from typing import Any, Dict, Literal, Protocol
85

96

107
# stac_pydantic classes extend _GeometryBase, which doesn't have a type field,
@@ -238,90 +235,3 @@ class Geometry(Protocol): # noqa
238235
"geo_shape": "object",
239236
"nested": "array",
240237
}
241-
242-
243-
@lru_cache(256)
244-
def index_by_collection_id(collection_id: str) -> str:
245-
"""
246-
Translate a collection id into an Elasticsearch index name.
247-
248-
Args:
249-
collection_id (str): The collection id to translate into an index name.
250-
251-
Returns:
252-
str: The index name derived from the collection id.
253-
"""
254-
cleaned = collection_id.translate(_ES_INDEX_NAME_UNSUPPORTED_CHARS_TABLE)
255-
return (
256-
f"{ITEMS_INDEX_PREFIX}{cleaned.lower()}_{collection_id.encode('utf-8').hex()}"
257-
)
258-
259-
260-
@lru_cache(256)
261-
def index_alias_by_collection_id(collection_id: str) -> str:
262-
"""
263-
Translate a collection id into an Elasticsearch index alias.
264-
265-
Args:
266-
collection_id (str): The collection id to translate into an index alias.
267-
268-
Returns:
269-
str: The index alias derived from the collection id.
270-
"""
271-
cleaned = collection_id.translate(_ES_INDEX_NAME_UNSUPPORTED_CHARS_TABLE)
272-
return f"{ITEMS_INDEX_PREFIX}{cleaned}"
273-
274-
275-
def indices(collection_ids: Optional[List[str]]) -> str:
276-
"""
277-
Get a comma-separated string of index names for a given list of collection ids.
278-
279-
Args:
280-
collection_ids: A list of collection ids.
281-
282-
Returns:
283-
A string of comma-separated index names. If `collection_ids` is empty, returns the default indices.
284-
"""
285-
return (
286-
",".join(map(index_alias_by_collection_id, collection_ids))
287-
if collection_ids
288-
else ITEM_INDICES
289-
)
290-
291-
292-
def mk_item_id(item_id: str, collection_id: str) -> str:
293-
"""Create the document id for an Item in Elasticsearch.
294-
295-
Args:
296-
item_id (str): The id of the Item.
297-
collection_id (str): The id of the Collection that the Item belongs to.
298-
299-
Returns:
300-
str: The document id for the Item, combining the Item id and the Collection id, separated by a `|` character.
301-
"""
302-
return f"{item_id}|{collection_id}"
303-
304-
305-
def mk_actions(collection_id: str, processed_items: List[Item]) -> List[Dict[str, Any]]:
306-
"""Create Elasticsearch bulk actions for a list of processed items.
307-
308-
Args:
309-
collection_id (str): The identifier for the collection the items belong to.
310-
processed_items (List[Item]): The list of processed items to be bulk indexed.
311-
312-
Returns:
313-
List[Dict[str, Union[str, Dict]]]: The list of bulk actions to be executed,
314-
each action being a dictionary with the following keys:
315-
- `_index`: the index to store the document in.
316-
- `_id`: the document's identifier.
317-
- `_source`: the source of the document.
318-
"""
319-
index_alias = index_alias_by_collection_id(collection_id)
320-
return [
321-
{
322-
"_index": index_alias,
323-
"_id": mk_item_id(item["id"], item["collection"]),
324-
"_source": item,
325-
}
326-
for item in processed_items
327-
]

stac_fastapi/sfeos_helpers/stac_fastapi/sfeos_helpers/utilities.py

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
"""Shared utilities functions for stac-fastapi elasticsearch and opensearch backends."""
22
import logging
3-
from typing import Union
3+
from functools import lru_cache
4+
from typing import Any, Dict, List, Optional, Union
45

56
from stac_fastapi.core.utilities import get_bool_env
67

8+
# Import constants from mappings
9+
from stac_fastapi.sfeos_helpers.mappings import (
10+
_ES_INDEX_NAME_UNSUPPORTED_CHARS_TABLE,
11+
ITEM_INDICES,
12+
ITEMS_INDEX_PREFIX,
13+
)
14+
from stac_fastapi.types.stac import Item
15+
716

817
def validate_refresh(value: Union[str, bool]) -> str:
918
"""
@@ -43,3 +52,90 @@ def validate_refresh(value: Union[str, bool]) -> str:
4352
f"Invalid value for `refresh`: '{value}'. Expected 'true', 'false', or 'wait_for'. Defaulting to 'false'."
4453
)
4554
return "false"
55+
56+
57+
@lru_cache(256)
58+
def index_by_collection_id(collection_id: str) -> str:
59+
"""
60+
Translate a collection id into an Elasticsearch index name.
61+
62+
Args:
63+
collection_id (str): The collection id to translate into an index name.
64+
65+
Returns:
66+
str: The index name derived from the collection id.
67+
"""
68+
cleaned = collection_id.translate(_ES_INDEX_NAME_UNSUPPORTED_CHARS_TABLE)
69+
return (
70+
f"{ITEMS_INDEX_PREFIX}{cleaned.lower()}_{collection_id.encode('utf-8').hex()}"
71+
)
72+
73+
74+
@lru_cache(256)
75+
def index_alias_by_collection_id(collection_id: str) -> str:
76+
"""
77+
Translate a collection id into an Elasticsearch index alias.
78+
79+
Args:
80+
collection_id (str): The collection id to translate into an index alias.
81+
82+
Returns:
83+
str: The index alias derived from the collection id.
84+
"""
85+
cleaned = collection_id.translate(_ES_INDEX_NAME_UNSUPPORTED_CHARS_TABLE)
86+
return f"{ITEMS_INDEX_PREFIX}{cleaned}"
87+
88+
89+
def indices(collection_ids: Optional[List[str]]) -> str:
90+
"""
91+
Get a comma-separated string of index names for a given list of collection ids.
92+
93+
Args:
94+
collection_ids: A list of collection ids.
95+
96+
Returns:
97+
A string of comma-separated index names. If `collection_ids` is empty, returns the default indices.
98+
"""
99+
return (
100+
",".join(map(index_alias_by_collection_id, collection_ids))
101+
if collection_ids
102+
else ITEM_INDICES
103+
)
104+
105+
106+
def mk_item_id(item_id: str, collection_id: str) -> str:
107+
"""Create the document id for an Item in Elasticsearch.
108+
109+
Args:
110+
item_id (str): The id of the Item.
111+
collection_id (str): The id of the Collection that the Item belongs to.
112+
113+
Returns:
114+
str: The document id for the Item, combining the Item id and the Collection id, separated by a `|` character.
115+
"""
116+
return f"{item_id}|{collection_id}"
117+
118+
119+
def mk_actions(collection_id: str, processed_items: List[Item]) -> List[Dict[str, Any]]:
120+
"""Create Elasticsearch bulk actions for a list of processed items.
121+
122+
Args:
123+
collection_id (str): The identifier for the collection the items belong to.
124+
processed_items (List[Item]): The list of processed items to be bulk indexed.
125+
126+
Returns:
127+
List[Dict[str, Union[str, Dict]]]: The list of bulk actions to be executed,
128+
each action being a dictionary with the following keys:
129+
- `_index`: the index to store the document in.
130+
- `_id`: the document's identifier.
131+
- `_source`: the source of the document.
132+
"""
133+
index_alias = index_alias_by_collection_id(collection_id)
134+
return [
135+
{
136+
"_index": index_alias,
137+
"_id": mk_item_id(item["id"], item["collection"]),
138+
"_source": item,
139+
}
140+
for item in processed_items
141+
]

stac_fastapi/tests/database/test_database.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
COLLECTIONS_INDEX,
88
ES_COLLECTIONS_MAPPINGS,
99
ES_ITEMS_MAPPINGS,
10-
index_alias_by_collection_id,
1110
)
11+
from stac_fastapi.sfeos_helpers.utilities import index_alias_by_collection_id
1212

1313
from ..conftest import MockRequest, database
1414

0 commit comments

Comments
 (0)