Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: timerange for sensor with a single belief (#732)
* fix: get sensor data timerange in case of a single data point Signed-off-by: F.N. Claessen <felix@seita.nl> * refactor: move to util function Signed-off-by: F.N. Claessen <felix@seita.nl> * fix: apply fix to sensor timerange, too Signed-off-by: F.N. Claessen <felix@seita.nl> * fix: flake8 Signed-off-by: F.N. Claessen <felix@seita.nl> * use min and max in query Signed-off-by: F.N. Claessen <felix@seita.nl> * docs: changelog entry Signed-off-by: F.N. Claessen <felix@seita.nl> --------- Signed-off-by: F.N. Claessen <felix@seita.nl>
- Loading branch information
Showing
4 changed files
with
39 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from __future__ import annotations | ||
|
||
from datetime import datetime | ||
|
||
from sqlalchemy import func | ||
|
||
from flexmeasures.utils import time_utils | ||
|
||
|
||
def get_timerange(sensor_ids: list[int]) -> tuple[datetime, datetime]: | ||
"""Get the start and end of the least recent and most recent event, respectively. | ||
In case of no data, defaults to (now, now). | ||
""" | ||
from flexmeasures.data.models.time_series import Sensor, TimedBelief | ||
|
||
least_recent_event_start_and_most_recent_event_end = ( | ||
TimedBelief.query.with_entities( | ||
# least recent event start | ||
func.min(TimedBelief.event_start), | ||
# most recent event end | ||
func.max(TimedBelief.event_start + Sensor.event_resolution), | ||
) | ||
.join(Sensor, TimedBelief.sensor_id == Sensor.id) | ||
.filter(TimedBelief.sensor_id.in_(sensor_ids)) | ||
).one_or_none() | ||
if least_recent_event_start_and_most_recent_event_end == (None, None): | ||
# return now in case there is no data for any of the sensors | ||
now = time_utils.server_now() | ||
return now, now | ||
return least_recent_event_start_and_most_recent_event_end |