mirror of
https://github.com/Akkudoktor-EOS/EOS.git
synced 2026-07-20 16:58:12 +00:00
fix: move data management to async (#1015)
FAstAPI is an async framework. Data may be imported and exported, load and save, set and get asynchronously. Prevent interleaving data operations to corrupt the data. In the previous design sync and async data access was intermixed leading to data corruption. The basic data classes DataSequence and DataContainer and the derived classes like Provider and Measurement now are async. Data access is protected by several async locks. To support the async design of the data classes the database interface became async. The energy management is also adapted to the new async design. Optimization is still off-loaded to another thread, but the prepration for the optimization and the post optimization actions now follow the async design. Adapter operations are now also protected by async locks. Tests were adapted to the async design and new tests were created. Besides this major fix several other improvements and fixes are included in this PR. * fix: key_to_dict/list/array only regard data records with key value set. Before the exclusion of no value data records was only done if the dropna flag was set. * fix: test for visual result pdf generation Due to updates in the library the generated charts text was a little bit different. Adapt the test to create the comaprison pdf in the test data durectory and update the reference pdf. * chore: Remove MutableMapping from DataSequence and DataContainer. Mutable Mapping does not fit to the now async design. * chore: Add NoDB database backend This backend implements the full database backend interface but performs no actual persistence. It is intended for configurations where database persistence is disabled (`provider=None`). * chore: Improve measurement data import testing with real world scenarios. Added two new endpoints to support testing. * chore: Add mermaid to supported documentation tools * chore: Add documentation about async design * chore: Add documentation about generic data handling Covers the basics of measurement and prediction time series data handling. * chore: Add empty lines around markdown lists. * chore: sync pre-commit config to updated package versions Signed-off-by: Bobby Noelte <b0661n0e17e@gmail.com>
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
"""Tests for fixed electricity price prediction module."""
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
from pathlib import Path
|
||||
from unittest.mock import Mock, patch
|
||||
@@ -91,6 +92,7 @@ def cache_store():
|
||||
return CacheFileStore()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
class TestElecPriceFixed:
|
||||
"""Tests for ElecPriceFixed provider."""
|
||||
|
||||
@@ -109,7 +111,7 @@ class TestElecPriceFixed:
|
||||
provider.config.reset_settings()
|
||||
assert not provider.enabled()
|
||||
|
||||
def test_update_data_hourly_intervals(self, provider, config_eos):
|
||||
async def test_update_data_hourly_intervals(self, provider, config_eos):
|
||||
"""Test updating data with hourly intervals (3600s)."""
|
||||
# Set start datetime
|
||||
ems_eos = get_ems()
|
||||
@@ -121,7 +123,7 @@ class TestElecPriceFixed:
|
||||
config_eos.prediction.hours = 24
|
||||
|
||||
# Update data
|
||||
provider.update_data(force_enable=True, force_update=True)
|
||||
await provider.update_data(force_enable=True, force_update=True)
|
||||
|
||||
# Verify data was generated
|
||||
assert len(provider) == 24 # 24 hours * 1 interval per hour
|
||||
@@ -140,7 +142,7 @@ class TestElecPriceFixed:
|
||||
for i in range(8, 24):
|
||||
assert abs(records[i].elecprice_marketprice_wh - 0.00034) < 1e-6
|
||||
|
||||
def test_update_data_15min_intervals(self, provider, config_eos):
|
||||
async def test_update_data_15min_intervals(self, provider, config_eos):
|
||||
"""Test updating data with 15-minute intervals (900s)."""
|
||||
ems_eos = get_ems()
|
||||
start_dt = to_datetime("2024-01-01 00:00:00", in_timezone="Europe/Berlin")
|
||||
@@ -149,7 +151,7 @@ class TestElecPriceFixed:
|
||||
config_eos.optimization.interval = 900
|
||||
config_eos.prediction.hours = 10 # spans both windows: 00:00–10:00 = 40 intervals
|
||||
|
||||
provider.update_data(force_enable=True, force_update=True)
|
||||
await provider.update_data(force_enable=True, force_update=True)
|
||||
|
||||
# 10 hours * 4 intervals per hour = 40 intervals
|
||||
assert len(provider) == 40
|
||||
@@ -173,7 +175,7 @@ class TestElecPriceFixed:
|
||||
f"Expected day rate at interval {i}, got {records[i].elecprice_marketprice_wh}"
|
||||
)
|
||||
|
||||
def test_update_data_30min_intervals(self, provider, config_eos):
|
||||
async def test_update_data_30min_intervals(self, provider, config_eos):
|
||||
"""Test updating data with 30-minute intervals (1800s)."""
|
||||
ems_eos = get_ems()
|
||||
start_dt = to_datetime("2024-01-01 00:00:00", in_timezone="Europe/Berlin")
|
||||
@@ -182,7 +184,7 @@ class TestElecPriceFixed:
|
||||
config_eos.optimization.interval = 1800
|
||||
config_eos.prediction.hours = 10 # spans both windows: 00:00–10:00 = 20 intervals
|
||||
|
||||
provider.update_data(force_enable=True, force_update=True)
|
||||
await provider.update_data(force_enable=True, force_update=True)
|
||||
|
||||
# 10 hours * 2 intervals per hour = 20 intervals
|
||||
assert len(provider) == 20
|
||||
@@ -206,24 +208,24 @@ class TestElecPriceFixed:
|
||||
f"Expected day rate at interval {i}, got {records[i].elecprice_marketprice_wh}"
|
||||
)
|
||||
|
||||
def test_update_data_without_config(self, provider, config_eos):
|
||||
async def test_update_data_without_config(self, provider, config_eos):
|
||||
"""Test update_data fails without configuration."""
|
||||
# Remove elecpricefixed settings
|
||||
config_eos.elecprice.elecpricefixed = {}
|
||||
|
||||
with pytest.raises(ValueError, match="No time windows configured"):
|
||||
provider.update_data(force_enable=True, force_update=True)
|
||||
await provider.update_data(force_enable=True, force_update=True)
|
||||
|
||||
def test_update_data_without_time_windows(self, provider, config_eos):
|
||||
async def test_update_data_without_time_windows(self, provider, config_eos):
|
||||
"""Test update_data fails without time windows."""
|
||||
# Set empty time windows
|
||||
empty_settings = ElecPriceFixedCommonSettings(time_windows=ValueTimeWindowSequence(windows=[]))
|
||||
config_eos.elecprice.elecpricefixed = empty_settings
|
||||
|
||||
with pytest.raises(ValueError, match="No time windows configured"):
|
||||
provider.update_data(force_enable=True, force_update=True)
|
||||
await provider.update_data(force_enable=True, force_update=True)
|
||||
|
||||
def test_key_to_array_resampling(self, provider, config_eos):
|
||||
async def test_key_to_array_resampling(self, provider, config_eos):
|
||||
"""Test that key_to_array can resample to different intervals."""
|
||||
# Setup provider with hourly data
|
||||
ems_eos = get_ems()
|
||||
@@ -233,10 +235,10 @@ class TestElecPriceFixed:
|
||||
config_eos.optimization.interval = 3600
|
||||
config_eos.prediction.hours = 24
|
||||
|
||||
provider.update_data(force_enable=True, force_update=True)
|
||||
await provider.update_data(force_enable=True, force_update=True)
|
||||
|
||||
# Get data as hourly array (original)
|
||||
hourly_array = provider.key_to_array(
|
||||
hourly_array = await provider.key_to_array(
|
||||
key="elecprice_marketprice_wh",
|
||||
start_datetime=start_dt,
|
||||
end_datetime=start_dt.add(hours=24)
|
||||
@@ -247,7 +249,7 @@ class TestElecPriceFixed:
|
||||
assert abs(hourly_array[8] - 0.00034) < 1e-6 # Day rate
|
||||
|
||||
# Resample to 15-minute intervals
|
||||
quarter_hour_array = provider.key_to_array(
|
||||
quarter_hour_array = await provider.key_to_array(
|
||||
key="elecprice_marketprice_wh",
|
||||
start_datetime=start_dt,
|
||||
end_datetime=start_dt.add(hours=24),
|
||||
@@ -260,7 +262,7 @@ class TestElecPriceFixed:
|
||||
assert abs(quarter_hour_array[i] - 0.000288) < 1e-6
|
||||
|
||||
# Resample to 30-minute intervals
|
||||
half_hour_array = provider.key_to_array(
|
||||
half_hour_array = await provider.key_to_array(
|
||||
key="elecprice_marketprice_wh",
|
||||
start_datetime=start_dt,
|
||||
end_datetime=start_dt.add(hours=24),
|
||||
@@ -277,7 +279,7 @@ class TestElecPriceFixedIntegration:
|
||||
"""Integration tests for ElecPriceFixed."""
|
||||
|
||||
@pytest.mark.skip(reason="For development only")
|
||||
def test_fixed_price_development(self, config_eos):
|
||||
async def test_fixed_price_development(self, config_eos):
|
||||
"""Test fixed price provider with real configuration."""
|
||||
# Create provider with config
|
||||
provider = ElecPriceFixed()
|
||||
@@ -308,7 +310,7 @@ class TestElecPriceFixedIntegration:
|
||||
config_eos.optimization.interval = 900 # 15 minutes
|
||||
|
||||
# Update data
|
||||
provider.update_data(force_enable=True, force_update=True)
|
||||
await provider.update_data(force_enable=True, force_update=True)
|
||||
|
||||
# Verify data
|
||||
expected_intervals = 168 * 4 # 7 days * 24h * 4 intervals
|
||||
|
||||
Reference in New Issue
Block a user