Skip to content

Commit defac44

Browse files
authored
Merge pull request #13 from jonhealy1/dev
Merge dev
2 parents f825d0c + c08c21a commit defac44

File tree

13 files changed

+230
-88
lines changed

13 files changed

+230
-88
lines changed

.github/workflows/cicd.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
name: stac-fastapi-nosql
22
on:
33
push:
4-
branches: [ main ]
4+
branches: [ main, dev ]
55
pull_request:
6-
branches: [ main ]
6+
branches: [ main, dev ]
77

88
jobs:
99
test:

.github/workflows/publish-to-pypi.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: Publish Python distributions to PyPI
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
jobs:
9+
build-n-publish:
10+
name: Build and publish Python distributions to PyPI and TestPyPI
11+
runs-on: ubuntu-18.04
12+
13+
steps:
14+
- uses: actions/checkout@master
15+
- name: Set up Python 3.8
16+
uses: actions/setup-python@v1
17+
with:
18+
python-version: 3.8
19+
20+
- name: Install pypa/build
21+
run: >-
22+
python -m
23+
pip install
24+
build
25+
--user
26+
- name: Build a binary wheel and a source tarball
27+
run: >-
28+
python -m
29+
build
30+
--sdist
31+
--wheel
32+
--outdir dist/
33+
stac_fastapi/mongo
34+
- name: Publish distribution to PyPI
35+
# if: startsWith(github.ref, 'refs/tags')
36+
uses: pypa/gh-action-pypi-publish@v1.4.2
37+
with:
38+
password: ${{ secrets.PYPI_API_TOKEN }}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
name: Publish Python distributions to PyPI
2+
3+
on:
4+
push:
5+
branches:
6+
- dev
7+
8+
jobs:
9+
build-n-publish:
10+
name: Build and publish Python distributions to PyPI and TestPyPI
11+
runs-on: ubuntu-18.04
12+
13+
steps:
14+
- uses: actions/checkout@master
15+
- name: Set up Python 3.8
16+
uses: actions/setup-python@v1
17+
with:
18+
python-version: 3.8
19+
20+
- name: Install pypa/build
21+
run: >-
22+
python -m
23+
pip install
24+
build
25+
--user
26+
- name: Build a binary wheel and a source tarball
27+
run: >-
28+
python -m
29+
build
30+
--sdist
31+
--wheel
32+
--outdir dist/
33+
stac_fastapi/mongo
34+
- name: Publish distribution to Test PyPI
35+
# if: startsWith(github.ref, 'refs/tags')
36+
uses: pypa/gh-action-pypi-publish@v1.4.2
37+
with:
38+
password: ${{ secrets.TEST_PYPI_API_TOKEN }}
39+
repository_url: https://test.pypi.org/legacy/

VERSION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2.3.0

stac_fastapi/elasticsearch/setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"elasticsearch[async]",
1717
"elasticsearch-dsl",
1818
"pystac[validation]",
19+
"uvicorn",
1920
]
2021

2122
extra_reqs = {
@@ -47,7 +48,7 @@
4748
keywords="STAC FastAPI COG",
4849
author=u"Arturo Engineering",
4950
author_email="engineering@arturo.ai",
50-
url="https://github.com/stac-utils/stac-fastapi",
51+
url="https://github.com/jonhealy1/stac-fastapi-nosql",
5152
license="MIT",
5253
packages=find_namespace_packages(exclude=["alembic", "tests", "scripts"]),
5354
zip_safe=False,

stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def get_collection(self, collection_id: str, **kwargs) -> Collection:
9494
return self.collection_serializer.db_to_stac(collection["_source"], base_url)
9595

9696
def item_collection(
97-
self, collection_id: str, limit: int = 10, token: str = None, **kwargs
97+
self, collection_id: str, limit: int = 10, **kwargs
9898
) -> ItemCollection:
9999
"""Read an item collection from the database."""
100100
links = []

stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/serializers.py

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,17 @@ def db_to_stac(cls, item: dict, base_url: str) -> stac_types.Item:
3737

3838
return stac_types.Item(
3939
type="Feature",
40-
stac_version=item["stac_version"],
41-
stac_extensions=item["stac_extensions"] or [],
42-
id=item["id"],
43-
collection=item["collection"],
44-
geometry=item["geometry"],
45-
bbox=item["bbox"],
46-
properties=item["properties"],
47-
links=item_links,
48-
assets=item["assets"],
40+
stac_version=item["stac_version"] if "stac_version" in item else "",
41+
stac_extensions=item["stac_extensions"]
42+
if "stac_extensions" in item
43+
else [],
44+
id=item_id,
45+
collection=item["collection"] if "collection" in item else "",
46+
geometry=item["geometry"] if "geometry" in item else {},
47+
bbox=item["bbox"] if "bbox" in item else [],
48+
properties=item["properties"] if "properties" in item else {},
49+
links=item_links if "links" in item else [],
50+
assets=item["assets"] if "assets" in item else {},
4951
)
5052

5153

@@ -66,14 +68,20 @@ def db_to_stac(cls, collection: dict, base_url: str) -> stac_types.Collection:
6668
return stac_types.Collection(
6769
type="Collection",
6870
id=collection["id"],
69-
stac_extensions=collection["stac_extensions"] or [],
70-
stac_version=collection["stac_version"],
71-
title=collection["title"],
72-
description=collection["description"],
73-
keywords=collection["keywords"],
74-
license=collection["license"],
75-
providers=collection["providers"],
76-
summaries=collection["summaries"],
77-
extent=collection["extent"],
71+
stac_extensions=collection["stac_extensions"]
72+
if "stac_extensions" in collection
73+
else [],
74+
stac_version=collection["stac_version"]
75+
if "stac_version" in collection
76+
else "",
77+
title=collection["title"] if "title" in collection else "",
78+
description=collection["description"]
79+
if "description" in collection
80+
else "",
81+
keywords=collection["keywords"] if "keywords" in collection else [],
82+
license=collection["license"] if "license" in collection else "",
83+
providers=collection["providers"] if "providers" in collection else {},
84+
summaries=collection["summaries"] if "summaries" in collection else {},
85+
extent=collection["extent"] if "extent" in collection else {},
7886
links=collection_links,
7987
)

stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/transactions.py

Lines changed: 81 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,26 @@ class TransactionsClient(BaseTransactionsClient):
3131
settings = ElasticsearchSettings()
3232
client = settings.create_client
3333

34+
def _create_item_index(self):
35+
mapping = {
36+
"mappings": {
37+
"properties": {
38+
"geometry": {"type": "geo_shape"},
39+
"id": {"type": "text", "fields": {"keyword": {"type": "keyword"}}},
40+
"properties__datetime": {
41+
"type": "text",
42+
"fields": {"keyword": {"type": "keyword"}},
43+
},
44+
}
45+
}
46+
}
47+
48+
_ = self.client.indices.create(
49+
index="stac_items",
50+
body=mapping,
51+
ignore=400, # ignore 400 already exists code
52+
)
53+
3454
def create_item(self, model: stac_types.Item, **kwargs):
3555
"""Create item."""
3656
base_url = str(kwargs["request"].base_url)
@@ -59,24 +79,7 @@ def create_item(self, model: stac_types.Item, **kwargs):
5979
if "created" not in model["properties"]:
6080
model["properties"]["created"] = str(now)
6181

62-
mapping = {
63-
"mappings": {
64-
"properties": {
65-
"geometry": {"type": "geo_shape"},
66-
"id": {"type": "text", "fields": {"keyword": {"type": "keyword"}}},
67-
"properties__datetime": {
68-
"type": "text",
69-
"fields": {"keyword": {"type": "keyword"}},
70-
},
71-
}
72-
}
73-
}
74-
75-
_ = self.client.indices.create(
76-
index="stac_items",
77-
body=mapping,
78-
ignore=400, # ignore 400 already exists code
79-
)
82+
self._create_item_index()
8083

8184
self.client.index(
8285
index="stac_items", doc_type="_doc", id=model["id"], document=model
@@ -91,6 +94,8 @@ def create_collection(self, model: stac_types.Collection, **kwargs):
9194
).create_links()
9295
model["links"] = collection_links
9396

97+
self._create_item_index()
98+
9499
if self.client.exists(index="stac_collections", id=model["id"]):
95100
raise ConflictError(f"Collection {model['id']} already exists")
96101
self.client.index(
@@ -155,39 +160,79 @@ def __attrs_post_init__(self):
155160
settings = ElasticsearchSettings()
156161
self.client = settings.create_client
157162

163+
def _create_item_index(self):
164+
mapping = {
165+
"mappings": {
166+
"properties": {
167+
"geometry": {"type": "geo_shape"},
168+
"id": {"type": "text", "fields": {"keyword": {"type": "keyword"}}},
169+
"properties__datetime": {
170+
"type": "text",
171+
"fields": {"keyword": {"type": "keyword"}},
172+
},
173+
}
174+
}
175+
}
176+
177+
_ = self.client.indices.create(
178+
index="stac_items",
179+
body=mapping,
180+
ignore=400, # ignore 400 already exists code
181+
)
182+
158183
def _preprocess_item(self, model: stac_types.Item, base_url) -> stac_types.Item:
159184
"""Preprocess items to match data model."""
160185
item_links = ItemLinks(
161186
collection_id=model["collection"], item_id=model["id"], base_url=base_url
162187
).create_links()
163188
model["links"] = item_links
164189

165-
# with self.client.start_session(causal_consistency=True) as session:
166-
# error_check = ErrorChecks(session=session, client=self.client)
167-
# error_check._check_collection_foreign_key(model)
168-
# error_check._check_item_conflict(model)
169-
# now = datetime.utcnow().strftime(DATETIME_RFC339)
170-
# if "created" not in model["properties"]:
171-
# model["properties"]["created"] = str(now)
172-
# return model
190+
if not self.client.exists(index="stac_collections", id=model["collection"]):
191+
raise ForeignKeyError(f"Collection {model['collection']} does not exist")
192+
193+
if self.client.exists(index="stac_items", id=model["id"]):
194+
raise ConflictError(
195+
f"Item {model['id']} in collection {model['collection']} already exists"
196+
)
197+
198+
now = datetime.utcnow().strftime(DATETIME_RFC339)
199+
if "created" not in model["properties"]:
200+
model["properties"]["created"] = str(now)
201+
202+
# elasticsearch doesn't like the fact that some values are float and some were int
203+
if "eo:bands" in model["properties"]:
204+
for wave in model["properties"]["eo:bands"]:
205+
for k, v in wave.items():
206+
if type(v) != str:
207+
v = float(v)
208+
wave.update({k: v})
209+
return model
173210

174211
def bulk_item_insert(self, items: Items, **kwargs) -> str:
175212
"""Bulk item insertion using es."""
213+
self._create_item_index()
176214
try:
177215
base_url = str(kwargs["request"].base_url)
178216
except Exception:
179217
base_url = ""
180218
processed_items = [self._preprocess_item(item, base_url) for item in items]
181219
return_msg = f"Successfully added {len(processed_items)} items."
182-
# with self.client.start_session(causal_consistency=True) as session:
183-
# self.item_table.insert_many(processed_items, session=session)
184-
# return return_msg
185220

186-
helpers.bulk(
187-
self.client,
188-
processed_items,
189-
index="stac_items",
190-
doc_type="_doc",
191-
request_timeout=200,
192-
)
221+
# helpers.bulk(
222+
# self.client,
223+
# processed_items,
224+
# index="stac_items",
225+
# doc_type="_doc",
226+
# request_timeout=200,
227+
# )
228+
229+
def bulk_sync(processed_items):
230+
actions = [
231+
{"_index": "stac_items", "_source": item} for item in processed_items
232+
]
233+
234+
helpers.bulk(self.client, actions)
235+
236+
bulk_sync(processed_items)
237+
193238
return return_msg
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
"""library version."""
2-
__version__ = "2.1.1"
2+
__version__ = "2.3.0"

stac_fastapi/elasticsearch/tests/clients/test_elasticsearch.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import time
12
import uuid
23
from copy import deepcopy
34
from typing import Callable
@@ -256,7 +257,7 @@ def test_delete_item(
256257
es_core.get_item(item["id"], item["collection"], request=MockStarletteRequest)
257258

258259

259-
@pytest.mark.skip(reason="bulk not implemented")
260+
# @pytest.mark.skip(reason="might need a larger timeout")
260261
def test_bulk_item_insert(
261262
es_core: CoreCrudClient,
262263
es_transactions: TransactionsClient,
@@ -274,18 +275,18 @@ def test_bulk_item_insert(
274275
_item["id"] = str(uuid.uuid4())
275276
items.append(_item)
276277

277-
fc = es_core.item_collection(coll["id"], request=MockStarletteRequest)
278-
assert len(fc["features"]) == 0
278+
# fc = es_core.item_collection(coll["id"], request=MockStarletteRequest)
279+
# assert len(fc["features"]) == 0
279280

280281
es_bulk_transactions.bulk_item_insert(items=items)
281-
282+
time.sleep(3)
282283
fc = es_core.item_collection(coll["id"], request=MockStarletteRequest)
283-
assert len(fc["features"]) == 10
284+
assert len(fc["features"]) >= 10
284285

285-
for item in items:
286-
es_transactions.delete_item(
287-
item["id"], item["collection"], request=MockStarletteRequest
288-
)
286+
# for item in items:
287+
# es_transactions.delete_item(
288+
# item["id"], item["collection"], request=MockStarletteRequest
289+
# )
289290

290291

291292
@pytest.mark.skip(reason="Not working")

0 commit comments

Comments
 (0)