Skip to content

Commit

Permalink
Backport PR #231: Fix device message to stop confusing multiple sched…
Browse files Browse the repository at this point in the history
…ules (#231)

Fixed the *getDeviceMessage* endpoint for cases in which there are multiple schedules available, by returning only the most recent one.

* Query for most recent schedule only

* Fix subquery in get_device_message

* One more fix of subquery in get_device_message

* Add inline comment

* Add todo for moving to the new data model

* Changelog entry

* API changelog entry
  • Loading branch information
Flix6x committed Nov 8, 2021
1 parent dc8c2f7 commit 95e75a3
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
9 changes: 8 additions & 1 deletion documentation/api/change_log.rst
Expand Up @@ -39,7 +39,14 @@ v2.0-0 | 2020-11-14
- REST endpoints for managing assets: `/assets/` (GET, POST) and `/asset/<id>` (GET, PATCH, DELETE).


v1.3.9 | 2021-04-21
v1.3-10 | 2021-11-08
""""""""""""""""""""

*Affects all versions since v1.3*.

- Fixed the *getDeviceMessage* endpoint for cases in which there are multiple schedules available, by returning only the most recent one.

v1.3-9 | 2021-04-21
"""""""""""""""""""

*Affects all versions since v1.0*.
Expand Down
9 changes: 9 additions & 0 deletions documentation/changelog.rst
Expand Up @@ -2,6 +2,15 @@
FlexMeasures Changelog
**********************


v0.7.1 | November XX, 2021
===========================

Bugfixes
-----------
* Fix device messages, which were mixing up older and more recent schedules [see `PR #231 <http://www.github.com/SeitaBV/flexmeasures/pull/231>`_]


v0.7.0 | October 26, 2021
===========================

Expand Down
30 changes: 30 additions & 0 deletions flexmeasures/api/v1_3/implementations.py
Expand Up @@ -8,6 +8,7 @@
import numpy as np
import pandas as pd
from rq.job import Job, NoSuchJobError
from sqlalchemy import and_, func

from flexmeasures.utils.entity_address_utils import (
parse_entity_address,
Expand Down Expand Up @@ -144,12 +145,41 @@ def get_device_message_response(generic_asset_name_groups, duration):
return unknown_schedule(
message + f'no data is known from "{schedule_data_source_name}".'
)

# todo: after moving the Asset's Power data to the corresponding Sensor's TimedBeliefs,
# the query below should be replaced by:
# sensor.search_beliefs(
# event_starts_after=schedule_start,
# event_ends_before=schedule_start + planning_horizon,
# source=scheduler_source,
# most_recent_only=True,
# )

# Subquery to get the most recent schedule only
subq = (
db.session.query(
Power.datetime,
Power.data_source_id,
func.min(Power.horizon).label("most_recent_belief_horizon"),
)
.filter(Power.asset_id == asset.id)
.group_by(Power.datetime, Power.data_source_id)
.subquery()
)
power_values = (
Power.query.filter(Power.asset_id == asset.id)
.filter(Power.data_source_id == scheduler_source.id)
.filter(Power.datetime >= schedule_start)
.filter(Power.datetime < schedule_start + planning_horizon)
.order_by(Power.datetime.asc())
.join(
subq,
and_(
Power.datetime == subq.c.datetime,
Power.data_source_id == subq.c.data_source_id,
Power.horizon == subq.c.most_recent_belief_horizon,
),
)
.all()
)
consumption_schedule = pd.Series(
Expand Down

0 comments on commit 95e75a3

Please sign in to comment.