@@ -148,9 +148,9 @@ def get_latest_forecast(
148
148
query = query .filter (ForecastSQL .historic == false ())
149
149
150
150
if start_target_time is not None :
151
- query = filter_query_on_target_time (query = query ,
152
- start_target_time = start_target_time ,
153
- historic = historic )
151
+ query = filter_query_on_target_time (
152
+ query = query , start_target_time = start_target_time , historic = historic
153
+ )
154
154
155
155
# filter on gsp_id
156
156
if gsp_id is not None :
@@ -159,13 +159,14 @@ def get_latest_forecast(
159
159
order_by_items .append (LocationSQL .gsp_id )
160
160
161
161
order_by_items .append (ForecastSQL .created_utc .desc ())
162
+ if not historic :
163
+ created_utc = get_latest_forecast_created_utc (session = session , gsp_id = gsp_id )
164
+ query = query .filter (ForecastSQL .created_utc == created_utc )
162
165
163
166
# this make the newest ones comes to the top
164
167
query = query .order_by (* order_by_items )
165
168
166
169
# get all results
167
- if not historic :
168
- query = query .limit (1 )
169
170
forecasts = query .all ()
170
171
171
172
if forecasts is None :
@@ -176,8 +177,10 @@ def get_latest_forecast(
176
177
forecast = forecasts [0 ]
177
178
178
179
# sort list
179
- logger .debug (f"sorting 'forecast_values_latest' values. "
180
- f"There are { len (forecast .forecast_values_latest )} " )
180
+ logger .debug (
181
+ f"sorting 'forecast_values_latest' values. "
182
+ f"There are { len (forecast .forecast_values_latest )} "
183
+ )
181
184
if forecast .forecast_values_latest is not None :
182
185
forecast .forecast_values_latest = sorted (
183
186
forecast .forecast_values_latest , key = lambda d : d .target_time
@@ -216,26 +219,33 @@ def get_all_gsp_ids_latest_forecast(
216
219
if start_created_utc is not None :
217
220
query = query .filter (ForecastSQL .created_utc >= start_created_utc )
218
221
219
- if start_target_time is not None :
220
- query = filter_query_on_target_time (query = query ,
221
- start_target_time = start_target_time ,
222
- historic = historic )
223
-
224
222
# join with tables
225
223
if not historic :
226
224
query = query .distinct (LocationSQL .gsp_id )
227
225
query = query .join (LocationSQL )
228
226
229
227
query = query .filter (ForecastSQL .historic == historic )
230
228
231
- query = query .order_by (LocationSQL .gsp_id , desc (ForecastSQL .created_utc ))
229
+ if start_target_time is not None :
230
+ query = filter_query_on_target_time (
231
+ query = query , start_target_time = start_target_time , historic = historic
232
+ )
232
233
233
234
if preload_children :
234
235
query = query .options (joinedload (ForecastSQL .location ))
235
236
query = query .options (joinedload (ForecastSQL .model ))
236
237
query = query .options (joinedload (ForecastSQL .input_data_last_updated ))
238
+ if not historic :
239
+ query = query .options (joinedload (ForecastSQL .forecast_values ))
240
+ query = query .options (joinedload (ForecastSQL .forecast_values_latest ))
237
241
238
- forecasts = query .all ()
242
+ query = query .order_by (LocationSQL .gsp_id , desc (ForecastSQL .created_utc ))
243
+
244
+ forecasts = query .populate_existing ().all ()
245
+
246
+ logger .debug (f"Found { len (forecasts )} forecasts" )
247
+ if len (forecasts ) > 0 :
248
+ logger .debug (f"The first forecast has { len (forecasts [0 ].forecast_values )} forecast_values" )
239
249
240
250
return forecasts
241
251
@@ -257,13 +267,14 @@ def filter_query_on_target_time(query, start_target_time, historic: bool):
257
267
join_object = ForecastSQL .forecast_values
258
268
259
269
if start_target_time is not None :
260
- query = (
261
- query .join (join_object )
262
- .filter (forecast_value_model .target_time >= start_target_time )
263
- .options (contains_eager (join_object ))
264
- .populate_existing ()
270
+ logger .debug (f"Filtering '{ start_target_time = } '" )
271
+ query = query .join (join_object ).filter (
272
+ forecast_value_model .target_time >= start_target_time
265
273
)
266
274
275
+ if historic :
276
+ query = query .options (contains_eager (join_object )).populate_existing ()
277
+
267
278
return query
268
279
269
280
@@ -347,7 +358,7 @@ def get_latest_national_forecast(
347
358
return forecast
348
359
349
360
350
- def get_latest_forecast_created_utc (session : Session , gsp_id : int ) -> datetime :
361
+ def get_latest_forecast_created_utc (session : Session , gsp_id : Optional [ int ] = None ) -> datetime :
351
362
"""
352
363
Get the latest forecast created utc value. Can choose for different gsps
353
364
@@ -360,8 +371,9 @@ def get_latest_forecast_created_utc(session: Session, gsp_id: int) -> datetime:
360
371
query = session .query (ForecastSQL .created_utc )
361
372
362
373
# filter on gsp_id
363
- query = query .join (LocationSQL )
364
- query = query .filter (LocationSQL .gsp_id == gsp_id )
374
+ if gsp_id is not None :
375
+ query = query .join (LocationSQL )
376
+ query = query .filter (LocationSQL .gsp_id == gsp_id )
365
377
366
378
# order, so latest is at the top
367
379
query = query .order_by (ForecastSQL .created_utc .desc ())
0 commit comments