-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: raise error if generation post>A x capacity (#102)
* feat: raise error if generation post>A x capacity * add test to check test_post_site_generation_exceding_max_capacity * load value of capacity_factor from env var
- Loading branch information
1 parent
917296f
commit 0b6f403
Showing
2 changed files
with
39 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
"""India DB client that conforms to the DatabaseInterface.""" | ||
import os | ||
import datetime as dt | ||
import pandas as pd | ||
import logging | ||
from typing import Optional | ||
from fastapi import HTTPException | ||
from uuid import UUID | ||
import sentry_sdk | ||
|
||
from pvsite_datamodel import DatabaseConnection | ||
from pvsite_datamodel.read import ( | ||
|
@@ -370,6 +372,31 @@ def post_site_generation( | |
) | ||
|
||
generation_values_df = pd.DataFrame(generations) | ||
capacity_factor = float(os.getenv("ERROR_GENERATION_CAPACITY_FACTOR", 1.1)) | ||
site = get_site_by_uuid(session=session, site_uuid=site_uuid) | ||
site_capacity_kw = site.capacity_kw | ||
exceeded_capacity = generation_values_df[ | ||
generation_values_df["power_kw"] > site_capacity_kw * capacity_factor | ||
] | ||
if len(exceeded_capacity) > 0: | ||
# alert Sentry and return 422 validation error | ||
sentry_sdk.capture_message( | ||
f"Error processing generation values. " | ||
f"One (or more) values are larger than {capacity_factor} " | ||
f"times the site capacity of {site_capacity_kw} kWp. " | ||
# f"User: {auth['https://openclimatefix.org/email']}" | ||
f"Site: {site_uuid}" | ||
) | ||
raise HTTPException( | ||
status_code=422, | ||
detail=( | ||
f"Error processing generation values. " | ||
f"One (or more) values are larger than {capacity_factor} " | ||
f"times the site capacity of {site_capacity_kw} kWp. " | ||
"Please adjust this generation value, the site capacity, " | ||
"or contact [email protected]." | ||
), | ||
) | ||
|
||
insert_generation_values(session, generation_values_df) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import logging | ||
from fastapi import HTTPException | ||
import pytest | ||
|
||
from india_api.internal import PredictedPower, ActualPower | ||
|
@@ -105,3 +106,14 @@ def test_post_site_generation(self, client, sites) -> None: | |
generation=[ActualPower(Time=1, PowerKW=1)], | ||
email="[email protected]", | ||
) | ||
|
||
def test_post_site_generation_exceding_max_capacity(self, client, sites): | ||
try: | ||
client.post_site_generation( | ||
site_uuid=sites[0].site_uuid, | ||
generation=[ActualPower(Time=1, PowerKW=1000)], | ||
email="[email protected]", | ||
) | ||
except HTTPException as e: | ||
assert e.status_code == 422 | ||
assert "generation values" in str(e.detail) |