Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue/national creation utc limit #302

Merged
merged 11 commits into from
Sep 27, 2023
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ uvicorn[standard]
pydantic
numpy
requests
nowcasting_datamodel==1.5.15
nowcasting_datamodel==1.5.17
nowcasting_dataset==3.7.12
sqlalchemy
psycopg2-binary
Expand Down
1 change: 1 addition & 0 deletions src/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ def get_latest_forecast_values_for_a_specific_gsp_from_database(
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,
Expand Down
25 changes: 20 additions & 5 deletions src/national.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
get_truth_values_for_a_specific_gsp_from_database,
)
from pydantic_models import NationalForecast, NationalForecastValue, NationalYield
from utils import format_datetime, format_plevels
from utils import filter_forecast_values, format_datetime, format_plevels

logger = structlog.stdlib.get_logger()

Expand Down Expand Up @@ -78,20 +78,35 @@ def get_national_forecast(
detail="Can not set forecast_horizon_minutes when including metadata",
)

if creation_limit_utc is None:
historic = True
else:
historic = False

forecast = get_latest_forecast_for_gsps(
session=session,
gsp_ids=[0],
model_name="blend",
historic=True,
historic=historic,
preload_children=True,
start_target_time=start_datetime_utc,
end_target_time=end_datetime_utc,
start_created_utc=creation_limit_utc,
end_created_utc=creation_limit_utc,
)
forecast = forecast[0]

forecast = NationalForecast.from_orm_latest(forecast)
forecast_values = forecast.forecast_values
if historic:
forecast = NationalForecast.from_orm_latest(forecast)
else:
forecast = NationalForecast.from_orm(forecast)

forecasts = filter_forecast_values(
forecasts=[forecast],
start_datetime_utc=start_datetime_utc,
end_datetime_utc=end_datetime_utc,
)
forecast_values = forecasts[0].forecast_values

else:
forecast_values = get_latest_forecast_values_for_a_specific_gsp_from_database(
session=session,
Expand Down
32 changes: 32 additions & 0 deletions src/tests/test_national.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,38 @@ def test_get_national_forecast(db_session, api_client):
)


def test_read_latest_national_values_start_and_end_filters_inculde_metadata(db_session, api_client):
"""Check main solar/GB/national/forecast route works"""

with freeze_time("2023-01-01"):
model = get_model(db_session, name="blend", version="0.0.1")

forecast = make_fake_national_forecast(
session=db_session, t0_datetime_utc=datetime.now(tz=timezone.utc)
)
forecast.model = model
db_session.add(forecast)
update_all_forecast_latest(forecasts=[forecast], session=db_session)

app.dependency_overrides[get_session] = lambda: db_session

response = api_client.get(
"/v0/solar/GB/national/forecast?start_datetime_utc=2023-01-01&include_metadata=true"
) # noqa
assert response.status_code == 200

national_forecast = NationalForecast(**response.json())
assert len(national_forecast.forecast_values) == 16

response = api_client.get(
"/v0/solar/GB/national/forecast?start_datetime_utc=2023-01-01&end_datetime_utc=2023-01-01 04:00&include_metadata=true" # noqa
)
assert response.status_code == 200

national_forecast = NationalForecast(**response.json())
assert len(national_forecast.forecast_values) == 9


def test_get_national_forecast_error(db_session, api_client):
"""Check main solar/GB/national/forecast route works"""

Expand Down
Loading