Replace the legacy Session.query() API #17
1 changed files with 10 additions and 9 deletions
Query with select() in the forecasting models
`Session.query()` is the legacy API in SQLAlchemy 2.x. `.one_or_none()` expresses that the unique constraint on the five filtered columns allows at most one cached `Forecast`.
commit
185a37f7d4
|
|
@ -5,6 +5,7 @@ import datetime as dt
|
|||
import typing as t
|
||||
|
||||
import pandas as pd
|
||||
import sqlalchemy as sa
|
||||
|
||||
from velofy import db
|
||||
from velofy.forecasts import timify
|
||||
|
|
@ -80,15 +81,15 @@ class ForecastingModelABC(abc.ABC):
|
|||
Raises:
|
||||
RuntimeError: `*Model.predict()` misses the prediction for `predict_at`
|
||||
"""
|
||||
if (
|
||||
cached_forecast := db.session.query(db.Forecast)
|
||||
.filter_by(pixel=pixel)
|
||||
.filter_by(start_at=predict_at)
|
||||
.filter_by(time_step=self._order_history.time_step)
|
||||
.filter_by(train_horizon=train_horizon)
|
||||
.filter_by(model=self.name)
|
||||
.first()
|
||||
):
|
||||
if cached_forecast := db.session.scalars(
|
||||
sa.select(db.Forecast).filter_by(
|
||||
pixel=pixel,
|
||||
start_at=predict_at,
|
||||
time_step=self._order_history.time_step,
|
||||
train_horizon=train_horizon,
|
||||
model=self.name,
|
||||
),
|
||||
).one_or_none():
|
||||
return cached_forecast
|
||||
|
||||
# Horizontal and real-time `*Model`s return a `pd.DataFrame` with one
|
||||
|
|
|
|||
Loading…
Reference in a new issue