Skip to content

Commit f027fa6

Browse files
committed
add comments and format
1 parent 699906d commit f027fa6

File tree

2 files changed

+31
-19
lines changed

2 files changed

+31
-19
lines changed

src/quartz_api/internal/service/uk_national/gsp.py

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ async def get_forecasts_for_a_specific_gsp(
6767
- **creation_utc_limit**: optional, only return forecasts made before this datetime.
6868
returns the latest forecast made 60 minutes before the target time)
6969
"""
70+
# set up start and end datetimes
7071
start_datetime_utc = add_timezone(start_datetime_utc)
7172
end_datetime_utc = add_timezone(end_datetime_utc)
7273
creation_utc_limit = add_timezone(creation_utc_limit)
@@ -77,6 +78,7 @@ async def get_forecasts_for_a_specific_gsp(
7778
start_datetime_utc, end_datetime_utc = get_window(start=start_datetime_utc,
7879
end=end_datetime_utc)
7980

81+
# get gsps
8082
gsps = await db.get_solar_regions(type="gsp")
8183
gsp_location = [
8284
site for site in gsps if int(site.region_metadata["gsp_id"]) == gsp_id
@@ -87,6 +89,7 @@ async def get_forecasts_for_a_specific_gsp(
8789
if forecast_horizon_minutes is None:
8890
forecast_horizon = ForecastHorizon.horizon
8991

92+
# get data
9093
predicted_powers = await db.get_predicted_solar_power_production_for_location(
9194
location=gsp_location_uuid,
9295
forecast_horizon=forecast_horizon,
@@ -98,6 +101,7 @@ async def get_forecasts_for_a_specific_gsp(
98101
created_before_datetime=creation_utc_limit,
99102
)
100103

104+
# format data
101105
gsp_forecasts = [
102106
ForecastValue(
103107
target_time=pp.time,
@@ -142,29 +146,31 @@ async def get_truths_for_a_specific_gsp(
142146
Only 3 days of history is available. If you want to get more PVLive data,
143147
please use the [PVLive API](https://www.solar.sheffield.ac.uk/api/)
144148
"""
149+
# set up start and end datetimes
145150
start_datetime_utc = add_timezone(start_datetime_utc)
146151
end_datetime_utc = add_timezone(end_datetime_utc)
147-
148152
start_datetime_utc, end_datetime_utc = get_window(start=start_datetime_utc,
149153
end=end_datetime_utc)
150154

155+
# get gsps
151156
gsps = await db.get_solar_regions(type="gsp")
152-
153157
gsp_location = [
154158
site for site in gsps if int(site.region_metadata["gsp_id"]) == gsp_id
155159
]
156-
157160
gsp_location_uuid = str(gsp_location[0].region_metadata["location_uuid"])
158161

162+
# format regime
159163
regime = regime.replace("-", "_")
160164

165+
# get data
161166
solar_production = await db.get_actual_solar_power_production_for_location(
162167
location=gsp_location_uuid,
163168
observer_name=f"pvlive_{regime}",
164169
start_datetime=start_datetime_utc,
165170
end_datetime=end_datetime_utc,
166171
)
167172

173+
# format data
168174
gsp_yields = [
169175
GSPYield(
170176
datetime_utc=sp.Time,
@@ -177,7 +183,6 @@ async def get_truths_for_a_specific_gsp(
177183

178184

179185
# corresponds to route /v0/solar/GB/gsp/forecast/all/
180-
# TODO currently takes 9 seconds to load, so probably needs optimization
181186
@router.get(
182187
"/forecast/all/",
183188
response_model=list[OneDatetimeManyForecastValuesMW],
@@ -212,8 +217,8 @@ async def get_all_available_forecasts(
212217
- **start_datetime_utc**: optional start datetime for the query. e.g '2023-08-12 10:00:00+00:00'
213218
- **end_datetime_utc**: optional end datetime for the query. e.g '2023-08-12 14:00:00+00:00'
214219
"""
220+
# get all gsp regions
215221
gsps = await db.get_solar_regions(type="gsp")
216-
# might need to add nation location in here too
217222

218223
# format gsp_ids
219224
if isinstance(gsp_ids, str):
@@ -229,7 +234,7 @@ async def get_all_available_forecasts(
229234
if len(gsp_ids) == 0:
230235
gsp_ids = None
231236

232-
# get locations uuids
237+
# get locations uuids and mapping from location uuids -> gsp_ids
233238
location_uuids_to_gsp_id = {
234239
str(gsp.region_metadata["location_uuid"]): int(gsp.region_metadata["gsp_id"])
235240
for gsp in gsps
@@ -241,31 +246,30 @@ async def get_all_available_forecasts(
241246
if gsp_id in gsp_ids
242247
}
243248

249+
# format start, end and creation limit, make sure values are rounded to 30 minutes
244250
start_datetime_utc = add_timezone(start_datetime_utc)
245251
end_datetime_utc = add_timezone(end_datetime_utc)
246252
creation_limit_utc = add_timezone(creation_limit_utc)
247253

254+
# make sure values are rounded to 30 minutes
248255
if start_datetime_utc is not None:
249256
start_datetime_utc = ceil_30_minutes_dt(start_datetime_utc)
250257
if end_datetime_utc is not None:
251258
end_datetime_utc = floor_30_minutes_dt(end_datetime_utc)
252259

253-
# by default, don't get any data in the past if more than one gsp
260+
# Default start and end times don't get any data in the past if more than one gsp
254261
if start_datetime_utc is None and (gsp_ids is None or len(gsp_ids) > 1):
255262
start_datetime_utc = floor_30_minutes_dt(dt.datetime.now(tz=dt.UTC))
256-
257-
if start_datetime_utc is not None:
263+
elif start_datetime_utc is not None:
258264
start_datetime_utc = ceil_30_minutes_dt(start_datetime_utc)
259-
260265
if end_datetime_utc is None:
261266
end_datetime_utc = get_window(start=start_datetime_utc)[1]
262267

263268
# limit end datetime by permissions
264269
permissions = getattr(auth, "permissions", [])
265270
end_datetime_utc = limit_end_datetime_by_permissions(permissions, end_datetime_utc)
266271

267-
268-
#now get the data
272+
# get a list of timestamps to loop over
269273
diff = (end_datetime_utc - start_datetime_utc).total_seconds()
270274
n_half_hours = int((diff // 60 // 30) + 1)
271275
timestamps = [start_datetime_utc \
@@ -276,6 +280,7 @@ async def get_all_available_forecasts(
276280
forecaster_name="blend",
277281
location_uuid=next(iter(location_uuids_to_gsp_id.keys())))
278282

283+
# get the data (async tasks)
279284
forecasts_per_timestamp = []
280285
tasks = []
281286
for timestamp in timestamps:
@@ -291,6 +296,7 @@ async def get_all_available_forecasts(
291296
for exc in filter(lambda x: isinstance(x, Exception), list_results):
292297
raise exc
293298

299+
# format rhe results
294300
for resp in list_results:
295301

296302
if len(resp.forecast_values_kW) == 0:
@@ -308,7 +314,6 @@ async def get_all_available_forecasts(
308314
return forecasts_per_timestamp
309315

310316
# corresponds to API route /v0/solar/GB/gsp/pvlive/all
311-
# TODO currently takes 2 seconds to load, so probably needs optimization
312317
@router.get(
313318
"/pvlive/all",
314319
response_model=list[GSPYieldGroupByDatetime],
@@ -341,6 +346,7 @@ async def get_truths_for_all_gsps(
341346
- **start_datetime_utc**: optional start datetime for the query.
342347
- **end_datetime_utc**: optional end datetime for the query.
343348
"""
349+
# format gsp_ids
344350
try:
345351
if isinstance(gsp_ids, str):
346352
gsp_ids = [int(gsp_id) for gsp_id in gsp_ids.split(",") if gsp_id != ""]
@@ -351,8 +357,10 @@ async def get_truths_for_all_gsps(
351357
detail=f"Invalid GSP IDs format. Tried to convert {gsp_ids} into list of integers",
352358
) from e
353359

360+
# get gsps regions
354361
gsps = await db.get_solar_regions(type="gsp")
355362

363+
# format start and end datetimes
356364
start_datetime_utc = add_timezone(start_datetime_utc)
357365
end_datetime_utc = add_timezone(end_datetime_utc)
358366
start_datetime_utc, end_datetime_utc = get_window(start=start_datetime_utc,
@@ -370,7 +378,7 @@ async def get_truths_for_all_gsps(
370378
if gsp_id in gsp_ids
371379
}
372380

373-
381+
# get the data (async tasks)
374382
tasks = []
375383
for location_uuid in location_uuids_to_gsp_id:
376384
req = db.get_actual_solar_power_production_for_location(
@@ -381,8 +389,6 @@ async def get_truths_for_all_gsps(
381389
)
382390
task = asyncio.create_task(req)
383391
tasks.append(task)
384-
# observation = await self.dp_client.get_observations_as_timeseries(req)
385-
# observations.append(observation)
386392

387393
list_results = await asyncio.gather(*tasks, return_exceptions=True)
388394
for exc in filter(lambda x: isinstance(x, Exception), list_results):

src/quartz_api/internal/service/uk_national/national.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ async def get_national_forecast(
9292
Returns: The national forecast data.
9393
9494
"""
95+
# set up start and end datetimes
9596
start_datetime_utc = add_timezone(start_datetime_utc)
9697
end_datetime_utc = add_timezone(end_datetime_utc)
9798
creation_limit_utc = add_timezone(creation_limit_utc)
@@ -102,17 +103,19 @@ async def get_national_forecast(
102103
start_datetime_utc, end_datetime_utc = get_window(start=start_datetime_utc,
103104
end=end_datetime_utc)
104105

106+
# get model name
105107
model_name = model_names_external_to_internal[model_name]
106108
if trend_adjuster_on:
107109
model_name = model_name + "_adjust"
108110

111+
# get national location UUID and and set forecast horizon
109112
sites = await db.get_solar_regions(type="nation")
110113
national_location_uuid = sites[0].region_metadata["location_uuid"]
111-
112114
forecast_horizon = ForecastHorizon.latest
113115
if forecast_horizon_minutes is not None:
114116
forecast_horizon = ForecastHorizon.horizon
115117

118+
# get data
116119
predicted_powers = await db.get_predicted_solar_power_production_for_location(
117120
location=national_location_uuid,
118121
forecast_horizon=forecast_horizon,
@@ -124,7 +127,7 @@ async def get_national_forecast(
124127
created_before_datetime=creation_limit_utc,
125128
)
126129

127-
130+
# format data
128131
national_forecast_values = [
129132
NationalForecastValue(
130133
target_time=pp.time,
@@ -189,20 +192,23 @@ async def get_national_pvlive(
189192
- **regime**: can choose __in-day__ or __day-after__
190193
191194
"""
195+
# get national location UUID
192196
sites = await db.get_solar_regions(type="nation")
193197
national_location_uuid = sites[0].region_metadata["location_uuid"]
194198

199+
# format regime and get start/end datetimes
195200
regime = regime.replace("-", "_")
196-
197201
start_datetime_utc, end_datetime_utc = get_window()
198202

203+
# get data
199204
solar_production = await db.get_actual_solar_power_production_for_location(
200205
location=national_location_uuid,
201206
observer_name=f"pvlive_{regime}",
202207
start_datetime=start_datetime_utc,
203208
end_datetime=end_datetime_utc,
204209
)
205210

211+
# format data
206212
national_yields = [
207213
NationalYield(
208214
datetime_utc=sp.Time,

0 commit comments

Comments
 (0)