Skip to content

Commit

Permalink
Merge pull request #204 from nismod/fix/python-deps
Browse files Browse the repository at this point in the history
Fix/python deps
  • Loading branch information
tomalrussell authored Nov 19, 2024
2 parents 00e56bc + e84548d commit 01b8234
Show file tree
Hide file tree
Showing 11 changed files with 1,424 additions and 1,077 deletions.
2 changes: 1 addition & 1 deletion backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM python:3.10
FROM python:3.12

WORKDIR /code

Expand Down
16 changes: 8 additions & 8 deletions backend/Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@ verify_ssl = true
name = "pypi"

[packages]
fastapi = "*"
geoalchemy2 = "*"
uvicorn = "*"
fastapi-pagination = "*"
backend = { editable = true, path = "." }
fastapi = "~=0.115"
fastapi-pagination = "~=0.12"
geoalchemy2 = "~=0.16"
setuptools = "*"
backend = {editable = true, path = "."}
psycopg2-binary = "*"
uvicorn = "~=0.32"
psycopg2-binary = "~=2.9"
pydantic = "~=2.9"

[dev-packages]
black = "*"
geopandas = "*"
pandas = "*"
pyarrow = "*"
pygeos = "*"
shapely = "*"
snakefmt = "*"
snakemake = "*"
tqdm = "*"
ujson = "*"

[requires]
python_version = "3.10"
python_version = "3.12"
2,362 changes: 1,355 additions & 1,007 deletions backend/Pipfile.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions backend/backend/app/internal/attribute_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def add_adaptation_value_query(
adaptation_protection_level=dimensions.adaptation_protection_level,
)

value: Column | ColumnOperators = None
value: Column | ColumnOperators | None = None

if field == "cost_benefit_ratio":
cost_benefit_params: schemas.AdaptationCostBenefitRatioParameters = field_params
Expand All @@ -78,7 +78,7 @@ class DataGroupConfig:
add_value_query: Callable[
[Query, schemas.DataDimensions, str, schemas.DataParameters | None], Query
]
field_parameters_schemas: dict[str, schemas.DataParameters] = None
field_parameters_schemas: dict[str, schemas.DataParameters] | None = None


DATA_GROUP_CONFIGS: dict[str, DataGroupConfig] = {
Expand Down Expand Up @@ -136,7 +136,7 @@ def add_value_query(
field_group: str,
field_dimensions: schemas.DataDimensions,
field: str,
field_params: schemas.DataParameters = None,
field_params: schemas.DataParameters | None = None,
):
data_group_config = DATA_GROUP_CONFIGS.get(field_group)

Expand Down
15 changes: 12 additions & 3 deletions backend/backend/app/routers/features.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from fastapi import APIRouter, Depends
from typing import Optional

import sqlalchemy.exc
from fastapi import APIRouter, Depends, HTTPException
from fastapi_pagination import Page, Params
from fastapi_pagination.ext.sqlalchemy import paginate
from sqlalchemy import desc
Expand All @@ -20,12 +23,18 @@

@router.get("/{feature_id}", response_model=schemas.FeatureOut)
def read_feature(feature_id: int, db: Session = Depends(get_db)):
feature = db.query(models.Feature).filter(models.Feature.id == feature_id).one()
try:
feature = db.query(models.Feature).filter(models.Feature.id == feature_id).one()
except sqlalchemy.exc.NoResultFound:
raise HTTPException(status_code=404, detail="Feature not found")
return feature


def get_layer_spec(
layer: str = None, sector: str = None, subsector: str = None, asset_type: str = None
layer: Optional[str] = None,
sector: Optional[str] = None,
subsector: Optional[str] = None,
asset_type: Optional[str] = None,
):
return schemas.LayerSpec(
layer_name=layer,
Expand Down
38 changes: 16 additions & 22 deletions backend/backend/app/schemas.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from enum import Enum
from typing import Any, Generic, Literal, TypeVar
from pydantic import BaseModel, conint, validator
from pydantic.generics import GenericModel
from typing import Generic, Optional, TypeVar
from typing_extensions import Annotated
from pydantic import BaseModel, ConfigDict, Field, field_validator


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


Expand All @@ -34,7 +34,7 @@ class DataParameters(BaseModel):
class ExpectedDamagesDimensions(DataDimensions):
hazard: str
rcp: str
epoch: str
epoch: str | int
protection_standard: int


Expand All @@ -48,15 +48,14 @@ class ExpectedDamagesVariables(DataVariables):


class ExpectedDamage(ExpectedDamagesDimensions, ExpectedDamagesVariables):
class Config:
orm_mode = True
model_config = ConfigDict(from_attributes=True)


# Return Period Damages
class ReturnPeriodDamagesDimensions(DataDimensions):
hazard: str
rcp: str
epoch: str
epoch: str | int
rp: int


Expand All @@ -71,8 +70,7 @@ class ReturnPeriodDamagesVariables(DataVariables):


class ReturnPeriodDamage(ReturnPeriodDamagesDimensions, ReturnPeriodDamagesVariables):
class Config:
orm_mode = True
model_config = ConfigDict(from_attributes=True)


# NPV Damages
Expand All @@ -91,8 +89,7 @@ class NPVDamagesVariables(DataVariables):


class NPVDamage(NPVDamagesDimensions, NPVDamagesVariables):
class Config:
orm_mode = True
model_config = ConfigDict(from_attributes=True)


# Adaptation Options
Expand All @@ -117,10 +114,10 @@ class AdaptationVariables(DataVariables):


class AdaptationCostBenefitRatioParameters(DataParameters):
eael_days: conint(ge=1, le=30)
eael_days: Annotated[int, Field(ge=1, le=30)]

@validator('eael_days')
def fix_eael_days(cls, eael_days) -> float:
@field_validator("eael_days")
def fix_eael_days(cls, eael_days: int) -> float:
"""
The data for `AdaptationCostBenefit.avoided_eael_mean` is erroneous and
should be modified in the meantime. This validator adds a fudge factor
Expand All @@ -132,14 +129,12 @@ def fix_eael_days(cls, eael_days) -> float:


class Adaptation(AdaptationDimensions, AdaptationVariables):
class Config:
orm_mode = True
model_config = ConfigDict(from_attributes=True)


# Features
class FeatureOutBase(FeatureBase):
class Config:
orm_mode = True
model_config = ConfigDict(from_attributes=True)


class FeatureOut(FeatureOutBase):
Expand All @@ -162,15 +157,14 @@ class LayerSpec(BaseModel):
SortFieldT = TypeVar("SortFieldT")


class FeatureListItemOut(GenericModel, Generic[SortFieldT]):
class FeatureListItemOut(BaseModel, Generic[SortFieldT]):
id: int
string_id: str
layer: str
bbox_wkt: str
value: SortFieldT

class Config:
orm_mode = True
model_config = ConfigDict(from_attributes=True)


# Feature Attributes Lookups
Expand Down
41 changes: 20 additions & 21 deletions backend/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,26 @@
# These requirements were autogenerated by pipenv
# To regenerate from the project's Pipfile, run:
#
# pipenv lock --requirements
# sed -i '7,$ d' requirements.txt; pipenv requirements >> requirements.txt
#

-i https://pypi.org/simple
annotated-types==0.7.0; python_version >= '3.8'
anyio==4.6.2.post1; python_version >= '3.9'
-e .
anyio==3.5.0; python_full_version >= '3.6.2'
asgiref==3.5.0; python_version >= '3.7'
click==8.1.2; python_version >= '3.7'
fastapi-pagination==0.9.3
fastapi==0.75.2
geoalchemy2==0.11.1
greenlet==1.1.2; python_version >= '3' and platform_machine == 'aarch64' or (platform_machine == 'ppc64le' or (platform_machine == 'x86_64' or (platform_machine == 'amd64' or (platform_machine == 'AMD64' or (platform_machine == 'win32' or platform_machine == 'WIN32')))))
h11==0.13.0; python_version >= '3.6'
idna==3.3; python_version >= '3.5'
packaging==21.3; python_version >= '3.6'
psycopg2==2.9.3
pydantic==1.9.0; python_full_version >= '3.6.1'
pyparsing==3.0.8; python_full_version >= '3.6.8'
setuptools==62.1.0
sniffio==1.2.0; python_version >= '3.5'
sqlalchemy==1.4.36; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4, 3.5'
starlette==0.17.1; python_version >= '3.6'
typing-extensions==4.2.0; python_version >= '3.7'
uvicorn==0.17.6
click==8.1.7; python_version >= '3.7'
fastapi==0.115.5; python_version >= '3.8'
fastapi-pagination==0.12.32; python_version >= '3.8' and python_version < '4.0'
geoalchemy2==0.16.0; python_version >= '3.7'
greenlet==3.1.1; python_version >= '3.7'
h11==0.14.0; python_version >= '3.7'
idna==3.10; python_version >= '3.6'
packaging==24.2; python_version >= '3.8'
psycopg2-binary==2.9.10; python_version >= '3.8'
pydantic==2.9.2; python_version >= '3.8'
pydantic-core==2.23.4; python_version >= '3.8'
setuptools==75.5.0; python_version >= '3.9'
sniffio==1.3.1; python_version >= '3.7'
sqlalchemy==2.0.36; python_version >= '3.7'
starlette==0.41.3; python_version >= '3.8'
typing-extensions==4.12.2; python_version >= '3.8'
uvicorn==0.32.0; python_version >= '3.8'
10 changes: 5 additions & 5 deletions deploy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,18 +207,18 @@ Build and publish all images:

```bash
# Build images locally
docker compose -f docker-compose-prod.yaml build
docker compose -f docker-compose.prod.yml build

# Push to GitHub container registry
docker push ghcr.io/nismod/jsrat-frontend:0.1
docker push ghcr.io/nismod/jsrat-backend:0.1
docker push ghcr.io/nismod/jsrat-backend:0.2
docker push ghcr.io/nismod/jsrat-vector-tileserver:0.1
docker push ghcr.io/nismod/jsrat-raster-tileserver:0.1
```

```bash
docker push ghcr.io/nismod/jsrat-frontend:0.1
docker push ghcr.io/nismod/jsrat-backend:0.1
docker push ghcr.io/nismod/jsrat-backend:0.2
docker push ghcr.io/nismod/jsrat-vector-tileserver:0.1
docker push ghcr.io/nismod/jsrat-raster-tileserver:0.1
```
Expand All @@ -228,12 +228,12 @@ docker push ghcr.io/nismod/jsrat-raster-tileserver:0.1
Update a specific image, then build and push:

```bash
# Edit the image version in `docker-compose.prod.yaml`
# Edit the image version in `docker-compose.prod.yml`
# in this example it's on line 33:
# image: ghcr.io/nismod/jsrat-frontend:0.1

# Build
docker compose -f docker-compose.prod.yaml build frontend
docker compose -f docker-compose.prod.yml build frontend

# Push
docker push ghcr.io/nismod/jsrat-frontend:0.1
Expand Down
5 changes: 2 additions & 3 deletions docker-compose.dev.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# Development environment - includes database, hard-coded env vars
version: '3.9'
volumes:
postgis-data:

Expand Down Expand Up @@ -34,7 +33,7 @@ services:
- "traefik.http.services.frontend.loadbalancer.server.port=80"

backend:
image: ghcr.io/nismod/jsrat-backend:0.1
image: ghcr.io/nismod/jsrat-backend:0.2
build: ./backend
ports:
- 8888:80
Expand Down Expand Up @@ -117,5 +116,5 @@ services:
"ingest",
"/data/{type}__rp_{rp}__rcp_{rcp}__epoch_{epoch}__conf_{confidence}.tif",
"-o",
"/data/terracotta.sqlite"
"/data/terracotta.sqlite",
]
4 changes: 1 addition & 3 deletions docker-compose.prod.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# Production environment
version: "3.9"

services:
frontend:
image: ghcr.io/nismod/jsrat-frontend:0.1
Expand All @@ -9,7 +7,7 @@ services:
- "3000:80"

backend:
image: ghcr.io/nismod/jsrat-backend:0.1
image: ghcr.io/nismod/jsrat-backend:0.2
build: ./backend
ports:
- "3001:80"
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.stage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ services:
- "3000:80"

backend:
image: ghcr.io/nismod/jsrat-backend:0.1
image: ghcr.io/nismod/jsrat-backend:0.2
platform: linux/amd64
build: ./backend
ports:
Expand Down

0 comments on commit 01b8234

Please sign in to comment.