Skip to content

Adding optional page argument to all_collections endpoint. #116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,20 @@ class CoreClient(AsyncBaseCoreClient):
async def all_collections(self, **kwargs) -> Collections:
"""Read all collections from the database."""
base_url = str(kwargs["request"].base_url)
request = kwargs["request"]
page_str = None
if request.query_params is not None:
page_str = request.query_params.get("page")
pageint = 1
if page_str is not None:
pageint = int(page_str)
if pageint < 1:
pageint = 1

return Collections(
collections=[
self.collection_serializer.db_to_stac(c, base_url=base_url)
for c in await self.database.get_all_collections()
for c in await self.database.get_all_collections(page=pageint)
],
links=[
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
NumType = Union[float, int]

COLLECTIONS_INDEX = "collections"
COLLECTIONS_PAGE_SIZE = 1000
ITEMS_INDEX_PREFIX = "items_"

DEFAULT_INDICES = f"*,-*kibana*,-{COLLECTIONS_INDEX}"
Expand Down Expand Up @@ -210,11 +211,12 @@ class DatabaseLogic:

"""CORE LOGIC"""

async def get_all_collections(self) -> Iterable[Dict[str, Any]]:
async def get_all_collections(self, page: int = 1) -> Iterable[Dict[str, Any]]:
"""Database logic to retrieve a list of all collections."""
# https://github.com/stac-utils/stac-fastapi-elasticsearch/issues/65
# collections should be paginated, but at least return more than the default 10 for now
collections = await self.client.search(index=COLLECTIONS_INDEX, size=1000)
results_after = (page - 1) * COLLECTIONS_PAGE_SIZE
collections = await self.client.search(
index=COLLECTIONS_INDEX, size=COLLECTIONS_PAGE_SIZE, from_=results_after
)
return (c["_source"] for c in collections["hits"]["hits"])

async def get_one_item(self, collection_id: str, item_id: str) -> Dict:
Expand Down Expand Up @@ -428,7 +430,7 @@ def sync_prep_create_item(self, item: Item, base_url: str) -> Item:

return self.item_serializer.stac_to_db(item, base_url)

async def create_item(self, item: Item, refresh: bool = False):
async def create_item(self, item: Item, refresh: bool = True):
"""Database logic for creating one item."""
# todo: check if collection exists, but cache
item_id = item["id"]
Expand All @@ -445,9 +447,7 @@ async def create_item(self, item: Item, refresh: bool = False):
f"Item {item_id} in collection {collection_id} already exists"
)

async def delete_item(
self, item_id: str, collection_id: str, refresh: bool = False
):
async def delete_item(self, item_id: str, collection_id: str, refresh: bool = True):
"""Database logic for deleting one item."""
try:
await self.client.delete(
Expand All @@ -460,7 +460,7 @@ async def delete_item(
f"Item {item_id} in collection {collection_id} not found"
)

async def create_collection(self, collection: Collection, refresh: bool = False):
async def create_collection(self, collection: Collection, refresh: bool = True):
"""Database logic for creating one collection."""
collection_id = collection["id"]

Expand All @@ -487,7 +487,7 @@ async def find_collection(self, collection_id: str) -> Collection:

return collection["_source"]

async def delete_collection(self, collection_id: str, refresh: bool = False):
async def delete_collection(self, collection_id: str, refresh: bool = True):
"""Database logic for deleting one collection."""
await self.find_collection(collection_id=collection_id)
await self.client.delete(
Expand Down
1 change: 1 addition & 0 deletions stac_fastapi/elasticsearch/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def __init__(self, item, collection):

class MockRequest:
base_url = "http://test-server"
query_params = None

def __init__(
self, method: str = "GET", url: str = "XXXX", app: Optional[Any] = None
Expand Down
28 changes: 28 additions & 0 deletions stac_fastapi/elasticsearch/tests/resources/test_collection.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pystac
import pytest


async def test_create_and_delete_collection(app_client, load_test_data):
Expand All @@ -13,6 +14,33 @@ async def test_create_and_delete_collection(app_client, load_test_data):
assert resp.status_code == 200


@pytest.mark.skip(
reason="paginating collections takes a long time to test, skip it if you haven't changed anything"
)
async def test_create_paginate_collections(app_client, load_test_data):
"""Test creation and pagination of collections"""
test_collection = load_test_data("test_collection.json")
test_collection["id"] = "test"

for i in range(1, 1005):
test_collection["id"] = "test_" + str(i)
resp = await app_client.post("/collections", json=test_collection)
assert resp.status_code == 200

resp = await app_client.get("/collections?page=2") # , params={"page": 2})
resp_json = resp.json()
collcount = len(resp_json["collections"])
assert collcount == 4

""" this many deletes all at once tends to error out after a certain point """


# for i in range(1, 1005):
# test_id = "test_" + str(i)
# resp = await app_client.delete(f"/collections/{test_id}")
# assert resp.status_code == 200


async def test_create_collection_conflict(app_client, ctx):
"""Test creation of a collection which already exists"""
# This collection ID is created in the fixture, so this should be a conflict
Expand Down