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

feat: validate generation values against site capacity #195

Merged
merged 8 commits into from
Oct 4, 2024

Conversation

anxkhn
Copy link
Contributor

@anxkhn anxkhn commented Sep 28, 2024

Pull Request

Description

  • Implemented logic to compare uploaded PV generation values against the site's capacity.
  • Raised HTTP 102 error if any generation value exceeds the site's capacity, with a custom error message informing the user.
  • Added site capacity check in the /sites/{site_uuid}/pv_actual route.

Fixes #191

How Has This Been Tested?

  • Yes

If your changes affect data processing, have you plotted any changes? i.e. have you done a quick sanity check?

  • Yes

Checklist:

  • My code follows OCF's coding style guidelines
  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works
  • I have checked my code and corrected any misspellings

@anxkhn
Copy link
Contributor Author

anxkhn commented Sep 29, 2024

Hey @peterdudfield !
Here's my PR. This is my first time contributing to github.com/openclimatefix/pv-site-api
Can you help set it up properly locally, with pv-site-datamodel so I can check API and do tests locally too?

Thanks!

pv_site_api/main.py Outdated Show resolved Hide resolved
Copy link
Contributor

@peterdudfield peterdudfield left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks so much for this @anxkhn, really appreviate you doing this.

Ive suggested two fairly small changes, would you be able to do them?

Thanks again

@peterdudfield
Copy link
Contributor

Hey @peterdudfield ! Here's my PR. This is my first time contributing to github.com/openclimatefix/pv-site-api Can you help set it up properly locally, with pv-site-datamodel so I can check API and do tests locally too?

Thanks!

Hey. The PR looks good.

  1. You should be able to run tests locally with poetry run pytest tests, see readme
  2. For running locally you can use poetry run uvicorn pv_site_api.main:app --reload. You can set export FAKE=1 to make it run wiht fake data not real database.
    let me know how you get on

@anxkhn
Copy link
Contributor Author

anxkhn commented Oct 1, 2024

Hey @peterdudfield
I have updated as requested.
Are there any more requirements?

@peterdudfield
Copy link
Contributor

Sorry, we've been chatting on #191 .

Would you be able to implement that. Its basically,

  1. If all power is below capacity, return 200 as normal
  2. if any power value is above the capacity, but less than 2* the capacity, then return 200, but add warning in the Http response
  3. if any power is above 2* the capacity, then return 400 with error message.

Sorry if this issue changed a bit while you were developing it

@anxkhn
Copy link
Contributor Author

anxkhn commented Oct 2, 2024

I would like to know a few things:

  • Return in what format? Log it or return something like :
JSONResponse(
            status_code=200,
            content={"message": "Generation values processed successfully", "warning": warning_message}
        )
  • Status code 422 right?

@peterdudfield
Copy link
Contributor

Yes status code 422 - thanks - https://fastapi.tiangolo.com/tutorial/handling-errors/#import-httpexception

For the values less than the capacity, the route can just be returned as normal.

For values between 1 and 2 * the capacity, yes, the above method looks a good way to do it

@peterdudfield
Copy link
Contributor

hi @anxkhn

Just had some internal chats.

  1. Could we change the threshold to 1.1*capacity to cause the 422.

  2. Could this 1.1 be a environment variable, so we can change it really easily. The default should be 1.1.
    error_generation_capacity_factor = float(os.getenv('ERROR_GENERTAION_CAPACITY_FACTOR', 1.1))

The error should say something like

Error processing generation values. One (or more) values are larger than {error_generation_capacity_factor} times the site capacity of {capacity} kWp. Please adjust this generation value, the site capacity, or contact [email protected]."
  1. Below 1.1, don't worry about the warning message. We now think people wont look at this.

Sorry its all changing, really appreciate your help on this

@anxkhn
Copy link
Contributor Author

anxkhn commented Oct 3, 2024

@peterdudfield I have ignored the warning message for now.
Rest code and test is updated and tested to handle 1.1 capacity_factor.

Please review the PR

pv_site_api/main.py Outdated Show resolved Hide resolved
@peterdudfield
Copy link
Contributor

@peterdudfield I have ignored the warning message for now. Rest code and test is updated and tested to handle 1.1 capacity_factor.

Please review the PR

Looks great, and thank you so much for doing this so quickly. Two small comments and I think itll then be ready to go.
Thank you

@anxkhn
Copy link
Contributor Author

anxkhn commented Oct 3, 2024

@peterdudfield Hi.
Review the changes and let me know if any more is needed. Thanks!

.python-version Outdated Show resolved Hide resolved
poetry.lock Outdated Show resolved Hide resolved
Copy link

codecov bot commented Oct 4, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.58%. Comparing base (fbf67a9) to head (845133b).
Report is 104 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #195   +/-   ##
=======================================
  Coverage   95.57%   95.58%           
=======================================
  Files          11       11           
  Lines         656      725   +69     
=======================================
+ Hits          627      693   +66     
- Misses         29       32    +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

pv_site_api/main.py Outdated Show resolved Hide resolved
@anxkhn
Copy link
Contributor Author

anxkhn commented Oct 4, 2024

Hi @peterdudfield
I have made the requested changes.

@peterdudfield
Copy link
Contributor

Just one comment about .python-version file, could you remove that?

@anxkhn
Copy link
Contributor Author

anxkhn commented Oct 4, 2024

@peterdudfield Done.

@peterdudfield
Copy link
Contributor

Thanks so much @anxkhn

@peterdudfield peterdudfield merged commit 0da624f into openclimatefix:main Oct 4, 2024
@peterdudfield
Copy link
Contributor

@all-contributors please add @anxkhn for code

Copy link
Contributor

@peterdudfield

I've put up a pull request to add @anxkhn! 🎉

@peterdudfield peterdudfield mentioned this pull request Oct 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Warning" If user uploads generation data > capacity of syste,
2 participants