Skip to content

Commit 41e7b44

Browse files
authored
Merge pull request #39 from stac-utils/stac_to_db
Clean up transactions logic
2 parents 03f3cba + 7762bb7 commit 41e7b44

File tree

2 files changed

+33
-56
lines changed

2 files changed

+33
-56
lines changed

stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/serializers.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
"""Serializers."""
22
import abc
3+
from datetime import datetime
34
from typing import TypedDict
45

56
import attr
7+
from stac_pydantic.shared import DATETIME_RFC339
68

79
from stac_fastapi.types import stac as stac_types
810
from stac_fastapi.types.links import CollectionLinks, ItemLinks, resolve_links
@@ -22,6 +24,30 @@ def db_to_stac(cls, item: dict, base_url: str) -> TypedDict:
2224
class ItemSerializer(Serializer):
2325
"""Serialization methods for STAC items."""
2426

27+
@classmethod
28+
def stac_to_db(cls, stac_data: TypedDict, base_url: str) -> stac_types.Item:
29+
"""Transform STAC Item to database-ready STAC Item."""
30+
item_links = ItemLinks(
31+
collection_id=stac_data["collection"],
32+
item_id=stac_data["id"],
33+
base_url=base_url,
34+
).create_links()
35+
stac_data["links"] = item_links
36+
37+
# elasticsearch doesn't like the fact that some values are float and some were int
38+
if "eo:bands" in stac_data["properties"]:
39+
for wave in stac_data["properties"]["eo:bands"]:
40+
for k, v in wave.items():
41+
if type(v) != str:
42+
v = float(v)
43+
wave.update({k: v})
44+
45+
now = datetime.utcnow().strftime(DATETIME_RFC339)
46+
if "created" not in stac_data["properties"]:
47+
stac_data["properties"]["created"] = str(now)
48+
stac_data["properties"]["updated"] = str(now)
49+
return stac_data
50+
2551
@classmethod
2652
def db_to_stac(cls, item: dict, base_url: str) -> stac_types.Item:
2753
"""Transform database model to stac item."""

stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/transactions.py

Lines changed: 7 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from stac_fastapi.types import stac as stac_types
1919
from stac_fastapi.types.core import BaseTransactionsClient
2020
from stac_fastapi.types.errors import ConflictError, ForeignKeyError, NotFoundError
21-
from stac_fastapi.types.links import CollectionLinks, ItemLinks
21+
from stac_fastapi.types.links import CollectionLinks
2222

2323
logger = logging.getLogger(__name__)
2424

@@ -69,19 +69,6 @@ def create_item(self, model: stac_types.Item, **kwargs):
6969
return return_msg
7070

7171
# If a single item is posted
72-
item_links = ItemLinks(
73-
collection_id=model["collection"], item_id=model["id"], base_url=base_url
74-
).create_links()
75-
model["links"] = item_links
76-
77-
# elasticsearch doesn't like the fact that some values are float and some were int
78-
if "eo:bands" in model["properties"]:
79-
for wave in model["properties"]["eo:bands"]:
80-
for k, v in wave.items():
81-
if type(v) != str:
82-
v = float(v)
83-
wave.update({k: v})
84-
8572
if not self.client.exists(index="stac_collections", id=model["collection"]):
8673
raise ForeignKeyError(f"Collection {model['collection']} does not exist")
8774

@@ -90,12 +77,10 @@ def create_item(self, model: stac_types.Item, **kwargs):
9077
f"Item {model['id']} in collection {model['collection']} already exists"
9178
)
9279

93-
now = datetime.utcnow().strftime(DATETIME_RFC339)
94-
if "created" not in model["properties"]:
95-
model["properties"]["created"] = str(now)
80+
data = ItemSerializer.stac_to_db(model, base_url)
9681

9782
self.client.index(
98-
index="stac_items", doc_type="_doc", id=model["id"], document=model
83+
index="stac_items", doc_type="_doc", id=model["id"], document=data
9984
)
10085
return ItemSerializer.db_to_stac(model, base_url)
10186

@@ -174,33 +159,8 @@ def __attrs_post_init__(self):
174159
settings = ElasticsearchSettings()
175160
self.client = settings.create_client
176161

177-
def _create_item_index(self):
178-
mapping = {
179-
"mappings": {
180-
"properties": {
181-
"geometry": {"type": "geo_shape"},
182-
"id": {"type": "text", "fields": {"keyword": {"type": "keyword"}}},
183-
"properties__datetime": {
184-
"type": "text",
185-
"fields": {"keyword": {"type": "keyword"}},
186-
},
187-
}
188-
}
189-
}
190-
191-
_ = self.client.indices.create(
192-
index="stac_items",
193-
body=mapping,
194-
ignore=400, # ignore 400 already exists code
195-
)
196-
197162
def _preprocess_item(self, model: stac_types.Item, base_url) -> stac_types.Item:
198163
"""Preprocess items to match data model."""
199-
item_links = ItemLinks(
200-
collection_id=model["collection"], item_id=model["id"], base_url=base_url
201-
).create_links()
202-
model["links"] = item_links
203-
204164
if not self.client.exists(index="stac_collections", id=model["collection"]):
205165
raise ForeignKeyError(f"Collection {model['collection']} does not exist")
206166

@@ -209,18 +169,8 @@ def _preprocess_item(self, model: stac_types.Item, base_url) -> stac_types.Item:
209169
f"Item {model['id']} in collection {model['collection']} already exists"
210170
)
211171

212-
now = datetime.utcnow().strftime(DATETIME_RFC339)
213-
if "created" not in model["properties"]:
214-
model["properties"]["created"] = str(now)
215-
216-
# elasticsearch doesn't like the fact that some values are float and some were int
217-
if "eo:bands" in model["properties"]:
218-
for wave in model["properties"]["eo:bands"]:
219-
for k, v in wave.items():
220-
if type(v) != str:
221-
v = float(v)
222-
wave.update({k: v})
223-
return model
172+
item = ItemSerializer.stac_to_db(model, base_url)
173+
return item
224174

225175
def bulk_sync(self, processed_items):
226176
"""Elasticsearch bulk insertion."""
@@ -231,7 +181,8 @@ def bulk_sync(self, processed_items):
231181

232182
def bulk_item_insert(self, items: Items, **kwargs) -> str:
233183
"""Bulk item insertion using es."""
234-
self._create_item_index()
184+
transactions_client = TransactionsClient()
185+
transactions_client._create_item_index()
235186
try:
236187
base_url = str(kwargs["request"].base_url)
237188
except Exception:

0 commit comments

Comments
 (0)