@@ -136,54 +136,56 @@ def get_latest_forecast(
136
136
return: List of forecasts objects from database
137
137
"""
138
138
139
- logger .debug ("getting latest forecast" )
139
+ logger .debug (f"Getting latest forecast for gsp { gsp_id } " )
140
140
141
141
# start main query
142
142
query = session .query (ForecastSQL )
143
143
order_by_items = []
144
144
145
145
if historic :
146
146
query = query .filter (ForecastSQL .historic == true ())
147
- join_object = ForecastSQL .forecast_values_latest
148
- forecast_value_model = ForecastValueLatestSQL
149
147
else :
150
148
query = query .filter (ForecastSQL .historic == false ())
151
- join_object = ForecastSQL .forecast_values
152
- forecast_value_model = ForecastValueSQL
149
+
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 )
153
154
154
155
# filter on gsp_id
155
156
if gsp_id is not None :
156
157
query = query .join (LocationSQL )
157
158
query = query .filter (LocationSQL .gsp_id == gsp_id )
158
159
order_by_items .append (LocationSQL .gsp_id )
159
160
160
- if start_target_time is not None :
161
- query = (
162
- query .join (join_object )
163
- .filter (forecast_value_model .target_time >= start_target_time )
164
- .options (contains_eager (join_object ))
165
- .populate_existing ()
166
- )
167
-
168
161
order_by_items .append (ForecastSQL .created_utc .desc ())
169
162
170
163
# this make the newest ones comes to the top
171
164
query = query .order_by (* order_by_items )
172
165
173
166
# get all results
174
- forecasts = query .first ()
167
+ if not historic :
168
+ query = query .limit (1 )
169
+ forecasts = query .all ()
170
+
171
+ if forecasts is None :
172
+ return None
173
+ if len (forecasts ) == 0 :
174
+ return None
175
+
176
+ forecast = forecasts [0 ]
175
177
176
178
# sort list
177
- if forecasts is not None :
178
- logger . debug ( "sorting 'forecast_values_latest' values " )
179
- if forecasts .forecast_values_latest is not None :
180
- forecasts .forecast_values_latest = sorted (
181
- forecasts .forecast_values_latest , key = lambda d : d .target_time
182
- )
179
+ logger . debug ( f"sorting 'forecast_values_latest' values. "
180
+ f"There are { len ( forecast . forecast_values_latest ) } " )
181
+ if forecast .forecast_values_latest is not None :
182
+ forecast .forecast_values_latest = sorted (
183
+ forecast .forecast_values_latest , key = lambda d : d .target_time
184
+ )
183
185
184
- logger .debug (f"Found forecasts for gsp id: { gsp_id } { historic = } { forecasts = } " )
186
+ logger .debug (f"Found forecasts for gsp id: { gsp_id } { historic = } { forecast = } " )
185
187
186
- return forecasts
188
+ return forecast
187
189
188
190
189
191
def get_all_gsp_ids_latest_forecast (
@@ -206,12 +208,7 @@ def get_all_gsp_ids_latest_forecast(
206
208
return: List of forecasts objects from database
207
209
"""
208
210
209
- if historic :
210
- forecast_value_model = ForecastValueLatestSQL
211
- join_object = ForecastSQL .forecast_values_latest
212
- else :
213
- forecast_value_model = ForecastValueSQL
214
- join_object = ForecastSQL .forecast_values
211
+ logger .debug ("Getting latest forecast for all gsps" )
215
212
216
213
# start main query
217
214
query = session .query (ForecastSQL )
@@ -220,33 +217,56 @@ def get_all_gsp_ids_latest_forecast(
220
217
query = query .filter (ForecastSQL .created_utc >= start_created_utc )
221
218
222
219
if start_target_time is not None :
223
- query = (
224
- query .join (join_object )
225
- .filter (forecast_value_model .target_time >= start_target_time )
226
- .options (contains_eager (join_object ))
227
- .populate_existing ()
228
- )
220
+ query = filter_query_on_target_time (query = query ,
221
+ start_target_time = start_target_time ,
222
+ historic = historic )
229
223
230
224
# join with tables
231
- query = query .distinct (LocationSQL .gsp_id )
225
+ if not historic :
226
+ query = query .distinct (LocationSQL .gsp_id )
232
227
query = query .join (LocationSQL )
233
228
234
229
query = query .filter (ForecastSQL .historic == historic )
235
230
236
231
query = query .order_by (LocationSQL .gsp_id , desc (ForecastSQL .created_utc ))
237
232
238
233
if preload_children :
239
- query = query .options (joinedload (ForecastSQL .forecast_values_latest ))
240
- query = query .options (joinedload (ForecastSQL .forecast_values ))
241
234
query = query .options (joinedload (ForecastSQL .location ))
242
235
query = query .options (joinedload (ForecastSQL .model ))
243
236
query = query .options (joinedload (ForecastSQL .input_data_last_updated ))
244
237
245
- forecasts = query .limit ( 339 ). all ()
238
+ forecasts = query .all ()
246
239
247
240
return forecasts
248
241
249
242
243
+ def filter_query_on_target_time (query , start_target_time , historic : bool ):
244
+ """
245
+ Filter query on start target time
246
+
247
+ :param query: sql query
248
+ :param start_target_time: datetime, target times only included after this
249
+ :param historic: bool, if data is historic or latest
250
+ :return: query
251
+ """
252
+ if historic :
253
+ forecast_value_model = ForecastValueLatestSQL
254
+ join_object = ForecastSQL .forecast_values_latest
255
+ else :
256
+ forecast_value_model = ForecastValueSQL
257
+ join_object = ForecastSQL .forecast_values
258
+
259
+ 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 ()
265
+ )
266
+
267
+ return query
268
+
269
+
250
270
def get_forecast_values (
251
271
session : Session ,
252
272
gsp_id : Optional [int ] = None ,
0 commit comments