1
1
""" Method to update latest forecast values """
2
2
import logging
3
3
from datetime import datetime , timezone
4
- from typing import List
4
+ from typing import Optional , List
5
5
6
6
from sqlalchemy import inspect
7
7
from sqlalchemy .dialects .postgresql import insert
8
8
from sqlalchemy .orm .session import Session
9
9
10
10
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
12
12
13
13
logger = logging .getLogger (__name__ )
14
14
@@ -66,7 +66,7 @@ def upsert(session: Session, model, rows: List[dict]):
66
66
session .execute (stmt , rows )
67
67
68
68
69
- def update_forecast_latest (forecast : ForecastSQL , session : Session ):
69
+ def update_forecast_latest (forecast : ForecastSQL , session : Session , forecast_historic : Optional [ ForecastSQL ] = None ):
70
70
"""
71
71
Update the forecast_values table
72
72
@@ -80,7 +80,8 @@ def update_forecast_latest(forecast: ForecastSQL, session: Session):
80
80
"""
81
81
82
82
# 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 )
84
85
85
86
# 2. create forecast value latest
86
87
forecast_values_dict = []
@@ -108,5 +109,14 @@ def update_all_forecast_latest(forecasts: List[ForecastSQL], session: Session):
108
109
Update all latest forecasts
109
110
"""
110
111
112
+ # get all latest forecasts
113
+ forecasts_historic = get_latest_forecast_for_gsps (session = session , historic = True )
114
+
111
115
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