def update_kpis(energy_system, etm): """ TODO """ for kpi in energy_system.es.instance[0].area.KPIs.kpi: prop = kpis.gqueries[kpi.id] list_of_gqueries = [gquery['gquery'] for gquery in prop['gqueries']] query_result = QueryScenario(etm.environment, etm.scenario_id)(*list_of_gqueries) if not query_result.successful: raise ValueError(query_result.errors) metrics = query_result.value if prop['esdl_type'] == 'DistributionKPI': for it in kpi.distribution.stringItem: kpi.distribution.stringItem.remove(it) for gquery in prop['gqueries']: val = metrics[gquery['gquery']]['future'] * prop['factor'] if val != 0: kpi.distribution.stringItem.append( energy_system.esdl.StringItem(label=gquery['label'], value=val)) else: kpi.value = metrics[prop['gqueries'][0] ['gquery']]['future'] * prop['factor']
def add_kpis(energy_system, etm): """ TODO """ energy_system.add_kpis() for kpi_id, prop in kpis.gqueries.items(): list_of_gqueries = [gquery['gquery'] for gquery in prop['gqueries']] query_result = QueryScenario(etm.environment, etm.scenario_id)(*list_of_gqueries) if not query_result.successful: raise ValueError(query_result.errors) metrics = query_result.value kpi = energy_system.create_kpi( prop['esdl_type'], kpi_id, prop['name'], energy_system.get_by_id_slow(prop['q_and_u'])) if prop['esdl_type'] == 'DistributionKPI': kpi.distribution = energy_system.esdl.StringLabelDistribution() for gquery in prop['gqueries']: val = metrics[gquery['gquery']]['future'] * prop['factor'] if val != 0: kpi.distribution.stringItem.append( energy_system.esdl.StringItem(label=gquery['label'], value=val)) else: kpi.value = metrics[prop['gqueries'][0] ['gquery']]['future'] * prop['factor'] print(f'Adding KPI of type: {type(kpi)}') energy_system.add_kpi(kpi)
def get_context_values(scenario_id, inputs, queries): ''' Gets all the requested inputs and queries from the ETM scenario May use two calls to achive this, as the queries and default inputs have different endpoints. scenario_id (int) - The ETM scenario ID inputs (list) - The ETM inputs (sliders) where future and present values are requested from queries (list) - ETM gqueries where future and present values are requested from Returns a dict with the inputs and queries as keys, with ETM future and present values in a nested dict ''' values = {} if inputs: raw_result = InputsScenario.execute(scenario_id, *inputs) values = process(raw_result) if raw_result.successful else fail_with(raw_result) # We always need to access this because we need area and end year # (TODO: we can also get that from inputs) query_result = QueryScenario.execute(scenario_id, detailed=True, *queries) if not query_result.successful: return fail_with(raw_result) values.update(query_result.value) return values
def get_metrics(self, *gqueries): '''Requests the metrics for the gqueries from the ETM''' query_result = QueryScenario.execute(self.scenario_id, *gqueries) if query_result.successful: return query_result.value raise ETMParseError.with_humanized_message(query_result.errors)
def test_call_with_etengine_failing(app, requests_mock): requests_mock.put(f'{app.config["ETENGINE"]["beta"]}/scenarios/12345', status_code=500) with app.app_context(): service = QueryScenario('beta', 12345) result = service('gquery1') assert not result.successful assert 'ETEngine returned a 500' in result.errors
def test_with_non_existing_scenario_id(app, requests_mock): requests_mock.put(f'{app.config["ETENGINE"]["beta"]}/scenarios/12345', json={'errors': ['Scenario not found']}, status_code=404) with app.app_context(): service = QueryScenario('beta', 12345) result = service('gquery1') assert not result.successful assert 'Scenario not found' in result.errors
def test_call_with_invalid_queries(app, requests_mock): requests_mock.put(f'{app.config["ETENGINE"]["beta"]}/scenarios/12345', json={'errors': ['Unkown gquery: gquery1']}, status_code=422) with app.app_context(): service = QueryScenario('beta', 12345) result = service(['gquery1']) assert not result.successful assert 'Unkown gquery: gquery1' in result.errors
def query_scenario(self, scenario_id, prop): """ TODO """ query_result = QueryScenario.execute(scenario_id, prop['gquery']) if query_result.successful: return query_result.value[ prop['gquery']]['future'] / prop['factor'] raise ETMParseError( f"We currently do not support the ETM gquery listed in the config: {prop['gquery']}" )
def query_scenario(self, etm, prop): """ TODO """ query_result = QueryScenario(etm.environment, etm.scenario_id)(prop['gquery']) if query_result.successful: return query_result.value[ prop['gquery']]['future'] / prop['factor'] raise EnergysystemParseError( f"We currently do not support the ETM gquery listed in the config: {prop['gquery']}" )
def test_call_with_valid_queries(app, requests_mock): queries = ['query1', 'query2'] requests_mock.put( f'{app.config["ETENGINE_URL"]}/scenarios/12345', json={ 'gqueries': { queries[0]: {'future': 1, 'present': 0.5}, queries[1]: {'future': 0.5, 'present': 1} } }, status_code=200 ) with app.app_context(): result = QueryScenario.execute(12345, queries) assert result.successful assert len(result.value) == 2 assert all([key in queries for key in result.value.keys()])
def test_call_with_detailed(app, requests_mock): queries = ['query1', 'query2'] requests_mock.put( f'{app.config["ETENGINE_URL"]}/scenarios/12345', json={ 'gqueries': { queries[0]: {'future': 1, 'present': 0.5}, queries[1]: {'future': 0.5, 'present': 1} }, 'scenario': { 'id' :12345, 'area_code': 'GM001_Faketown', 'end_year': 2050 } }, status_code=200 ) with app.app_context(): result = QueryScenario.execute(12345, detailed=True, *queries) assert result.successful assert 'end_year' in result.value assert queries[0] in result.value
def test_instance(app): # We need the context to access the config variables with app.app_context(): service = QueryScenario('beta', 12345) assert isinstance(service, EtengineService)