Skip to content

Commit

Permalink
Tidy up the table response model
Browse files Browse the repository at this point in the history
  • Loading branch information
BielStela committed Sep 12, 2024
1 parent 8fb0e89 commit 5b543e1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
9 changes: 9 additions & 0 deletions api/app/models/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,12 @@ def to_sql_query(self, table_name: str) -> str:
)
)
return str(query.compile(compile_kwargs={"literal_binds": True}))


class TableResultColumn(BaseModel):
column: Annotated[str, Field(title="column", description="Column name")]
values: Annotated[list, Field(description="Check dataset metadata for type info")]


class TableResults(BaseModel):
table: list[TableResultColumn]
7 changes: 3 additions & 4 deletions api/app/routers/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import shapely
from fastapi import APIRouter, Depends, HTTPException, Path, Query
from fastapi.params import Body
from fastapi.responses import ORJSONResponse
from geojson_pydantic import Feature
from h3 import H3CellError
from h3ronpy.polars import cells_to_string
Expand All @@ -19,7 +18,7 @@
from starlette.responses import Response

from app.config.config import get_settings
from app.models.grid import MultiDatasetMeta, TableFilters
from app.models.grid import MultiDatasetMeta, TableFilters, TableResults

log = logging.getLogger("uvicorn.error")

Expand Down Expand Up @@ -118,7 +117,7 @@ def read_table(
level: Annotated[int, Query(..., description="Tile level at which the query will be computed")],
filters: TableFilters = Depends(),
geojson: Annotated[Feature | None, Body(description="GeoJSON feature used to filter the cells.")] = None,
) -> ORJSONResponse:
) -> TableResults:
"""Query tile dataset and return table data"""
files_path = pathlib.Path(get_settings().grid_tiles_path) / str(level)
if not files_path.exists():
Expand All @@ -144,4 +143,4 @@ def read_table(
log.exception(e)
raise HTTPException(status_code=422, detail=str(e)) from None

return ORJSONResponse(res.to_dict(as_series=False))
return TableResults(table=[{"column": k, "values": v} for k, v in res.to_dict(as_series=False).items()])
25 changes: 11 additions & 14 deletions api/tests/test_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,11 @@ def test_grid_table(grid_dataset):
response = test_client.post("/grid/table?level=4&order_by=-population", headers=HEADERS, content=json.dumps(body))
assert response.status_code == 200
assert json.loads(response.read()) == {
"cell": [
"865f00007ffffff",
"895f4261e03ffff",
],
"landcover": [4, 1],
"population": [200, 100],
"table": [
{"column": "cell", "values": ["865f00007ffffff", "895f4261e03ffff"]},
{"column": "landcover", "values": [4, 1]},
{"column": "population", "values": [200, 100]},
]
}


Expand All @@ -241,11 +240,11 @@ def test_grid_table_geojson(grid_dataset, geojson):
response = test_client.post("/grid/table?level=4&order_by=-population", headers=HEADERS, content=json.dumps(body))
assert response.status_code == 200
assert json.loads(response.read()) == {
"cell": [
"895f4261e03ffff",
],
"landcover": [1],
"population": [100],
"table": [
{"column": "cell", "values": ["895f4261e03ffff"]},
{"column": "landcover", "values": [1]},
{"column": "population", "values": [100]},
]
}


Expand All @@ -258,9 +257,7 @@ def test_grid_tile_post_geojson(grid_dataset, geojson):
)
assert response.status_code == 200
assert pl.read_ipc(response.read()).to_dict(as_series=False) == {
"cell": [
"895f4261e03ffff",
],
"cell": ["895f4261e03ffff"],
"landcover": [1],
"population": [100],
}
Expand Down

0 comments on commit 5b543e1

Please sign in to comment.