Skip to content
Snippets Groups Projects
Commit c23e267a authored by Campos Airas, Daniel's avatar Campos Airas, Daniel
Browse files

[IMP] Kpis relative load in to BD

parent 2e01e716
Branches
No related tags found
No related merge requests found
...@@ -222,7 +222,6 @@ def planner_plan_detail(): ...@@ -222,7 +222,6 @@ def planner_plan_detail():
@app.get("/database/store/kpi") @app.get("/database/store/kpi")
def 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 The json provided should have the same format that json with the kpi
""" """
#if request.json: #if request.json:
...@@ -231,6 +230,17 @@ def database_store_kpi(): ...@@ -231,6 +230,17 @@ def database_store_kpi():
else: else:
return constants.ERROR_JSON_NEEDED 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: ---------------------------------------------------------------------------------------------------------------- # MAIN: ----------------------------------------------------------------------------------------------------------------
if __name__ == "__main__": if __name__ == "__main__":
model = pickle.load(open('model.pkcls', 'rb')) model = pickle.load(open('model.pkcls', 'rb'))
......
{
"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
...@@ -65,7 +65,7 @@ def database_save_plan(cnx, request): ...@@ -65,7 +65,7 @@ def database_save_plan(cnx, request):
intermediate_nodes) intermediate_nodes)
return constants.ALL_RIGHT 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 Stores kpi data from select json file in BD
:return: :return:
...@@ -73,7 +73,7 @@ def database_store_kpi(cnx, request): ...@@ -73,7 +73,7 @@ def database_store_kpi(cnx, request):
cursor = cnx.cursor(buffered=True) cursor = cnx.cursor(buffered=True)
dic = request.args.to_dict() dic = request.args.to_dict()
data = [key for key in dic.keys()] 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() file_contents= file1.read()
parsed_json = json.loads(file_contents) parsed_json = json.loads(file_contents)
keys_1_level = parsed_json.keys() keys_1_level = parsed_json.keys()
...@@ -94,7 +94,8 @@ def database_store_kpi(cnx, request): ...@@ -94,7 +94,8 @@ def database_store_kpi(cnx, request):
'use_case_id': row[6], 'use_case_id': row[6],
} }
pos += 1 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): def kpi_by_name_level(cnx, kpi_name, kpi_level, case_id, parent_id=None):
cursor = cnx.cursor(buffered=True) cursor = cnx.cursor(buffered=True)
...@@ -178,14 +179,45 @@ def update_kpi_action(cnx, value, kpi_name, case_id, parent_id): ...@@ -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]) cursor.execute(constants.sql_insert_action_kpi, [4, parent_id, kpi_name, case_id, value, None, None, None])
cnx.commit() 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): if isinstance(json, dict):
for json_key in json.keys(): for json_key in json.keys():
key_data = get_json_key_bd_data(json_key, level, kpis_bd_bio_data, parent_id) key_data = get_json_key_bd_data(json_key, level, kpis_bd_bio_data, parent_id)
if not key_data: if not key_data:
key_data = kpi_by_name_level(cnx, json_key, level, case_id, parent_id) 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: else:
parent_dict = get_kpi_data_by_id(parent_id, kpis_bd_bio_data) parent_dict = get_kpi_data_by_id(parent_id, kpis_bd_bio_data)
if parent_dict: 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'])
...@@ -223,6 +223,53 @@ Created on: 16/01/2023 ...@@ -223,6 +223,53 @@ Created on: 16/01/2023
<p>Input: <br> <p>Input: <br>
A json file that matches the structure of the response of /planner/plan_detail A json file that matches the structure of the response of /planner/plan_detail
</p> </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> </div>
</body> </body>
</html> </html>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment