Skip to content

Commit

Permalink
Add national history (#304)
Browse files Browse the repository at this point in the history
* add optional to load more than 3 days of national forecasts

* add option of limiting days back in

* fix

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
peterdudfield and pre-commit-ci[bot] authored Oct 4, 2023
1 parent e0dd043 commit fb828cd
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
15 changes: 13 additions & 2 deletions src/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -218,14 +218,25 @@ 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,
start_datetime=start_datetime,
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,
)
Expand Down
1 change: 1 addition & 0 deletions src/gsp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 5 additions & 2 deletions src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")

Expand Down

0 comments on commit fb828cd

Please sign in to comment.