/
implementations.py
328 lines (295 loc) · 12.1 KB
/
implementations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import isodate
from typing import Dict, List, Optional, Tuple, Union
from datetime import datetime as datetime_type, timedelta
from flask import current_app, request
from flask_json import as_json
from flask_security import current_user
import timely_beliefs as tb
from flexmeasures.utils.entity_address_utils import (
parse_entity_address,
EntityAddressException,
)
from flexmeasures.data.models.assets import Power
from flexmeasures.data.models.data_sources import get_or_create_source
from flexmeasures.data.models.time_series import Sensor
from flexmeasures.data.services.resources import get_sensors
from flexmeasures.data.services.forecasting import create_forecasting_jobs
from flexmeasures.api.common.responses import (
invalid_domain,
invalid_role,
power_value_too_big,
power_value_too_small,
unrecognized_connection_group,
request_processed,
)
from flexmeasures.api.common.utils.api_utils import (
groups_to_dict,
save_to_db,
)
from flexmeasures.api.common.utils.validators import (
type_accepted,
units_accepted,
assets_required,
optional_user_sources_accepted,
post_data_checked_for_required_resolution,
get_data_downsampling_allowed,
optional_horizon_accepted,
optional_prior_accepted,
period_required,
values_required,
)
@type_accepted("GetMeterDataRequest")
@units_accepted("power", "MW")
@assets_required("connection")
@optional_user_sources_accepted(default_source="MDC")
@optional_horizon_accepted(
ex_post=True, infer_missing=False, accept_repeating_interval=True
)
@optional_prior_accepted(ex_post=True, infer_missing=False)
@period_required
@get_data_downsampling_allowed("connection", "fm0")
@as_json
def get_meter_data_response(
unit,
resolution,
generic_asset_name_groups,
horizon,
prior,
start,
duration,
user_source_ids,
) -> Tuple[dict, int]:
"""
Read out the power values for each asset.
The response message has a different structure depending on:
1) the number of connections for which meter data is requested, and
2) whether the time window in the request maps an integer number of time slots for the meter data
In all cases, the API defaults to use shorthand for univariate timeseries data,
in which the data resolution can be derived by dividing the duration of the time window over the number of values.
"""
# Any meter data observed at most <horizon> after the fact and not before the fact
belief_horizon_window = (horizon, timedelta(hours=0))
# Any meter data observed at least before <prior>
belief_time_window = (None, prior)
# Check the user's intention first, fall back to other data from script
source_types = ["user", "script"]
return collect_connection_and_value_groups(
unit,
resolution,
belief_horizon_window,
belief_time_window,
start,
duration,
generic_asset_name_groups,
user_source_ids,
source_types,
)
@type_accepted("PostMeterDataRequest")
@units_accepted("power", "MW")
@assets_required("connection")
@values_required
@optional_horizon_accepted(
ex_post=True, infer_missing=True, accept_repeating_interval=True
)
@period_required
@post_data_checked_for_required_resolution("connection", "fm0")
@as_json
def post_meter_data_response(
unit,
generic_asset_name_groups,
value_groups,
horizon,
rolling,
start,
duration,
resolution,
) -> Union[dict, Tuple[dict, int]]:
"""
Store the new power values for each asset.
"""
return create_connection_and_value_groups(
unit, generic_asset_name_groups, value_groups, horizon, rolling, start, duration
)
@as_json
def get_service_response(service_listing) -> Union[dict, Tuple[dict, int]]:
"""
Lists the available services for the public endpoint version,
either all of them or only those that apply to the requested access role.
"""
requested_access_role = request.args.get("access")
response = {"version": service_listing["version"]}
if requested_access_role:
accessible_services = []
for service in service_listing["services"]:
if requested_access_role in service["access"]:
accessible_services.append(service)
response["services"] = accessible_services
if not accessible_services:
return invalid_role(requested_access_role)
else:
response["services"] = service_listing["services"]
d, s = request_processed()
return dict(**response, **d), s
def collect_connection_and_value_groups(
unit: str,
resolution: str,
belief_horizon_window: Tuple[Union[None, timedelta], Union[None, timedelta]],
belief_time_window: Tuple[Optional[datetime_type], Optional[datetime_type]],
start: datetime_type,
duration: timedelta,
connection_groups: List[List[str]],
user_source_ids: Union[int, List[int]] = None, # None is interpreted as all sources
source_types: List[str] = None,
) -> Tuple[dict, int]:
"""
Code for GETting power values from the API.
Only allows to get values from assets owned by current user.
Returns value sign in accordance with USEF specs
(with negative production and positive consumption).
"""
current_app.logger.info("GETTING")
user_sensors = get_sensors()
if not user_sensors:
current_app.logger.info("User doesn't seem to have any assets")
user_sensor_ids = [sensor.id for sensor in user_sensors]
end = start + duration
value_groups = []
new_connection_groups = (
[]
) # Each connection in the old connection groups will be interpreted as a separate group
for connections in connection_groups:
# Get the sensor names
sensor_names: List[str] = []
for connection in connections:
# Parse the entity address
try:
connection_details = parse_entity_address(
connection, entity_type="connection", fm_scheme="fm0"
)
except EntityAddressException as eae:
return invalid_domain(str(eae))
sensor_id = connection_details["asset_id"]
# Look for the Sensor object
if sensor_id in user_sensor_ids:
sensor = Sensor.query.filter(Sensor.id == sensor_id).one_or_none()
else:
current_app.logger.warning("Cannot identify connection %s" % connection)
return unrecognized_connection_group()
sensor_names.append(sensor.name)
# Get the power values
# TODO: fill NaN for non-existing values
power_bdf_dict: Dict[str, tb.BeliefsDataFrame] = Power.search(
old_sensor_names=sensor_names,
event_starts_after=start,
event_ends_before=end,
resolution=resolution,
horizons_at_least=belief_horizon_window[0],
horizons_at_most=belief_horizon_window[1],
beliefs_after=belief_time_window[0],
beliefs_before=belief_time_window[1],
user_source_ids=user_source_ids,
source_types=source_types,
sum_multiple=False,
)
# Todo: parse time window of power_bdf_dict, which will be different for requests that are not of the form:
# - start is a timestamp on the hour or a multiple of 15 minutes thereafter
# - duration is a multiple of 15 minutes
for k, bdf in power_bdf_dict.items():
value_groups.append(
[x * -1 for x in bdf["event_value"].tolist()]
) # Reverse sign of values (from FlexMeasures specs to USEF specs)
new_connection_groups.append(k)
response = groups_to_dict(
new_connection_groups, value_groups, generic_asset_type_name="connection"
)
response["start"] = isodate.datetime_isoformat(start)
response["duration"] = isodate.duration_isoformat(duration)
response["unit"] = unit # TODO: convert to requested unit
d, s = request_processed()
return dict(**response, **d), s
def create_connection_and_value_groups( # noqa: C901
unit, generic_asset_name_groups, value_groups, horizon, rolling, start, duration
):
"""
Code for POSTing Power values to the API.
Only lets users post to assets they own.
The sign of values is validated according to asset specs, but in USEF terms.
Then, we store the reverse sign for FlexMeasures specs (with positive production
and negative consumption).
If power values are not forecasts, forecasting jobs are created.
"""
current_app.logger.info("POSTING POWER DATA")
data_source = get_or_create_source(current_user)
user_sensors = get_sensors()
if not user_sensors:
current_app.logger.info("User doesn't seem to have any assets")
user_sensor_ids = [sensor.id for sensor in user_sensors]
power_measurements = []
forecasting_jobs = []
for connection_group, value_group in zip(generic_asset_name_groups, value_groups):
for connection in connection_group:
# TODO: get asset through util function after refactoring
# Parse the entity address
try:
connection = parse_entity_address(
connection, entity_type="connection", fm_scheme="fm0"
)
except EntityAddressException as eae:
return invalid_domain(str(eae))
sensor_id = connection["asset_id"]
# Look for the Sensor object
if sensor_id in user_sensor_ids:
sensor = Sensor.query.filter(Sensor.id == sensor_id).one_or_none()
else:
current_app.logger.warning("Cannot identify connection %s" % connection)
return unrecognized_connection_group()
# Validate the sign of the values (following USEF specs with positive consumption and negative production)
if sensor.get_attribute("is_strictly_non_positive") and any(
v < 0 for v in value_group
):
extra_info = (
"Connection %s is registered as a pure consumer and can only receive non-negative values."
% sensor.entity_address
)
return power_value_too_small(extra_info)
elif sensor.get_attribute("is_strictly_non_negative") and any(
v > 0 for v in value_group
):
extra_info = (
"Connection %s is registered as a pure producer and can only receive non-positive values."
% sensor.entity_address
)
return power_value_too_big(extra_info)
# Create new Power objects
for j, value in enumerate(value_group):
dt = start + j * duration / len(value_group)
if rolling:
h = horizon
else: # Deduct the difference in end times of the individual timeslot and the timeseries duration
h = horizon - (
(start + duration) - (dt + duration / len(value_group))
)
p = Power(
datetime=dt,
value=value
* -1, # Reverse sign for FlexMeasures specs with positive production and negative consumption
horizon=h,
sensor_id=sensor_id,
data_source_id=data_source.id,
)
power_measurements.append(p)
# make forecasts, but only if the sent-in values are not forecasts themselves
if horizon <= timedelta(
hours=0
): # Todo: replace 0 hours with whatever the moment of switching from ex-ante to ex-post is for this sensor
forecasting_jobs.extend(
create_forecasting_jobs(
Power,
sensor_id,
start,
start + duration,
resolution=duration / len(value_group),
enqueue=False,
)
)
return save_to_db(power_measurements, forecasting_jobs)