diff --git a/src/database.py b/src/database.py index d57f24a..fe73eb2 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( @@ -218,6 +218,17 @@ def get_latest_forecast_values_for_a_specific_gsp_from_database( ) else: + if creation_utc_limit is not None and creation_utc_limit < datetime.now( + tz=timezone.utc + ) - timedelta(days=7): + model = ForecastValueSQL + elif start_datetime_utc is not None and start_datetime_utc < datetime.now( + tz=timezone.utc + ) - timedelta(days=7): + model = ForecastValueSQL + else: + model = ForecastValueSevenDaysSQL + forecast_values = get_forecast_values( session=session, gsp_id=gsp_id, @@ -225,7 +236,7 @@ def get_latest_forecast_values_for_a_specific_gsp_from_database( end_datetime=end_datetime_utc, forecast_horizon_minutes=forecast_horizon_minutes, model_name="blend", - model=ForecastValueSevenDaysSQL, + model=model, only_return_latest=True, created_utc_limit=creation_utc_limit, ) diff --git a/src/gsp.py b/src/gsp.py index 663cc9c..298baff 100644 --- a/src/gsp.py +++ b/src/gsp.py @@ -172,6 +172,7 @@ def get_forecasts_for_a_specific_gsp( start_datetime_utc = format_datetime(start_datetime_utc) end_datetime_utc = format_datetime(end_datetime_utc) + creation_limit_utc = format_datetime(creation_limit_utc) if gsp_id > GSP_TOTAL: return Response(None, status.HTTP_204_NO_CONTENT) diff --git a/src/utils.py b/src/utils.py index 409045f..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 or now - start_datetime > timedelta(days=3): + 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")