Skip to content

Commit

Permalink
Add ScenarioParams
Browse files Browse the repository at this point in the history
  • Loading branch information
JoanaFigueira committed Nov 10, 2021
1 parent 98d0598 commit 5e5ab75
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
36 changes: 35 additions & 1 deletion heman/api/pvcalculator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def get(self, contract):
error='NOT_FOUND',
message='Scenario not found',
)),
mimetype='application/json'
mimetype='application/json',
)

bestScenario = min(
Expand All @@ -82,7 +82,41 @@ def get(self, contract):
return Response(json.dumps(result), mimetype='application/json')


class ScenarioParams(PVCalculatorResource):

def get(self, contract):

scenario_report = self.get_last_scenario(contract_name=contract)
if not scenario_report:
return Response({}, mimetype='application/json')

try:
scenarios = scenario_report['results']['pvAutoSize']['scenarios']
except KeyError as e:
print("Error {}", e)
return Response({}, mimetype='application/json')
except IndexError as e:
print("Error {}", e)
return Response({}, mimetype='application/json')

tilts, azimuths, powers = zip(*[
(
scenario['settings']['tilt'],
scenario['settings']['azimuth'],
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 Response(json.dumps(result), mimetype='application/json')


resources = [
(ScenarioReport, '/ScenarioReport/<contract>'),
(ScenarioParams, '/ScenarioParams/<contract>'),

]
24 changes: 24 additions & 0 deletions heman/api/pvcalculator/pvcalculator_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import unicode_literals
import pymongo
import unittest
import mock
Expand Down Expand Up @@ -122,3 +123,26 @@ def test__scenario_report__parameter_value_not_found(api, scenario_data):
'message': "Scenario not found",
}

def test__scenario_params(api, scenario_data):
contract, token = scenario_data
r = api.get('/api/ScenarioParams/{}'.format(contract),
headers=dict(
Authorization = 'token {}'.format(token)
),
)

assert r.get_json() == {
'tilt': [15.0, 30.0],
'azimuth': [100.0, 140.0, 180.0, '100#280', '140#320', '180#0'],
'power': [
'10.640 kWp',
'2.280 kWp',
'3.040 kWp',
'4.560 kWp',
'5.320 kWp',
'6.080 kWp',
'7.600 kWp',
'8.360 kWp',
'9.120 kWp',
],
}

0 comments on commit 5e5ab75

Please sign in to comment.