Skip to content

Commit

Permalink
Merge pull request #183 from nismod/fix/backend
Browse files Browse the repository at this point in the history
Fix backend errors
  • Loading branch information
tomalrussell authored Jun 5, 2024
2 parents 5a74dd8 + 8e668f4 commit deb1831
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 19 deletions.
5 changes: 4 additions & 1 deletion containers/backend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ LABEL maintainer="[email protected]"

WORKDIR /code

# Install project with dependencies - these can be cached as docker layers if unchanged
COPY pyproject.toml .

RUN python -m pip install .

# Copy and install latest package code
COPY ./backend /code/backend
RUN python -m pip install --upgrade --compile .

CMD ["uvicorn", "backend.app.main:app", "--host", "0.0.0.0", "--port", "8888"]

Expand Down
38 changes: 27 additions & 11 deletions containers/backend/backend/app/routers/tiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from sqlalchemy.exc import NoResultFound
from sqlalchemy.orm import Session
from geoalchemy2 import functions
from terracotta.exceptions import DatasetNotFoundError


from app import schemas
Expand Down Expand Up @@ -93,19 +94,29 @@ def _domain_exists(db: Session, domain: str) -> bool:
return res == 1


def _tile_db_from_domain(db: Session, domain: str) -> str:
def _tile_db_from_domain(domain: str) -> str:
"""
Query the name of the mysql database within-which the tiles reside
using the first value of the path keys (which links to hazard-type in the UI)
See: frontend/src/config/hazards/domains.ts
"""
source_db = db.execute(
# Should only return one entry
select(models.RasterTileSource.source_db).where(
models.RasterTileSource.domain == domain
)
).scalar_one()
return source_db
# TODO try conventional or configured mappin again - hot fix for db pool exhaustion
domain_to_db = {
"buildings": "terracotta_buildings",
"coastal": "terracotta_aqueduct",
"cyclone_iris": "terracotta_iris",
"cyclone": "terracotta_cyclone",
"dem": "terracotta_dem",
"drought": "terracotta_drought",
"earthquake": "terracotta_gem_earthquake",
"extreme_heat": "terracotta_extreme_heat",
"fluvial": "terracotta_aqueduct",
"land_cover": "terracotta_land_cover",
"nature": "terracotta_exposure_nature",
"population": "terracotta_jrc_pop",
"traveltime_to_healthcare": "terracotta_traveltime_to_healthcare",
}
return domain_to_db[domain]


def _source_options(source_db: str, domain: str = None) -> List[dict]:
Expand Down Expand Up @@ -307,7 +318,6 @@ async def get_tile(
colormap: Union[str, None] = None,
stretch_range: Union[str, None] = None,
explicit_color_map: Union[str, None] = None,
db: Session = Depends(get_db),
):
"""
Serves XYZ Raster Tiles with the given colormap / stretch range or explicit color map for categorical data.
Expand Down Expand Up @@ -346,9 +356,9 @@ async def get_tile(
parsed_keys = _parse_keys(keys)
domain = _domain_from_keys(parsed_keys)
try:
source_db = _tile_db_from_domain(db, domain)
source_db = _tile_db_from_domain(domain)
logger.debug("source DB for tile path: %s", source_db)
except NoResultFound:
except KeyError:
raise HTTPException(
status_code=404,
detail=f"No source database for the given domain {domain} could be found",
Expand Down Expand Up @@ -393,6 +403,12 @@ async def get_tile(
status_code=400,
detail=f"source database {source_db} does not exist in tiles metastore",
)
except DatasetNotFoundError as err:
handle_exception(logger, err)
raise HTTPException(
status_code=400,
detail=f"layer with keys {parsed_keys} not found in {source_db} in tiles metastore",
)
except Exception as err:
handle_exception(logger, err)
raise HTTPException(status_code=500)
8 changes: 4 additions & 4 deletions containers/backend/backend/app/schemas.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from enum import Enum
from typing import Generic, List, Optional, TypeVar
from typing import Generic, List, Optional, TypeVar, Union
from pydantic import BaseModel, ConfigDict, conint, validator


class FeatureBase(BaseModel):
id: int
string_id: str
layer: str
sublayer: str | None
sublayer: Optional[str] = None
properties: dict


Expand All @@ -33,7 +33,7 @@ class DataParameters(BaseModel):
class ExpectedDamagesDimensions(DataDimensions):
hazard: str
rcp: str
epoch: str
epoch: Union[str, int]
protection_standard: int


Expand All @@ -54,7 +54,7 @@ class ExpectedDamage(ExpectedDamagesDimensions, ExpectedDamagesVariables):
class ReturnPeriodDamagesDimensions(DataDimensions):
hazard: str
rcp: str
epoch: str
epoch: Union[str, int]
rp: int


Expand Down
2 changes: 1 addition & 1 deletion docker-compose-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ services:

backend:
build: ./containers/backend
image: ghcr.io/nismod/gri-backend:1.3
image: ghcr.io/nismod/gri-backend:1.4.2
volumes:
- ./etl/raster/cog/:/data/
- ./containers/backend/backend/:/code/backend/
Expand Down
2 changes: 1 addition & 1 deletion docker-compose-prod-build.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
services:
backend:
image: ghcr.io/nismod/gri-backend:1.3
image: ghcr.io/nismod/gri-backend:1.4.2
build: ./containers/backend

vector-tileserver:
Expand Down
2 changes: 1 addition & 1 deletion docker-compose-prod-deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ services:
mem_limit: "250M"

backend:
image: ghcr.io/nismod/gri-backend:1.3
image: ghcr.io/nismod/gri-backend:1.4.2
restart: always
volumes:
- ./tileserver/raster/data:/data
Expand Down

0 comments on commit deb1831

Please sign in to comment.