Skip to content

Commit

Permalink
Add flag to filter the data with a source in its latest version.
Browse files Browse the repository at this point in the history
Signed-off-by: Victor Garcia Reolid <victor@seita.nl>
  • Loading branch information
victorgarcia98 committed Apr 24, 2024
1 parent 031d6d6 commit 27da01c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
17 changes: 16 additions & 1 deletion flexmeasures/data/models/reporting/pandas_reporter.py
Expand Up @@ -3,6 +3,7 @@
from typing import Any
from datetime import datetime, timedelta
from copy import deepcopy, copy
from packaging.version import Version

from flask import current_app
import timely_beliefs as tb
Expand Down Expand Up @@ -38,6 +39,7 @@ def fetch_data(
input: dict,
resolution: timedelta | None = None,
belief_time: datetime | None = None,
use_latest_version_only: bool = False,
):
"""
Fetches the time_beliefs from the database
Expand All @@ -58,12 +60,22 @@ def fetch_data(
event_ends_before = _input_search_parameters.pop("event_ends_before", end)
resolution = _input_search_parameters.pop("resolution", resolution)
belief_time = _input_search_parameters.pop("belief_time", belief_time)
sources = None

if use_latest_version_only:
sources = sensor.data_sources
if len(sources) > 0:
sources = max(
sources,
key=lambda x: Version(x.version if x.version else "0.0.0"),
)

bdf = sensor.search_beliefs(
event_starts_after=event_starts_after,
event_ends_before=event_ends_before,
resolution=resolution,
beliefs_before=belief_time,
source=sources,
**_input_search_parameters,
)

Expand All @@ -89,13 +101,16 @@ def _compute_report(self, **kwargs) -> list[dict[str, Any]]:
belief_time: datetime | None = kwargs.get("belief_time", None)
belief_horizon: timedelta | None = kwargs.get("belief_horizon", None)
output: list[dict[str, Any]] = kwargs.get("output")
use_latest_version_only: bool = kwargs.get("use_latest_version_only", False)

# by default, use the minimum resolution among the input sensors
if resolution is None:
resolution = min([i["sensor"].event_resolution for i in input])

# fetch sensor data
self.fetch_data(start, end, input, resolution, belief_time)
self.fetch_data(
start, end, input, resolution, belief_time, use_latest_version_only
)

if belief_time is None:
belief_time = server_now()
Expand Down
1 change: 1 addition & 0 deletions flexmeasures/data/schemas/reporting/pandas_reporter.py
Expand Up @@ -139,6 +139,7 @@ class PandasReporterParametersSchema(ReporterParametersSchema):
# for the single sensors in `input_variables`
start = AwareDateTimeField(required=False)
end = AwareDateTimeField(required=False)
use_latest_version_only = fields.Bool(required=False, defaul=False)

@validates_schema
def validate_time_parameters(self, data, **kwargs):
Expand Down

0 comments on commit 27da01c

Please sign in to comment.