From ecd71fcd8b4851eaefdeae7d7259581bcbd38e31 Mon Sep 17 00:00:00 2001 From: peterdudfield Date: Tue, 3 Oct 2023 17:53:18 +0100 Subject: [PATCH] add option of limiting days back in --- src/database.py | 2 +- src/utils.py | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/database.py b/src/database.py index 9d1664c..866778a 100644 --- a/src/database.py +++ b/src/database.py @@ -206,7 +206,7 @@ def get_latest_forecast_values_for_a_specific_gsp_from_database( :return: list of latest forecat values """ - start_datetime = get_start_datetime(start_datetime=start_datetime_utc) + start_datetime = get_start_datetime(start_datetime=start_datetime_utc, days=365) if (forecast_horizon_minutes is None) and (creation_utc_limit is None): forecast_values = get_forecast_values_latest( diff --git a/src/utils.py b/src/utils.py index 3a1d6d6..a633574 100644 --- a/src/utils.py +++ b/src/utils.py @@ -76,7 +76,9 @@ def format_datetime(datetime_str: str = None): def get_start_datetime( - n_history_days: Optional[Union[str, int]] = None, start_datetime: Optional[datetime] = None + n_history_days: Optional[Union[str, int]] = None, + start_datetime: Optional[datetime] = None, + days: Optional[int] = 3, ) -> datetime: """ Get the start datetime for the query @@ -88,12 +90,13 @@ def get_start_datetime( :param start_datetime: optional start datetime for the query. If not set, after now, or set to over three days ago defaults to N_HISTORY_DAYS env var, which defaults to yesterday. + :param days: number of days limit the data by :return: start datetime """ now = datetime.now(tz=utc) - if start_datetime is None: + if start_datetime is None or now - start_datetime > timedelta(days=days): if n_history_days is None: n_history_days = os.getenv("N_HISTORY_DAYS", "yesterday")