mirror of
https://github.com/Akkudoktor-EOS/EOS.git
synced 2026-02-24 01:46:21 +00:00
Add database support for measurements and historic prediction data. (#848)
The database supports backend selection, compression, incremental data load, automatic data saving to storage, automatic vaccum and compaction. Make SQLite3 and LMDB database backends available. Update tests for new interface conventions regarding data sequences, data containers, data providers. This includes the measurements provider and the prediction providers. Add database documentation. The fix includes several bug fixes that are not directly related to the database implementation but are necessary to keep EOS running properly and to test and document the changes. * fix: config eos test setup Make the config_eos fixture generate a new instance of the config_eos singleton. Use correct env names to setup data folder path. * fix: startup with no config Make cache and measurements complain about missing data path configuration but do not bail out. * fix: soc data preparation and usage for genetic optimization. Search for soc measurments 48 hours around the optimization start time. Only clamp soc to maximum in battery device simulation. * fix: dashboard bailout on zero value solution display Do not use zero values to calculate the chart values adjustment for display. * fix: openapi generation script Make the script also replace data_folder_path and data_output_path to hide real (test) environment pathes. * feat: add make repeated task function make_repeated_task allows to wrap a function to be repeated cyclically. * chore: removed index based data sequence access Index based data sequence access does not make sense as the sequence can be backed by the database. The sequence is now purely time series data. * chore: refactor eos startup to avoid module import startup Avoid module import initialisation expecially of the EOS configuration. Config mutation, singleton initialization, logging setup, argparse parsing, background task definitions depending on config and environment-dependent behavior is now done at function startup. * chore: introduce retention manager A single long-running background task that owns the scheduling of all periodic server-maintenance jobs (cache cleanup, DB autosave, …) * chore: canonicalize timezone name for UTC Timezone names that are semantically identical to UTC are canonicalized to UTC. * chore: extend config file migration for default value handling Extend the config file migration handling values None or nonexisting values that will invoke a default value generation in the new config file. Also adapt test to handle this situation. * chore: extend datetime util test cases * chore: make version test check for untracked files Check for files that are not tracked by git. Version calculation will be wrong if these files will not be commited. * chore: bump pandas to 3.0.0 Pandas 3.0 now performs inference on the appropriate resolution (a.k.a. unit) for the output dtype which may become datetime64[us] (before it was ns). Also numeric dtype detection is now more strict which needs a different detection for numerics. * chore: bump pydantic-settings to 2.12.0 pydantic-settings 2.12.0 under pytest creates a different behaviour. The tests were adapted and a workaround was introduced. Also ConfigEOS was adapted to allow for fine grain initialization control to be able to switch off certain settings such as file settings during test. * chore: remove sci learn kit from dependencies The sci learn kit is not strictly necessary as long as we have scipy. * chore: add documentation mode guarding for sphinx autosummary Sphinx autosummary excecutes functions. Prevent exceptions in case of pure doc mode. * chore: adapt docker-build CI workflow to stricter GitHub handling Signed-off-by: Bobby Noelte <b0661n0e17e@gmail.com>
This commit is contained in:
@@ -3,10 +3,17 @@ import pytest
|
||||
from pendulum import datetime, duration
|
||||
|
||||
from akkudoktoreos.config.config import SettingsEOS
|
||||
from akkudoktoreos.core.coreabc import get_measurement
|
||||
from akkudoktoreos.measurement.measurement import (
|
||||
MeasurementCommonSettings,
|
||||
MeasurementDataRecord,
|
||||
get_measurement,
|
||||
)
|
||||
from akkudoktoreos.utils.datetimeutil import (
|
||||
DateTime,
|
||||
Duration,
|
||||
compare_datetimes,
|
||||
to_datetime,
|
||||
to_duration,
|
||||
)
|
||||
|
||||
|
||||
@@ -41,8 +48,9 @@ class TestMeasurementDataRecord:
|
||||
|
||||
def test_getitem_existing_field(self, record):
|
||||
"""Test that __getitem__ returns correct value for existing native field."""
|
||||
record.date_time = "2024-01-01T00:00:00+00:00"
|
||||
assert record["date_time"] is not None
|
||||
date_time = "2024-01-01T00:00:00+00:00"
|
||||
record.date_time = date_time
|
||||
assert compare_datetimes(record["date_time"], to_datetime(date_time)).equal
|
||||
|
||||
def test_getitem_existing_measurement(self, record):
|
||||
"""Test that __getitem__ retrieves existing measurement values."""
|
||||
@@ -220,6 +228,7 @@ class TestMeasurement:
|
||||
# Load meter readings are in kWh
|
||||
config_eos.measurement.load_emr_keys = ["load0_mr", "load1_mr", "load2_mr", "load3_mr"]
|
||||
measurement = get_measurement()
|
||||
measurement.delete_by_datetime(None, None)
|
||||
record0 = MeasurementDataRecord(
|
||||
date_time=datetime(2023, 1, 1, hour=0),
|
||||
load0_mr=100,
|
||||
@@ -227,52 +236,54 @@ class TestMeasurement:
|
||||
)
|
||||
assert record0.load0_mr == 100
|
||||
assert record0.load1_mr == 200
|
||||
measurement.records = [
|
||||
records = [
|
||||
MeasurementDataRecord(
|
||||
date_time=datetime(2023, 1, 1, hour=0),
|
||||
date_time=to_datetime("2023-01-01T00:00:00"),
|
||||
load0_mr=100,
|
||||
load1_mr=200,
|
||||
),
|
||||
MeasurementDataRecord(
|
||||
date_time=datetime(2023, 1, 1, hour=1),
|
||||
date_time=to_datetime("2023-01-01T01:00:00"),
|
||||
load0_mr=150,
|
||||
load1_mr=250,
|
||||
),
|
||||
MeasurementDataRecord(
|
||||
date_time=datetime(2023, 1, 1, hour=2),
|
||||
date_time=to_datetime("2023-01-01T02:00:00"),
|
||||
load0_mr=200,
|
||||
load1_mr=300,
|
||||
),
|
||||
MeasurementDataRecord(
|
||||
date_time=datetime(2023, 1, 1, hour=3),
|
||||
date_time=to_datetime("2023-01-01T03:00:00"),
|
||||
load0_mr=250,
|
||||
load1_mr=350,
|
||||
),
|
||||
MeasurementDataRecord(
|
||||
date_time=datetime(2023, 1, 1, hour=4),
|
||||
date_time=to_datetime("2023-01-01T04:00:00"),
|
||||
load0_mr=300,
|
||||
load1_mr=400,
|
||||
),
|
||||
MeasurementDataRecord(
|
||||
date_time=datetime(2023, 1, 1, hour=5),
|
||||
date_time=to_datetime("2023-01-01T05:00:00"),
|
||||
load0_mr=350,
|
||||
load1_mr=450,
|
||||
),
|
||||
]
|
||||
for record in records:
|
||||
measurement.insert_by_datetime(record)
|
||||
return measurement
|
||||
|
||||
def test_interval_count(self, measurement_eos):
|
||||
"""Test interval count calculation."""
|
||||
start = datetime(2023, 1, 1, 0)
|
||||
end = datetime(2023, 1, 1, 3)
|
||||
start = to_datetime("2023-01-01T00:00:00")
|
||||
end = to_datetime("2023-01-01T03:00:00")
|
||||
interval = duration(hours=1)
|
||||
|
||||
assert measurement_eos._interval_count(start, end, interval) == 3
|
||||
|
||||
def test_interval_count_invalid_end_before_start(self, measurement_eos):
|
||||
"""Test interval count raises ValueError when end_datetime is before start_datetime."""
|
||||
start = datetime(2023, 1, 1, 3)
|
||||
end = datetime(2023, 1, 1, 0)
|
||||
start = to_datetime("2023-01-01T03:00:00")
|
||||
end = to_datetime("2023-01-01T00:00:00")
|
||||
interval = duration(hours=1)
|
||||
|
||||
with pytest.raises(ValueError, match="end_datetime must be after start_datetime"):
|
||||
@@ -280,8 +291,8 @@ class TestMeasurement:
|
||||
|
||||
def test_interval_count_invalid_non_positive_interval(self, measurement_eos):
|
||||
"""Test interval count raises ValueError when interval is non-positive."""
|
||||
start = datetime(2023, 1, 1, 0)
|
||||
end = datetime(2023, 1, 1, 3)
|
||||
start = to_datetime("2023-01-01T00:00:00")
|
||||
end = to_datetime("2023-01-01T03:00:00")
|
||||
|
||||
with pytest.raises(ValueError, match="interval must be positive"):
|
||||
measurement_eos._interval_count(start, end, duration(hours=0))
|
||||
@@ -289,8 +300,8 @@ class TestMeasurement:
|
||||
def test_energy_from_meter_readings_valid_input(self, measurement_eos):
|
||||
"""Test _energy_from_meter_readings with valid inputs and proper alignment of load data."""
|
||||
key = "load0_mr"
|
||||
start_datetime = datetime(2023, 1, 1, 0)
|
||||
end_datetime = datetime(2023, 1, 1, 5)
|
||||
start_datetime = to_datetime("2023-01-01T00:00:00")
|
||||
end_datetime = to_datetime("2023-01-01T05:00:00")
|
||||
interval = duration(hours=1)
|
||||
|
||||
load_array = measurement_eos._energy_from_meter_readings(
|
||||
@@ -303,12 +314,12 @@ class TestMeasurement:
|
||||
def test_energy_from_meter_readings_empty_array(self, measurement_eos):
|
||||
"""Test _energy_from_meter_readings with no data (empty array)."""
|
||||
key = "load0_mr"
|
||||
start_datetime = datetime(2023, 1, 1, 0)
|
||||
end_datetime = datetime(2023, 1, 1, 5)
|
||||
start_datetime = to_datetime("2023-01-01T00:00:00")
|
||||
end_datetime = to_datetime("2023-01-01T05:00:00")
|
||||
interval = duration(hours=1)
|
||||
|
||||
# Use empyt records array
|
||||
measurement_eos.records = []
|
||||
measurement_eos.delete_by_datetime(start_datetime, end_datetime)
|
||||
|
||||
load_array = measurement_eos._energy_from_meter_readings(
|
||||
key, start_datetime, end_datetime, interval
|
||||
@@ -324,25 +335,46 @@ class TestMeasurement:
|
||||
def test_energy_from_meter_readings_misaligned_array(self, measurement_eos):
|
||||
"""Test _energy_from_meter_readings with misaligned array size."""
|
||||
key = "load1_mr"
|
||||
start_datetime = measurement_eos.min_datetime
|
||||
end_datetime = measurement_eos.max_datetime
|
||||
interval = duration(hours=1)
|
||||
start_datetime = to_datetime("2023-01-01T00:00:00")
|
||||
end_datetime = to_datetime("2023-01-01T05:00:00")
|
||||
|
||||
# Use misaligned array, latest interval set to 2 hours (instead of 1 hour)
|
||||
measurement_eos.records[-1].date_time = datetime(2023, 1, 1, 6)
|
||||
latest_record_datetime = to_datetime("2023-01-01T05:00:00")
|
||||
new_record_datetime = to_datetime("2023-01-01T06:00:00")
|
||||
record = measurement_eos.get_by_datetime(latest_record_datetime)
|
||||
assert record is not None
|
||||
measurement_eos.delete_by_datetime(start_datetime = latest_record_datetime,
|
||||
end_datetime = new_record_datetime)
|
||||
record.date_time = new_record_datetime
|
||||
measurement_eos.insert_by_datetime(record)
|
||||
|
||||
# Check test setup
|
||||
dates, values = measurement_eos.key_to_lists(key, start_datetime, None)
|
||||
assert dates == [
|
||||
to_datetime("2023-01-01T00:00:00"),
|
||||
to_datetime("2023-01-01T01:00:00"),
|
||||
to_datetime("2023-01-01T02:00:00"),
|
||||
to_datetime("2023-01-01T03:00:00"),
|
||||
to_datetime("2023-01-01T04:00:00"),
|
||||
to_datetime("2023-01-01T06:00:00"),
|
||||
]
|
||||
assert values == [200, 250, 300, 350, 400, 450]
|
||||
array = measurement_eos.key_to_array(key, start_datetime, end_datetime + interval, interval=interval)
|
||||
np.testing.assert_array_equal(array, [200, 250, 300, 350, 400, 425])
|
||||
|
||||
load_array = measurement_eos._energy_from_meter_readings(
|
||||
key, start_datetime, end_datetime, interval
|
||||
)
|
||||
|
||||
expected_load_array = np.array([50, 50, 50, 50, 25]) # Differences between consecutive readings
|
||||
expected_load_array = np.array([50., 50., 50., 50., 25.]) # Differences between consecutive readings
|
||||
np.testing.assert_array_equal(load_array, expected_load_array)
|
||||
|
||||
def test_energy_from_meter_readings_partial_data(self, measurement_eos, caplog):
|
||||
"""Test _energy_from_meter_readings with partial data (misaligned but empty array)."""
|
||||
key = "load2_mr"
|
||||
start_datetime = datetime(2023, 1, 1, 0)
|
||||
end_datetime = datetime(2023, 1, 1, 5)
|
||||
start_datetime = to_datetime("2023-01-01T00:00:00")
|
||||
end_datetime = to_datetime("2023-01-01T05:00:00")
|
||||
interval = duration(hours=1)
|
||||
|
||||
with caplog.at_level("DEBUG"):
|
||||
@@ -359,8 +391,8 @@ class TestMeasurement:
|
||||
def test_energy_from_meter_readings_negative_interval(self, measurement_eos):
|
||||
"""Test _energy_from_meter_readings with a negative interval."""
|
||||
key = "load3_mr"
|
||||
start_datetime = datetime(2023, 1, 1, 0)
|
||||
end_datetime = datetime(2023, 1, 1, 5)
|
||||
start_datetime = to_datetime("2023-01-01T00:00:00")
|
||||
end_datetime = to_datetime("2023-01-01T05:00:00")
|
||||
interval = duration(hours=-1)
|
||||
|
||||
with pytest.raises(ValueError, match="interval must be positive"):
|
||||
@@ -368,11 +400,11 @@ class TestMeasurement:
|
||||
|
||||
def test_load_total_kwh(self, measurement_eos):
|
||||
"""Test total load calculation."""
|
||||
start = datetime(2023, 1, 1, 0)
|
||||
end = datetime(2023, 1, 1, 2)
|
||||
start_datetime = to_datetime("2023-01-01T03:00:00")
|
||||
end_datetime = to_datetime("2023-01-01T05:00:00")
|
||||
interval = duration(hours=1)
|
||||
|
||||
result = measurement_eos.load_total_kwh(start_datetime=start, end_datetime=end, interval=interval)
|
||||
result = measurement_eos.load_total_kwh(start_datetime=start_datetime, end_datetime=end_datetime, interval=interval)
|
||||
|
||||
# Expected total load per interval
|
||||
expected = np.array([100, 100]) # Differences between consecutive meter readings
|
||||
@@ -381,20 +413,20 @@ class TestMeasurement:
|
||||
def test_load_total_kwh_no_data(self, measurement_eos):
|
||||
"""Test total load calculation with no data."""
|
||||
measurement_eos.records = []
|
||||
start = datetime(2023, 1, 1, 0)
|
||||
end = datetime(2023, 1, 1, 3)
|
||||
start_datetime = to_datetime("2023-01-01T00:00:00")
|
||||
end_datetime = to_datetime("2023-01-01T03:00:00")
|
||||
interval = duration(hours=1)
|
||||
|
||||
result = measurement_eos.load_total_kwh(start_datetime=start, end_datetime=end, interval=interval)
|
||||
result = measurement_eos.load_total_kwh(start_datetime=start_datetime, end_datetime=end_datetime, interval=interval)
|
||||
expected = np.zeros(3) # No data, so all intervals are zero
|
||||
np.testing.assert_array_equal(result, expected)
|
||||
|
||||
def test_load_total_kwh_partial_intervals(self, measurement_eos):
|
||||
"""Test total load calculation with partial intervals."""
|
||||
start = datetime(2023, 1, 1, 0, 30) # Start in the middle of an interval
|
||||
end = datetime(2023, 1, 1, 1, 30) # End in the middle of another interval
|
||||
start_datetime = to_datetime("2023-01-01T00:30:00") # Start in the middle of an interval
|
||||
end_datetime = to_datetime("2023-01-01T01:30:00") # End in the middle of another interval
|
||||
interval = duration(hours=1)
|
||||
|
||||
result = measurement_eos.load_total_kwh(start_datetime=start, end_datetime=end, interval=interval)
|
||||
result = measurement_eos.load_total_kwh(start_datetime=start_datetime, end_datetime=end_datetime, interval=interval)
|
||||
expected = np.array([100]) # Only one complete interval covered
|
||||
np.testing.assert_array_equal(result, expected)
|
||||
|
||||
Reference in New Issue
Block a user