Skip to content
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

Fix backend errors #183

Merged
merged 6 commits into from
Jun 5, 2024
Merged
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
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