8
8
from datetime import datetime , timedelta , timezone
9
9
from typing import List , Optional
10
10
11
- from sqlalchemy import desc
11
+ from sqlalchemy import desc , text
12
12
from sqlalchemy .orm import contains_eager , joinedload
13
13
from sqlalchemy .orm .session import Session
14
14
@@ -121,7 +121,6 @@ def get_latest_forecast(
121
121
gsp_id : Optional [int ] = None ,
122
122
historic : bool = False ,
123
123
start_target_time : Optional [datetime ] = None ,
124
- forecast_horizon_minutes : Optional [int ] = None ,
125
124
) -> ForecastSQL :
126
125
"""
127
126
Read forecasts
@@ -149,7 +148,6 @@ def get_latest_forecast(
149
148
start_target_time = start_target_time ,
150
149
historic = historic ,
151
150
gsp_ids = gsp_ids ,
152
- forecast_horizon_minutes = forecast_horizon_minutes ,
153
151
)
154
152
155
153
if forecasts is None :
@@ -217,7 +215,6 @@ def get_all_gsp_ids_latest_forecast(
217
215
start_target_time : Optional [datetime ] = None ,
218
216
preload_children : Optional [bool ] = False ,
219
217
historic : bool = False ,
220
- forecast_horizon_minutes : Optional [int ] = None ,
221
218
) -> List [ForecastSQL ]:
222
219
"""
223
220
Read forecasts
@@ -228,9 +225,6 @@ def get_all_gsp_ids_latest_forecast(
228
225
Filter: forecast values target time should be larger than this datetime
229
226
:param preload_children: Option to preload children. This is a speed up, if we need them.
230
227
:param historic: Option to load historic values or not
231
- :param forecast_horizon_minutes: Optional filter on forecast horizon. For example
232
- forecast_horizon_minutes=120, means load the forecast than was made 2 hours before the
233
- target time. Note this only works for non-historic data.
234
228
235
229
return: List of forecasts objects from database
236
230
"""
@@ -244,7 +238,6 @@ def get_all_gsp_ids_latest_forecast(
244
238
preload_children = preload_children ,
245
239
historic = historic ,
246
240
gsp_ids = list (range (0 , N_GSP + 1 )),
247
- forecast_horizon_minutes = forecast_horizon_minutes ,
248
241
)
249
242
250
243
@@ -254,7 +247,6 @@ def get_latest_forecast_for_gsps(
254
247
start_target_time : Optional [datetime ] = None ,
255
248
preload_children : Optional [bool ] = False ,
256
249
historic : bool = False ,
257
- forecast_horizon_minutes : Optional [int ] = None ,
258
250
gsp_ids : List [int ] = None ,
259
251
):
260
252
"""
@@ -267,19 +259,9 @@ def get_latest_forecast_for_gsps(
267
259
:param preload_children: Option to preload children. This is a speed up, if we need them.
268
260
:param historic: Option to load historic values or not
269
261
:param gsp_ids: Option to filter on gsps. If None, then only the lastest forecast is loaded.
270
- :param forecast_horizon_minutes: Optional filter on forecast horizon. For example
271
- forecast_horizon_minutes=120, means load the forecast than was made 2 hours before the
272
- target time. Note this only works for non-historic data.
273
262
274
- return: List of forecasts objects from database
263
+ : return: List of forecasts objects from database
275
264
276
- :param session:
277
- :param start_created_utc:
278
- :param start_target_time:
279
- :param preload_children:
280
- :param historic:
281
- :param gsp_ids:
282
- :return:
283
265
"""
284
266
order_by_cols = []
285
267
@@ -307,17 +289,6 @@ def get_latest_forecast_for_gsps(
307
289
query = query , start_target_time = start_target_time , historic = historic
308
290
)
309
291
310
- from sqlalchemy import text
311
-
312
- if forecast_horizon_minutes is not None :
313
- assert historic is False , Exception (
314
- "Loading a forecast horizon only works on non latest data."
315
- )
316
- query = query .join (ForecastValueSQL ).filter (
317
- ForecastValueSQL .target_time - ForecastValueSQL .created_utc
318
- >= text (f"interval '{ forecast_horizon_minutes } minute'" )
319
- )
320
-
321
292
query = query .join (LocationSQL )
322
293
323
294
# option to preload values, makes querying quicker
@@ -374,6 +345,7 @@ def get_forecast_values(
374
345
session : Session ,
375
346
gsp_id : Optional [int ] = None ,
376
347
start_datetime : Optional [datetime ] = None ,
348
+ forecast_horizon_minutes : Optional [int ] = None ,
377
349
only_return_latest : Optional [bool ] = False ,
378
350
) -> List [ForecastValueSQL ]:
379
351
"""
@@ -386,6 +358,9 @@ def get_forecast_values(
386
358
If None is given then all are returned.
387
359
:param only_return_latest: Optional to only return the latest forecast, not all of them.
388
360
Default is False
361
+ :param forecast_horizon_minutes: Optional filter on forecast horizon. For example
362
+ forecast_horizon_minutes=120, means load the forecast than was made 2 hours before the
363
+ target time. Note this only works for non-historic data.
389
364
390
365
return: List of forecasts values objects from database
391
366
@@ -405,6 +380,14 @@ def get_forecast_values(
405
380
query = query .filter (ForecastValueSQL .created_utc >= created_utc_filter )
406
381
query = query .filter (ForecastSQL .created_utc >= created_utc_filter )
407
382
383
+ if forecast_horizon_minutes is not None :
384
+
385
+ # this seems to only work for postgres
386
+ query = query .filter (
387
+ ForecastValueSQL .target_time - ForecastValueSQL .created_utc
388
+ >= text (f"interval '{ forecast_horizon_minutes } minute'" )
389
+ )
390
+
408
391
# filter on gsp_id
409
392
if gsp_id is not None :
410
393
query = query .join (ForecastSQL )
0 commit comments