-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(fire alerts): experiment with ports and adapters approach
https://alistair.cockburn.us/hexagonal-architecture/ https://8thlight.com/insights/a-color-coded-guide-to-ports-and-adapters
- Loading branch information
Showing
12 changed files
with
157 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from typing import Any, Callable, Coroutine, Dict | ||
from uuid import UUID | ||
|
||
from fastapi import HTTPException | ||
|
||
from app.domain.ports.geostore_geometry_port import GeostoreGeometryPort | ||
from app.errors import BadResponseError, InvalidResponseError | ||
from app.models.enumerators.geostore import GeostoreOrigin | ||
from app.models.types import Geometry | ||
from app.utils import rw_api | ||
|
||
|
||
class DataApiGeostoreGeometryAdapter(GeostoreGeometryPort): | ||
async def get_geometry(self, geostore_id, geostore_origin): | ||
geostore_constructor: Dict[ | ||
str, Callable[[UUID], Coroutine[Any, Any, Geometry]] | ||
] = { | ||
# GeostoreOrigin.gfw: geostore.get_geostore_geometry, | ||
GeostoreOrigin.rw: rw_api.get_geostore_geometry | ||
} | ||
|
||
try: | ||
return await geostore_constructor[geostore_origin](geostore_id) | ||
except KeyError: | ||
raise HTTPException( | ||
status_code=501, | ||
detail=f"Geostore origin {geostore_origin} not fully implemented.", | ||
) | ||
|
||
except InvalidResponseError as e: | ||
raise HTTPException(status_code=500, detail=str(e)) | ||
except BadResponseError as e: | ||
raise HTTPException(status_code=400, detail=str(e)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from typing import Any, Dict | ||
|
||
|
||
class GeostoreGeometryPort: | ||
async def get_geometry(self, geostore_id, geostore_origin) -> Dict[str, Any]: | ||
pass |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from app.domain.ports.geostore_geometry_port import GeostoreGeometryPort | ||
|
||
|
||
class GeostoreGeometryService: | ||
def __init__(self, geostore_geometry_port: GeostoreGeometryPort): | ||
self.geostore_geometry_port = geostore_geometry_port | ||
|
||
async def get_geometry(self, geostore_id, geostore_origin): | ||
return await self.geostore_geometry_port.get_geometry( | ||
geostore_id, geostore_origin | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from fastapi import HTTPException | ||
from psycopg2._psycopg import QueryCanceledError | ||
|
||
from app.crud.async_db.vector_tiles import nasa_viirs_fire_alerts | ||
from app.crud.async_db.vector_tiles.filters import ( | ||
contextual_filter, | ||
date_filter, | ||
geometry_filter, | ||
) | ||
from app.domain.services.geostore_geometry_service import GeostoreGeometryService | ||
|
||
|
||
class NasaViirsFireAlertsService: | ||
def __init__(self, geostore_geometry_service: GeostoreGeometryService): | ||
self.geostore_geometry_service = geostore_geometry_service | ||
|
||
async def get_aggregated_tile( | ||
self, | ||
version, | ||
geostore_id, | ||
geostore_origin, | ||
bbox, | ||
extent, | ||
high_confidence_only, | ||
start_date, | ||
end_date, | ||
contextual_filters, | ||
include_attribute, | ||
): | ||
geometry = None | ||
if geostore_id: | ||
geometry = self.geostore_geometry_service.get_geometry( | ||
geostore_id, geostore_origin | ||
) | ||
|
||
filters = [ | ||
await geometry_filter(bbox, geometry), | ||
nasa_viirs_fire_alerts.confidence_filter(high_confidence_only), | ||
date_filter("alert__date", start_date, end_date), | ||
] + contextual_filter(**contextual_filters) | ||
|
||
# Remove empty filters | ||
filters = [f for f in filters if f is not None] | ||
|
||
try: | ||
tile = await nasa_viirs_fire_alerts.get_aggregated_tile( | ||
version, bbox, extent, include_attribute, filters | ||
) | ||
except QueryCanceledError: | ||
raise HTTPException( | ||
status_code=524, | ||
detail="A timeout occurred while processing the request. Request canceled.", | ||
) | ||
else: | ||
return tile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters