Skip to content

Commit 5c51169

Browse files
committed
speed up getting historic forecasts
1 parent 68c619b commit 5c51169

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

nowcasting_datamodel/update.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
""" Method to update latest forecast values """
22
import logging
33
from datetime import datetime, timezone
4-
from typing import List
4+
from typing import Optional, List
55

66
from sqlalchemy import inspect
77
from sqlalchemy.dialects.postgresql import insert
88
from sqlalchemy.orm.session import Session
99

1010
from nowcasting_datamodel.models.models import ForecastSQL, ForecastValueLatestSQL
11-
from nowcasting_datamodel.read.read import get_latest_forecast, get_model
11+
from nowcasting_datamodel.read.read import get_latest_forecast, get_model, get_latest_forecast_for_gsps
1212

1313
logger = logging.getLogger(__name__)
1414

@@ -66,7 +66,7 @@ def upsert(session: Session, model, rows: List[dict]):
6666
session.execute(stmt, rows)
6767

6868

69-
def update_forecast_latest(forecast: ForecastSQL, session: Session):
69+
def update_forecast_latest(forecast: ForecastSQL, session: Session, forecast_historic: Optional[ForecastSQL] = None):
7070
"""
7171
Update the forecast_values table
7272
@@ -80,7 +80,8 @@ def update_forecast_latest(forecast: ForecastSQL, session: Session):
8080
"""
8181

8282
# 1. get forecast object
83-
forecast_historic = get_historic_forecast(session=session, forecast=forecast)
83+
if forecast_historic is None:
84+
forecast_historic = get_historic_forecast(session=session, forecast=forecast)
8485

8586
# 2. create forecast value latest
8687
forecast_values_dict = []
@@ -108,5 +109,14 @@ def update_all_forecast_latest(forecasts: List[ForecastSQL], session: Session):
108109
Update all latest forecasts
109110
"""
110111

112+
# get all latest forecasts
113+
forecasts_historic = get_latest_forecast_for_gsps(session=session, historic=True)
114+
111115
for forecast in forecasts:
112-
update_forecast_latest(forecast=forecast, session=session)
116+
117+
# chose the correct forecast historic
118+
gsp_id = forecast.location.gsp_id
119+
forecast_historic = [forecast for forecast in forecasts_historic if forecast.location.gsp_id == gsp_id]
120+
forecast_historic = forecast_historic[0]
121+
122+
update_forecast_latest(forecast=forecast, session=session, forecast_historic=forecast_historic)

0 commit comments

Comments
 (0)