-
Notifications
You must be signed in to change notification settings - Fork 0
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
Forecast endpoint #734
base: master
Are you sure you want to change the base?
Forecast endpoint #734
Changes from 5 commits
18b7115
56438ed
b5d6729
a315052
010c4f7
332a125
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,9 @@ | |
HexGrid, | ||
) | ||
from cleanair.decorators import db_query | ||
|
||
from cleanair.mixins.query_mixins.data_config_query_mixin import AirQualityDataConfigQueryMixin | ||
from cleanair.mixins import InstanceQueryMixin, ResultQueryMixin | ||
from cleanair.params.shared_params import PRODUCTION_DYNAMIC_FEATURES, PRODUCTION_STATIC_FEATURES | ||
from ..database import all_or_404 | ||
from ..schemas.air_quality_forecast import ForecastResultGeoJson, GeometryGeoJson | ||
|
||
|
@@ -22,28 +24,54 @@ | |
|
||
@db_query() | ||
def query_instance_ids( | ||
db: Session, start_datetime: datetime, end_datetime: datetime, | ||
db: Session, | ||
start_datetime: datetime, | ||
end_datetime: datetime, | ||
static_features: List[str], | ||
dynamic_features: List[str] | ||
) -> Query: | ||
""" | ||
Check which model IDs produced forecasts between start_datetime and end_datetime. | ||
""" | ||
query = ( | ||
db.query( | ||
AirQualityResultTable.instance_id, | ||
AirQualityResultTable.measurement_start_utc, | ||
) | ||
.join( | ||
AirQualityInstanceTable, | ||
AirQualityInstanceTable.instance_id == AirQualityResultTable.instance_id, | ||
) | ||
.filter( | ||
AirQualityInstanceTable.tag == "production", | ||
AirQualityInstanceTable.model_name == "svgp", | ||
AirQualityResultTable.measurement_start_utc >= start_datetime, | ||
AirQualityResultTable.measurement_start_utc < end_datetime, | ||
) | ||
|
||
data_ids = AirQualityDataConfigQueryMixin().query_data_config( | ||
static_features=static_features, | ||
dynamic_features=dynamic_features | ||
).subquery() | ||
|
||
instance_query = InstanceQueryMixin().get_instances( | ||
tag='production', | ||
models=['mrdgp'], | ||
data_ids=data_ids, | ||
).subquery() | ||
|
||
results_query = ResultQueryMixin().query_results( | ||
start=start_datetime, | ||
upto=end_datetime, | ||
).subquery() | ||
|
||
query = results_query.innerjoin( | ||
instance_query, | ||
instance_query.c.instance_id == results_query.c.instance_id | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Which would mean the type of |
||
|
||
# query = ( | ||
# db.query( | ||
# AirQualityResultTable.instance_id, | ||
# AirQualityResultTable.measurement_start_utc, | ||
# ) | ||
# .join( | ||
# AirQualityInstanceTable, | ||
# AirQualityInstanceTable.instance_id == AirQualityResultTable.instance_id, | ||
# ) | ||
# .filter( | ||
# AirQualityInstanceTable.tag == "production", | ||
# AirQualityInstanceTable.model_name == "mrdgp", | ||
# AirQualityResultTable.measurement_start_utc >= start_datetime, | ||
# AirQualityResultTable.measurement_start_utc < end_datetime, | ||
# ) | ||
# ) | ||
|
||
# Return only instance IDs and distinct values | ||
query = query.with_entities( | ||
AirQualityInstanceTable.instance_id, AirQualityInstanceTable.fit_start_time | ||
|
@@ -58,20 +86,27 @@ def query_instance_ids( | |
key=lambda _, *args, **kwargs: hashkey(*args, **kwargs), | ||
) | ||
def cached_instance_ids( | ||
db: Session, start_datetime: datetime, end_datetime: datetime, | ||
db: Session, start_datetime: datetime, end_datetime: datetime, | ||
) -> Optional[List[Tuple]]: | ||
"""Cache available model instances""" | ||
logger.info( | ||
"Querying available instance IDs between %s and %s", | ||
start_datetime, | ||
end_datetime, | ||
) | ||
return query_instance_ids(db, start_datetime, end_datetime).all() | ||
|
||
return query_instance_ids( | ||
db, | ||
start_datetime, | ||
end_datetime, | ||
PRODUCTION_STATIC_FEATURES, | ||
PRODUCTION_DYNAMIC_FEATURES | ||
).all() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You haven't specified |
||
|
||
|
||
@db_query() | ||
def query_geometries_hexgrid( | ||
db: Session, bounding_box: Optional[Tuple[float]] = None, | ||
db: Session, bounding_box: Optional[Tuple[float]] = None, | ||
) -> Query: | ||
""" | ||
Query geometries for combining with plain JSON forecasts | ||
|
@@ -93,7 +128,7 @@ def query_geometries_hexgrid( | |
cache=LRUCache(maxsize=256), key=lambda _, *args, **kwargs: hashkey(*args, **kwargs) | ||
) | ||
def cached_geometries_hexgrid( | ||
db: Session, bounding_box: Optional[Tuple[float]] = None, | ||
db: Session, bounding_box: Optional[Tuple[float]] = None, | ||
) -> GeometryGeoJson: | ||
"""Cache geometries with optional bounding box""" | ||
logger.info("Querying hexgrid geometries") | ||
|
@@ -107,12 +142,12 @@ def cached_geometries_hexgrid( | |
|
||
@db_query() | ||
def query_forecasts_hexgrid( | ||
db: Session, | ||
instance_id: str, | ||
start_datetime: datetime, | ||
end_datetime: datetime, | ||
with_geometry: bool, | ||
bounding_box: Optional[Tuple[float]] = None, | ||
db: Session, | ||
instance_id: str, | ||
start_datetime: datetime, | ||
end_datetime: datetime, | ||
with_geometry: bool, | ||
bounding_box: Optional[Tuple[float]] = None, | ||
) -> Query: | ||
""" | ||
Get all forecasts for a given model instance in the given datetime range | ||
|
@@ -155,12 +190,12 @@ def query_forecasts_hexgrid( | |
cache=LRUCache(maxsize=256), key=lambda _, *args, **kwargs: hashkey(*args, **kwargs) | ||
) | ||
def cached_forecast_hexgrid_json( | ||
db: Session, | ||
instance_id: str, | ||
start_datetime: datetime, | ||
end_datetime: datetime, | ||
with_geometry: bool, | ||
bounding_box: Optional[Tuple[float]] = None, | ||
db: Session, | ||
instance_id: str, | ||
start_datetime: datetime, | ||
end_datetime: datetime, | ||
with_geometry: bool, | ||
bounding_box: Optional[Tuple[float]] = None, | ||
) -> Optional[List[Tuple]]: | ||
"""Cache forecasts with geometry with optional bounding box""" | ||
logger.info( | ||
|
@@ -186,11 +221,11 @@ def cached_forecast_hexgrid_json( | |
cache=LRUCache(maxsize=256), key=lambda _, *args, **kwargs: hashkey(*args, **kwargs) | ||
) | ||
def cached_forecast_hexgrid_geojson( | ||
db: Session, | ||
instance_id: str, | ||
start_datetime: datetime, | ||
end_datetime: datetime, | ||
bounding_box: Optional[Tuple[float]] = None, | ||
db: Session, | ||
instance_id: str, | ||
start_datetime: datetime, | ||
end_datetime: datetime, | ||
bounding_box: Optional[Tuple[float]] = None, | ||
) -> ForecastResultGeoJson: | ||
"""Cache forecasts with geometry with optional bounding box""" | ||
query_results = cached_forecast_hexgrid_json( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or perhaps because this is a subquery? Not a query
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that might be it