Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix device message to stop confusing multiple schedules #231

Merged
merged 7 commits into from Nov 8, 2021
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 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 @@ -143,12 +144,31 @@ def get_device_message_response(generic_asset_name_groups, duration):
return unknown_schedule(
message + f'no data is known from "{schedule_data_source_name}".'
)
# 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"),
Flix6x marked this conversation as resolved.
Show resolved Hide resolved
)
.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