Skip to content

optimize row count #2

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 5 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
13 changes: 8 additions & 5 deletions stac_api/app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from fastapi import FastAPI
from fastapi import FastAPI, APIRouter
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

Expand All @@ -9,11 +9,14 @@

app = FastAPI()
app.debug = settings.DEBUG
app.include_router(mgmt.router)
app.include_router(conformance.router)
app.include_router(collection.router)
app.include_router(item.router)

stac_router = APIRouter()
stac_router.include_router(conformance.router)
stac_router.include_router(collection.router)
stac_router.include_router(item.router)

app.include_router(mgmt.router)
app.include_router(stac_router)

@app.on_event("startup")
async def on_startup():
Expand Down
8 changes: 5 additions & 3 deletions stac_api/clients/collection_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from fastapi import Depends
from sqlalchemy.orm import Session
from sqlalchemy.sql import func
from sqlakeyset import get_page, Page

from .base_crud import BaseCrudClient
Expand Down Expand Up @@ -34,14 +35,15 @@ def get_item_collection(
) -> Tuple[Page, int]:
"""Read an item collection from the database"""
try:
collection_children = (
query = (
self.lookup_id(collection_id)
.first()
.children.order_by(database.Item.datetime.desc(), database.Item.id)
)
count = collection_children.count()
count_query = query.statement.with_only_columns([func.count()]).order_by(None)
count = query.session.execute(count_query).scalar()
token = self.pagination_client.get(token) if token else token
page = get_page(collection_children, per_page=limit, page=(token or False))
page = get_page(query, per_page=limit, page=(token or False))
# Create dynamic attributes for each page
page.next = (
self.pagination_client.insert(keyset=page.paging.bookmark_next)
Expand Down
4 changes: 3 additions & 1 deletion stac_api/clients/item_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import geoalchemy2 as ga
import sqlalchemy as sa
from sqlalchemy.orm import Session
from sqlalchemy.sql import func
from sqlakeyset import get_page, Page

from .base_crud import BaseCrudClient
Expand Down Expand Up @@ -110,7 +111,8 @@ def stac_search(self, search_request: schemas.STACSearch) -> Tuple[Page, int]:
query = query.filter(op.operator(field, value))

try:
count = query.count()
count_query = query.statement.with_only_columns([func.count()]).order_by(None)
count = query.session.execute(count_query).scalar()
page = get_page(query, per_page=search_request.limit, page=token)
# Create dynamic attributes for each page
page.next = (
Expand Down
2 changes: 1 addition & 1 deletion stac_api/resources/conformance.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def landing_page(
):
resp = LandingPage(
title="Arturo STAC API",
description="Arturo raster datastore",
description="Arturo STAC API",
links=[
Link(rel=Relations.self, type=MimeTypes.json, href=base_url),
Link(
Expand Down
2 changes: 0 additions & 2 deletions stac_api/resources/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,6 @@ def search_items_get(
query: Optional[str] = Query(None),
token: Optional[str] = None,
fields: Optional[List[str]] = Depends(parse_list_factory("fields")),
cloudfront_ttl: Optional[int] = 2628000,
sortby: Optional[str] = Depends(parse_list_factory("sortby")),
crud_client: ItemCrudClient = Depends(item_crud_client_factory),
base_url: str = Depends(discover_base_url),
Expand All @@ -310,7 +309,6 @@ def search_items_get(
"bbox": bbox,
"limit": limit,
"token": token,
"cloudfront_ttl": cloudfront_ttl,
"query": json.loads(query) if query else query,
}
if datetime:
Expand Down