-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
136 additions
and
21 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from fastapi import FastAPI | ||
|
||
from aross_stations_db.api.v1.routes import router as v1_router | ||
|
||
api = FastAPI( | ||
title=( | ||
"Rain on snow events detected by" | ||
" Automated Surface Observing System (ASOS) stations" | ||
), | ||
) | ||
api.include_router(v1_router) |
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,59 @@ | ||
import datetime as dt | ||
from typing import Annotated | ||
|
||
from fastapi import APIRouter, Depends, Query | ||
from geoalchemy2 import WKBElement | ||
from pydantic import BaseModel | ||
from sqlalchemy import func | ||
from sqlalchemy.orm import Session | ||
|
||
from aross_stations_db.middleware import get_db_session | ||
from aross_stations_db.tables import Event, Station | ||
|
||
router = APIRouter(prefix="/v1", tags=["v1"]) | ||
|
||
|
||
class ReturnElement(BaseModel): | ||
name: str | ||
location: str | ||
event_count: int | ||
|
||
|
||
@router.get("/") | ||
def get( | ||
start: Annotated[dt.datetime, Query(description="ISO-format timestamp")], | ||
end: Annotated[dt.datetime, Query(description="ISO-format timestamp")], | ||
db: Annotated[Session, Depends(get_db_session)], | ||
polygon: Annotated[str | None, WKBElement, Query(description="WKT shape")] = None, | ||
) -> list[ReturnElement]: | ||
"""Get stations and their events matching query parameters.""" | ||
query = ( | ||
db.query( | ||
Station, | ||
Station.location.ST_AsEWKT(), | ||
func.count(), | ||
) | ||
.join( | ||
Event, | ||
) | ||
.filter(Event.time_start >= start, Event.time_end < end) | ||
) | ||
|
||
if polygon: | ||
query = query.filter( | ||
Station.location.ST_Within( | ||
func.ST_SetSRID(func.ST_GeomFromText(polygon), 4326), | ||
) | ||
) | ||
|
||
query = query.group_by(Station.id) | ||
|
||
results = query.all() | ||
return [ | ||
ReturnElement( | ||
name=station.name, | ||
location=location, | ||
event_count=event_count, | ||
) | ||
for station, location, event_count in results | ||
] |
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
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,26 @@ | ||
from collections.abc import Iterator | ||
from functools import cache | ||
from typing import Annotated | ||
|
||
from fastapi import Depends | ||
from sqlalchemy.orm import Session, sessionmaker | ||
|
||
from aross_stations_db.config import Settings | ||
|
||
|
||
@cache | ||
def get_config() -> Settings: | ||
# TODO: False-positive. Remove type-ignore. | ||
# See: https://github.com/pydantic/pydantic/issues/6713 | ||
return Settings() # type:ignore[call-arg] | ||
|
||
|
||
def get_db_session( | ||
config: Annotated[Settings, Depends(get_config)], | ||
) -> Iterator[Session]: | ||
SessionFactory = sessionmaker(config.db_engine) | ||
session = SessionFactory() | ||
try: | ||
yield session | ||
finally: | ||
session.close() |
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