forked from gisce/heman
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
18,546 additions
and
18 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 |
---|---|---|
@@ -0,0 +1,70 @@ | ||
name: CI | ||
|
||
on: | ||
push: # git push | ||
pull_request: # PR | ||
workflow_dispatch: # manual | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
env: | ||
TRAVIS: 'true' # Skip tests requiring data | ||
MONGO_URI: 'mongodb://localhost:27017/heman' | ||
SENTRY_REDIS_HOST: http://localhost:6379 | ||
#SENTRY_DSN: 'http://localhost:6379' | ||
services: | ||
redis: | ||
image: redis | ||
ports: | ||
- 6379:6379 | ||
#options: --entrypoint redis-server | ||
sentry: | ||
image: sentry | ||
ports: | ||
- 8099:80 | ||
mongo: | ||
image: mongo | ||
ports: | ||
- 27017:27017 | ||
strategy: | ||
matrix: | ||
python-version: | ||
- '2.7' # production | ||
#- '3.8' | ||
#- '3.9' | ||
mongodb-version: | ||
- 2 | ||
redis-version: | ||
- 6 | ||
name: Python ${{ matrix.python-version }} | ||
steps: | ||
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it | ||
- uses: actions/checkout@v2 | ||
- uses: actions/setup-python@v2 | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
- name: Install dependencies | ||
run: | | ||
pip install -r requirements.txt # needed to use empowering and amoniak from git not from pypi | ||
./setup.py develop | ||
- uses: BSFishy/pip-action@v1 | ||
with: | ||
packages: | | ||
coveralls | ||
- name: Unit tests | ||
run: | | ||
pytest | ||
- name: Coveralls | ||
uses: AndreMiras/coveralls-python-action@develop | ||
with: | ||
parallel: true | ||
flag-name: Unit tests | ||
coveralls_finish: | ||
needs: build | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Coveralls Finished | ||
uses: AndreMiras/coveralls-python-action@develop | ||
with: | ||
parallel-finished: true |
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,40 @@ | ||
# This workflow will upload a Python Package using Twine when a release is created | ||
# For more information see: https://help.github.com/en/actions/language-and-framework-guides/using-python-with-github-actions#publishing-to-package-registries | ||
|
||
# This workflow uses actions that are not certified by GitHub. | ||
# They are provided by a third-party and are governed by | ||
# separate terms of service, privacy policy, and support | ||
# documentation. | ||
|
||
name: Upload Python Package | ||
|
||
on: | ||
release: | ||
types: [published] | ||
workflow_dispatch: | ||
push: | ||
tags: | ||
- 'heman-[0-9]+.[0-9]+.[0-9]+' | ||
|
||
jobs: | ||
deploy: | ||
|
||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: Set up Python | ||
uses: actions/setup-python@v2 | ||
with: | ||
python-version: '3.x' | ||
- name: Install dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
pip install build | ||
- name: Build package | ||
run: python -m build | ||
- name: Publish package | ||
uses: pypa/gh-action-pypi-publish@27b31702a0e7fc50959f5ad993c78deac1bdfc29 | ||
with: | ||
user: __token__ | ||
password: ${{ secrets.PYPI_API_TOKEN }} |
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,13 @@ | ||
# Changelog | ||
|
||
## Unreleased | ||
|
||
- New entry point to access precomputations of home | ||
photovoltaic production. | ||
- Upgrade notes: | ||
- Flask should be updated the the latest Flask<2 | ||
- In order to work with current BeeData API server | ||
env BEEDATA_UNAGREED_API should be set to 1 | ||
in the uwsgi configuration. | ||
|
||
|
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,45 @@ | ||
# He-Man | ||
|
||
Uses the Empowering Sword (a.k.a Empowering Proxy API for users). | ||
|
||
He-Man provides an authentified API to access several information regarding Som Energia users stored as objects in monogdb. | ||
|
||
- CCH curves from distribution | ||
- Photovoltaic simulations | ||
- Infoenergia profiles | ||
|
||
## Develoment Deployment | ||
|
||
```bash | ||
# In different consoles run | ||
docker run -it --rm -p 27017:27017 mongo | ||
docker run -it --rm -p 6379:6379 redis | ||
|
||
# then in your main one | ||
./setup.py develop | ||
|
||
# to run the test | ||
pytest | ||
|
||
# to deploy locally | ||
MONGO_URI=mongodb://localhost python run_api.py | ||
|
||
``` | ||
|
||
Updating the | ||
|
||
## Test data | ||
|
||
To obtain test data: | ||
|
||
curl 'https://api.beedataanalytics.com/v1/components?where="contractId"=="XXXXXXX" and "type"=="FV"' | ||
|
||
curl 'https://api.beedataanalytics.com/authn/login' | ||
|
||
|
||
## TODO | ||
|
||
- Discovering tests from heman/ fails since the api is created twice | ||
- Py3: Migrate to sentry-sdk, since raven library raven library is not Py3. | ||
|
||
|
This file was deleted.
Oops, something went wrong.
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,5 +1,5 @@ | ||
try: | ||
VERSION = __import__('pkg_resources') \ | ||
.get_distribution(__name__).version | ||
except Exception, e: | ||
except Exception as e: | ||
VERSION = 'unknown' |
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,195 @@ | ||
import json | ||
import pymongo | ||
import os | ||
from flask import current_app | ||
from flask_restful import request | ||
|
||
from heman.api import AuthorizedResource | ||
from heman.auth import check_contract_allowed | ||
from heman.config import mongo | ||
|
||
|
||
class PVCalculatorResource(AuthorizedResource): | ||
method_decorators = ( | ||
AuthorizedResource.method_decorators + [check_contract_allowed] | ||
) | ||
|
||
def options(self, *args, **kwargs): | ||
return {} | ||
|
||
def get_last_scenario(self, contract_name): | ||
return mongo.db['photovoltaic_reports'].find_one( | ||
{'contractName': contract_name}, | ||
sort=[('id', pymongo.DESCENDING)] | ||
) | ||
|
||
def timeSerie(curve): | ||
if type(curve) == list: | ||
return curve | ||
return curve['values'] | ||
|
||
def parseMongoAzimuth(azimuth, gabledroof): | ||
""" | ||
This function turns mongo representation of azimuth into a int tuple. | ||
Currently, Mongo objects represents azimuths either as a float like 120.0 | ||
or as a string like "120#300" when it is a gabled roof. | ||
The first azimuth is always the one towards south or 90 if W-E orientation. | ||
TODO: change the mongo representation to be a more uniform one. | ||
""" | ||
if not gabledroof: | ||
return (int(azimuth),) | ||
return (int(azimuth), int((azimuth+180) % 360)) | ||
|
||
def parseMongoAzimuthFromSettings(settings): | ||
if os.environ.get("BEEDATA_UNAGREED_API"): | ||
return parseMongoAzimuthFromSettings_unagreedVersion(settings) | ||
|
||
return parseMongoAzimuth( | ||
settings['azimuth'], | ||
settings['gabledroof'] | ||
) | ||
|
||
def parseMongoAzimuthFromSettings_unagreedVersion(settings): | ||
azimuth0 = int(settings['azimuth0']) | ||
try: | ||
azimuth1 = int(settings['azimuth1']) | ||
except ValueError: | ||
return (azimuth0,) | ||
return (azimuth0, azimuth1) | ||
|
||
def parseTilt(settings): | ||
return float(settings['tilt']) | ||
|
||
def parsePower(settings): | ||
return float(settings['power']) | ||
|
||
def queryPeakPower(peakPower): | ||
if not peakPower: return None | ||
return float(peakPower) | ||
|
||
def queryAzimuth(queryAzimuth): | ||
""" | ||
This turns a list of strings representing the azimuths into | ||
a hashable tuple of ints. | ||
""" | ||
return tuple(int(a) for a in queryAzimuth) | ||
|
||
class ScenarioReport(PVCalculatorResource): | ||
|
||
"""Given some parameter values chooses the matching scenario with least payback | ||
""" | ||
|
||
def get(self, contract): | ||
current_app.logger.debug('PVCalculator Report, contract {}'.format(contract)) | ||
|
||
tiltDegrees = float(request.args.get('tilt')) | ||
azimuthDegrees = queryAzimuth(request.args.getlist('azimuth')) | ||
peakPowerKw = queryPeakPower(request.args.get('power')) | ||
|
||
scenario_report = self.get_last_scenario(contract_name=contract) | ||
|
||
if not scenario_report: | ||
return {} | ||
|
||
try: | ||
scenarios = scenario_report['results']['pvAutoSize']['scenarios'] | ||
totalLoad = scenario_report['results']['pvAutoSize']['load']['total'] | ||
except KeyError as e: | ||
print("Error {}", e) | ||
return {} | ||
except IndexError as e: | ||
print("Error {}", e) | ||
return {} | ||
|
||
selectedScenarios = [ | ||
scenario | ||
for i,scenario in enumerate(scenarios) | ||
if parseTilt(scenario['settings']) == tiltDegrees | ||
and parseMongoAzimuthFromSettings(scenario['settings']) == azimuthDegrees | ||
and (not peakPowerKw or parsePower(scenario['settings']) == peakPowerKw) | ||
] | ||
if not selectedScenarios: | ||
return dict( | ||
error='NOT_FOUND', | ||
message='Scenario not found', | ||
) | ||
|
||
bestScenario = min( | ||
selectedScenarios, | ||
key=lambda s: s['economics']['payback'], | ||
) | ||
|
||
result = dict( | ||
loadKwhYear = totalLoad, | ||
loadByPeriodKwh = scenario_report['results']['pvAutoSize']['load']['timeslots'], | ||
productionKwhYear = bestScenario['generation']['total'], | ||
productionToLoadKwhYear = bestScenario['generation']['PVtoLoad'], | ||
productionToLoadEuroYear = bestScenario['generation']['PVtoLoadCost'], | ||
productionToLoadPercent = bestScenario['generation']['PVtoLoadPct'], | ||
productionToGridKwhYear = bestScenario['generation']['PVtoGrid'], | ||
productionToGridEuroYear = bestScenario['generation']['PVtoGridCost'], | ||
productionToGridPercent = bestScenario['generation']['PVtoGridPct'], | ||
loadFromGridKwhYear = bestScenario['generation']['loadFromGrid'], | ||
savingsEuroYear = bestScenario['generation']['savings'], | ||
paybackYears = bestScenario['economics']['payback'], | ||
installationCostEuro = bestScenario['settings']['cost'], | ||
azimuthDegrees= parseMongoAzimuthFromSettings(bestScenario['settings']), | ||
tiltDegrees= parseTilt(bestScenario['settings']), | ||
areaM2 = bestScenario['settings']['area'], | ||
nModules = bestScenario['settings']['numModules'], | ||
peakPowerKw = parsePower(bestScenario['settings']), | ||
dailyLoadProfileKwh = timeSerie(scenario_report['results']['pvAutoSize']['load']['profile']), | ||
dailyProductionProfileKwh = timeSerie(bestScenario['generation']['profile']), | ||
monthlyProductionToLoadKwh = timeSerie(bestScenario['generation']['monthlyPVtoLoad']), | ||
monthlyProductionToLoadEuro = timeSerie(bestScenario['generation']['monthlyPVtoLoadCost']), | ||
monthlyGridToLoadKwh = timeSerie(bestScenario['generation']['monthlyLoadFromGrid']), | ||
monthlyGridToLoadEuro = timeSerie(bestScenario['generation']['monthlyLoadFromGridCost']), | ||
monthlyProductionToGridKwh = timeSerie(bestScenario['generation']['monthlyPVtoGrid']), | ||
monthlyProductionToGridEuro = timeSerie(bestScenario['generation']['monthlyPVtoGridCost']), | ||
monthlyProductionKwh = timeSerie(bestScenario['generation']['monthlyPV']), | ||
#monthlyProductionEuro = bestScenario['generation']['monthlyPVCost'], # TODO: Info not yet available | ||
) | ||
|
||
return result | ||
|
||
|
||
class ScenarioParams(PVCalculatorResource): | ||
|
||
"""Returns the parameters available values to choose scenario""" | ||
|
||
def get(self, contract): | ||
|
||
scenario_report = self.get_last_scenario(contract_name=contract) | ||
if not scenario_report: | ||
return {} | ||
|
||
try: | ||
scenarios = scenario_report['results']['pvAutoSize']['scenarios'] | ||
except KeyError as e: | ||
print("Error {}", e) | ||
return {} | ||
except IndexError as e: | ||
print("Error {}", e) | ||
return {} | ||
|
||
tilts, azimuths, powers = zip(*[ | ||
( | ||
parseTilt(scenario['settings']), | ||
parseMongoAzimuthFromSettings(scenario['settings']), | ||
scenario['settings']['power'], | ||
) | ||
for i,scenario in enumerate(scenarios) | ||
]) | ||
result = dict( | ||
tilt = list(sorted(set(tilts))), | ||
azimuth = list(sorted(set(azimuths))), | ||
power = list(sorted(set(powers))), | ||
) | ||
return result | ||
|
||
|
||
resources = [ | ||
(ScenarioReport, '/ScenarioReport/<contract>'), | ||
(ScenarioParams, '/ScenarioParams/<contract>'), | ||
|
||
] |
Oops, something went wrong.