-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Write forecast and forecast values (#97)
* Added insert_forecast_values function * Added fixtures and tests for insert_forecast_values function * Added assertions for expected input keys for forecast write test
- Loading branch information
1 parent
decdceb
commit 1226dfe
Showing
7 changed files
with
201 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
""" | ||
Functions for writing to the PVSite database | ||
""" | ||
|
||
from .forecast import insert_forecast_values | ||
from .generation import insert_generation_values | ||
from .user_and_site import ( | ||
add_site_to_site_group, | ||
change_user_site_group, | ||
create_site, | ||
create_site_group, | ||
create_user, | ||
delete_site, | ||
delete_site_group, | ||
delete_user, | ||
make_fake_site, | ||
update_user_site_group, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
""" | ||
Write helpers for the Forecast and ForecastValues table. | ||
""" | ||
|
||
import logging | ||
|
||
import pandas as pd | ||
from sqlalchemy.orm import Session | ||
|
||
from pvsite_datamodel.sqlmodels import ForecastSQL, ForecastValueSQL | ||
|
||
_log = logging.getLogger(__name__) | ||
|
||
|
||
def insert_forecast_values( | ||
session: Session, | ||
forecast_meta: dict, | ||
forecast_values_df: pd.DataFrame, | ||
): | ||
"""Insert a dataframe of forecast values and forecast meta info into the database. | ||
:param session: sqlalchemy session for interacting with the database | ||
:param forecast_meta: Meta info about the forecast values | ||
:param forecast_values_df: dataframe with the data to insert | ||
""" | ||
|
||
forecast = ForecastSQL(**forecast_meta) | ||
session.add(forecast) | ||
|
||
# Flush to get the Forecast's primary key. | ||
session.flush() | ||
|
||
rows = forecast_values_df.to_dict("records") | ||
session.bulk_save_objects( | ||
[ | ||
ForecastValueSQL( | ||
**row, | ||
forecast_uuid=forecast.forecast_uuid, | ||
) | ||
for row in rows | ||
] | ||
) | ||
session.commit() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
""" | ||
Useful functions for write operations. | ||
""" | ||
|
||
from sqlalchemy.dialects import postgresql | ||
from sqlalchemy.orm import Session | ||
|
||
from pvsite_datamodel.sqlmodels import Base | ||
|
||
|
||
def _insert_do_nothing_on_conflict(session: Session, table: Base, rows: list[dict]): | ||
"""Upserts rows into table. | ||
This functions checks the primary keys and constraints, and if present, does nothing | ||
:param session: sqlalchemy Session | ||
:param table: the table | ||
:param rows: the rows we are going to update | ||
""" | ||
stmt = postgresql.insert(table.__table__) | ||
stmt = stmt.on_conflict_do_nothing() | ||
session.execute(stmt, rows) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters