diff --git a/app.py b/app.py index 111f00fd3805393629c67eec8463ad65727bf4dc..b34263512b4ad67db2ad4e46c026d86ad8a2565f 100644 --- a/app.py +++ b/app.py @@ -222,7 +222,6 @@ def planner_plan_detail(): @app.get("/database/store/kpi") def database_store_kpi(): """ - It stores the Kpis that have been selected. The json provided should have the same format that json with the kpi """ #if request.json: @@ -231,6 +230,17 @@ def database_store_kpi(): else: return constants.ERROR_JSON_NEEDED +@app.get("/database/store/kpi_relative") +def database_store_kpi_relative(): + """ + The json provided should have the same format that json with the kpi + """ + #if request.json: + if request: + return database.database_store_kpi(cnx, request, True) + else: + return constants.ERROR_JSON_NEEDED + # MAIN: ---------------------------------------------------------------------------------------------------------------- if __name__ == "__main__": model = pickle.load(open('model.pkcls', 'rb')) diff --git a/extra_data/kpis_relative.json b/extra_data/kpis_relative.json new file mode 100644 index 0000000000000000000000000000000000000000..c6cc24775e377a89f4da45262f6ae326d92ab96a --- /dev/null +++ b/extra_data/kpis_relative.json @@ -0,0 +1,34 @@ +{ + "simulation_id": 2, + "bilbao": { + "cityWide": { + "traffic": { + "pedestrianTravelTime": 2420.339269937224, + "dailyInternalBikeTravels": 1801 + }, + "pollution": { + "CO": 93208.2575225834, + "CO2_TOTAL": 3235293.3925066995, + "HC": 13157.842323619561, + "NOx": 5239.199220005189, + "PM": 77.20151943494339, + "CO2_rep": 2747710.252532586 + } + }, + "local": { + "traffic": { + "pedestrianTravelTime": 0, + "dailyInternalBikeTravels": 0 + }, + "pollution": { + "CO": 0.0, + "CO2_TOTAL": 0.0, + "HC": 0.0, + "NOx": 0.0, + "PM": 0.0, + "CO2_rep": 0.0 + } + }, + "entryCapacityToCenter": 0 + } +} \ No newline at end of file diff --git a/src/database.py b/src/database.py index 0d304f01915a580351f13a92abd9857edfa5cedb..d12e563046decbf9ed9d8bc257a9805b8d160497 100644 --- a/src/database.py +++ b/src/database.py @@ -65,7 +65,7 @@ def database_save_plan(cnx, request): intermediate_nodes) return constants.ALL_RIGHT -def database_store_kpi(cnx, request): +def database_store_kpi(cnx, request, relative=False): """ Stores kpi data from select json file in BD :return: @@ -73,7 +73,7 @@ def database_store_kpi(cnx, request): cursor = cnx.cursor(buffered=True) dic = request.args.to_dict() data = [key for key in dic.keys()] - file1 = open("../extra_data/%s" % data[0], "r") + file1 = open("extra_data/%s" % data[0], "r") file_contents= file1.read() parsed_json = json.loads(file_contents) keys_1_level = parsed_json.keys() @@ -94,7 +94,8 @@ def database_store_kpi(cnx, request): 'use_case_id': row[6], } pos += 1 - load_json_kpi_data(cnx, parsed_json['bilbao'], 0, kpis_bd_bio_data, 'BIO') + load_json_kpi_data(cnx, parsed_json['bilbao'], 0, kpis_bd_bio_data, 'BIO', relative) + return constants.ALL_RIGHT def kpi_by_name_level(cnx, kpi_name, kpi_level, case_id, parent_id=None): cursor = cnx.cursor(buffered=True) @@ -178,14 +179,45 @@ def update_kpi_action(cnx, value, kpi_name, case_id, parent_id): cursor.execute(constants.sql_insert_action_kpi, [4, parent_id, kpi_name, case_id, value, None, None, None]) cnx.commit() -def load_json_kpi_data(cnx, json, level, kpis_bd_bio_data, case_id, parent_id=None): +def get_action_kpi(cnx, action_kpi_id): + cursor = cnx.cursor(buffered=True) + cursor.execute(constants.sql_action_kpi, [action_kpi_id]) + ret = cursor.fetchall() + res_dict = {} + if ret and len(ret) == 1: + result = ret[0] + res_dict = { + 'id': result[0], + 'action_id': result[1], + 'kpi_id': result[2], + 'kpi_name': result[3], + 'use_case_id': result[4], + 'absolute': result[5], + # 'relative': ret[6], + # 'geographical': ret[7], + # 'zone_id': ret[8], + } + return res_dict + +def update_kpi_action_relative(cnx, value, kpi_name, case_id, relative, parent_id): + cursor = cnx.cursor(buffered=True) + kpi_action = get_action_kpi(cnx, parent_id) + if kpi_action and value != 0: + relative_data = (kpi_action['absolute'] - value) / (value * 100) + cursor.execute(constants.sql_update_action_kpi, [14, parent_id, kpi_name, case_id, value, relative_data, None, None, parent_id]) + cnx.commit() + +def load_json_kpi_data(cnx, json, level, kpis_bd_bio_data, case_id, relative=False, parent_id=None): if isinstance(json, dict): for json_key in json.keys(): key_data = get_json_key_bd_data(json_key, level, kpis_bd_bio_data, parent_id) if not key_data: key_data = kpi_by_name_level(cnx, json_key, level, case_id, parent_id) - load_json_kpi_data(cnx, json[json_key], level+1, kpis_bd_bio_data, case_id, key_data['id']) + load_json_kpi_data(cnx, json[json_key], level+1, kpis_bd_bio_data, case_id, relative, key_data['id']) else: parent_dict = get_kpi_data_by_id(parent_id, kpis_bd_bio_data) if parent_dict: - update_kpi_action(cnx, json, parent_dict['kpi_name'], case_id, parent_dict['id']) + if relative: + update_kpi_action_relative(cnx, json, parent_dict['kpi_name'], case_id, relative, parent_dict['id']) + else: + update_kpi_action(cnx, json, parent_dict['kpi_name'], case_id, parent_dict['id']) diff --git a/templates/index.html b/templates/index.html index 7c20f787199179f8343d3b8fa68c1a3a55d66cf8..266871bf68947cee43d4a82b74a468cbd43e1538 100644 --- a/templates/index.html +++ b/templates/index.html @@ -223,6 +223,53 @@ Created on: 16/01/2023 <p>Input: <br> A json file that matches the structure of the response of /planner/plan_detail </p> + + <h4>/database/store/kpi_relative</h4> + <p> + Takes a json file that matches the structure that /store/kpi creates and it stores it into the database.<br> + </p> + <p>Input</p> + <table class="table table-striped"> + <thead class="thead-dark"> + <tr> + <th scope="col">Parameter</th> + <th scope="col">Mandatory</th> + <th scope="col">Data</th> + <th scope="col">Example</th> + <th scope="col">Regexp</th> + </tr> + </thead> + <tbody> + <tr> + </tr> + <tr> + <th scope="row">Json</th> <td>Yes</td> <td>str</td> <td>file.json</td> <td>-</td> + </tr> + </tbody> + </table> + <h4>/database/store/kpi</h4> + <p> + Takes a json file that matches the structure that /store/kpi creates and it stores it into the database.<br> + </p> + <p>Input</p> + <table class="table table-striped"> + <thead class="thead-dark"> + <tr> + <th scope="col">Parameter</th> + <th scope="col">Mandatory</th> + <th scope="col">Data</th> + <th scope="col">Example</th> + <th scope="col">Regexp</th> + </tr> + </thead> + <tbody> + <tr> + </tr> + <tr> + <th scope="row">Json</th> <td>Yes</td> <td>str</td> <td>file.json</td> <td>-</td> + </tr> + </tbody> + </table> </div> </body> -</html> \ No newline at end of file +</html>