Skip to content

Commit

Permalink
Use 2 times sensor resolution instead of instant staleness as default
Browse files Browse the repository at this point in the history
Signed-off-by: Nikolai <nrozanov@iponweb.net>
  • Loading branch information
Nikolai committed Jun 23, 2024
1 parent 28ec8cc commit 13d028e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
40 changes: 40 additions & 0 deletions flexmeasures/api/common/schemas/tests/test_sensor_data_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,46 @@ def test_get_status(
assert sensor_status["stale"] == expected_stale


@pytest.mark.parametrize(
"now, expected_staleness, expected_stale, expected_stale_reason",
[
# sensor resolution is 15 min
(
# Last event start at 2016-01-02T07:45+01, with knowledge time 2016-01-02T08:00+01, 29 minutes ago
"2016-01-02T08:29+01",
timedelta(minutes=29),
False,
"not more than 30 minutes old",
),
(
# Last event start at 2016-01-02T07:45+01, with knowledge time 2016-01-02T08:00+01, 31 minutes ago
"2016-01-02T08:31+01",
timedelta(minutes=31),
True,
"more than 30 minutes old",
),
],
)
def test_get_status_no_status_specs(
capacity_sensors,
now,
expected_staleness,
expected_stale,
expected_stale_reason,
):
sensor = capacity_sensors["production"]
now = pd.Timestamp(now)
sensor_status = get_status(
sensor=sensor,
status_specs=None,
now=now,
)

assert sensor_status["staleness"] == expected_staleness
assert sensor_status["stale"] == expected_stale
assert sensor_status["reason"] == expected_stale_reason


def test_build_asset_status_data(mock_get_status, add_weather_sensors):
asset = add_weather_sensors["asset"]

Expand Down
8 changes: 6 additions & 2 deletions flexmeasures/data/services/sensors.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import hashlib
from datetime import datetime, timedelta
from flask import current_app
from isodate import duration_isoformat
from timely_beliefs import BeliefsDataFrame

from humanize.time import naturaldelta
Expand Down Expand Up @@ -127,8 +128,11 @@ def get_status_specs(sensor: Sensor) -> dict:
if sensor.knowledge_horizon_fnc == "x_days_ago_at_y_oclock":
status_specs = {"staleness_search": {}, "max_staleness": "P1D"}
else:
# Default to status specs indicating immediate staleness after knowledge time
status_specs = {"staleness_search": {}, "max_staleness": "PT0H"}
# Default to status specs indicating staleness after knowledge time + 2 sensor resolutions
status_specs = {
"staleness_search": {},
"max_staleness": duration_isoformat(sensor.event_resolution * 2),
}
return status_specs


Expand Down

0 comments on commit 13d028e

Please sign in to comment.