Skip to content

Commit

Permalink
Add minimum historic prediction hours for prediction providers.
Browse files Browse the repository at this point in the history
Prediction providers can now individually set the minimum historic prediction hours
to be kept in the data records. This setting supersedes any lower setting given by the
configuration `prediction_historic_hours`.

As a help for the future change of ElecPriceAkkudoktor, the minimum historic hours for
this provider is set to 5 * 7 * 24 hours.

Signed-off-by: Bobby Noelte <[email protected]>
  • Loading branch information
b0661 committed Jan 6, 2025
1 parent 10acc70 commit 57d31f3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/akkudoktoreos/prediction/elecpriceakkudoktor.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ def _validate_data(cls, json_str: Union[bytes, Any]) -> AkkudoktorElecPrice:
raise ValueError(error_msg)
return akkudoktor_data

def historic_hours_min(self) -> int:
"""Return the minimum historic prediction hours for Akkudoktor electricity price data."""
return 5 * 7 * 24 # 5 weeks a 7 days a 24 hours

def _calculate_weighted_mean(self, day_of_week: int, hour: int) -> float:
"""Calculate the weighted mean price for given day_of_week and hour.
Expand Down
20 changes: 15 additions & 5 deletions src/akkudoktoreos/prediction/predictionabc.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ class PredictionStartEndKeepMixin(PredictionBase):
system. Predictions cannot be computed if this value is `None`.
"""

def historic_hours_min(self) -> int:
"""Return the minimum historic prediction hours for specific data.
To be implemented by derived classes if default 0 is not appropriate.
"""
return 0

# Computed field for end_datetime and keep_datetime
@computed_field # type: ignore[prop-decorator]
@property
Expand Down Expand Up @@ -136,11 +143,14 @@ def keep_datetime(self) -> Optional[DateTime]:
Returns:
Optional[DateTime]: The calculated retention cutoff datetime, or `None` if inputs are missing.
"""
if self.start_datetime and self.config.prediction_historic_hours:
return self.start_datetime - to_duration(
f"{int(self.config.prediction_historic_hours)} hours"
)
return None
if self.start_datetime is None:
return None
historic_hours = self.historic_hours_min()
if self.config.prediction_historic_hours and self.config.prediction_historic_hours > historic_hours:
historic_hours = int(self.config.prediction_historic_hours)
return self.start_datetime - to_duration(
f"{historic_hours} hours"
)

@computed_field # type: ignore[prop-decorator]
@property
Expand Down

0 comments on commit 57d31f3

Please sign in to comment.