Skip to content

Commit

Permalink
add created_by filter to latest_forecast_value (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterdudfield authored Mar 7, 2024
1 parent 57f48fa commit e3174fa
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
16 changes: 11 additions & 5 deletions pvsite_datamodel/read/latest_forecast_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def get_latest_forecast_values_by_site(
site_uuids: list[uuid.UUID],
start_utc: dt.datetime,
sum_by: Optional[str] = None,
created_by: Optional[dt.datetime] = None,
) -> Union[dict[uuid.UUID, list[ForecastValueSQL]], List[ForecastValueSum]]:
"""Get the forecast values by input sites, get the latest value.
Expand All @@ -40,6 +41,7 @@ def get_latest_forecast_values_by_site(
:param session: The sqlalchemy database session
:param site_uuids: list of site_uuids for which to fetch latest forecast values
:param start_utc: filters on forecast values target_time >= start_utc
:param created_by: filter on forecast values created time <= created_by
:param sum_by: optional, sum the forecast values by this column
"""

Expand All @@ -57,11 +59,15 @@ def get_latest_forecast_values_by_site(
ForecastValueSQL.start_utc >= start_utc,
ForecastSQL.site_uuid.in_(site_uuids),
)
.order_by(
ForecastSQL.site_uuid,
ForecastValueSQL.start_utc,
ForecastSQL.timestamp_utc.desc(),
)
)

if created_by is not None:
query = query.filter(ForecastValueSQL.created_utc <= created_by)

query = query.order_by(
ForecastSQL.site_uuid,
ForecastValueSQL.start_utc,
ForecastSQL.timestamp_utc.desc(),
)

if sum_by is None:
Expand Down
9 changes: 9 additions & 0 deletions tests/test_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,15 @@ def test_get_latest_forecast_values(db_session, sites):

assert values_as_tuple == expected[site_uuid]

latest_forecast = get_latest_forecast_values_by_site(
session=db_session,
site_uuids=site_uuids,
start_utc=d1,
sum_by="total",
created_by=dt.datetime.now() - dt.timedelta(hours=3),
)
assert len(latest_forecast) == 0

latest_forecast = get_latest_forecast_values_by_site(
session=db_session, site_uuids=site_uuids, start_utc=d1, sum_by="total"
)
Expand Down

0 comments on commit e3174fa

Please sign in to comment.