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

[IMP] Load kpis area and relative fields on BD kpi_action

parent 59fb8d63
No related branches found
No related tags found
No related merge requests found
......@@ -237,7 +237,29 @@ def database_store_kpi_relative():
"""
#if request.json:
if request:
return database.database_store_kpi(cnx, request, True)
return database.database_store_kpi(cnx, request, relative=True)
else:
return constants.ERROR_JSON_NEEDED
@app.get("/database/store/kpi_area")
def database_store_kpi_area():
"""
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, area=True)
else:
return constants.ERROR_JSON_NEEDED
@app.get("/database/store/kpi_relative_area")
def database_store_kpi_relative_area():
"""
The json provided should have the same format that json with the kpi
"""
#if request.json:
if request:
return database.database_store_kpi_relative_area(cnx, request)
else:
return constants.ERROR_JSON_NEEDED
......
......@@ -82,7 +82,8 @@ VALUES(%s, %s, %s, %s, %s, %s)
sql_get_kpis_data = """SELECT id, kpi_name, kpi_full_name, parent_id, kpi_level, popularity, use_case_id FROM kpi"""
sql_get_kpi_from_case_ids = """SELECT id, kpi_name, kpi_full_name, parent_id, kpi_level, popularity, use_case_id FROM kpi WHERE use_case_id IN (%s)"""
sql_action_kpi = """SELECT id, action_id, kpi_id, kpi_name, use_case_id, absolute FROM kpi_action WHERE kpi_id = %s"""
sql_action_kpi = """SELECT action_id, kpi_id, kpi_name, use_case_id, absolute FROM kpi_action WHERE kpi_id = %s"""
sql_action_kpi_zone = """SELECT action_id, kpi_id, kpi_name, use_case_id, absolute FROM kpi_action WHERE kpi_id = %s and ZoneId= %s"""
sql_insert_action_kpi = """
INSERT IGNORE INTO kpi_action(action_id, kpi_id, kpi_name, use_case_id, absolute, relative, geographical, zoneId)
......@@ -94,3 +95,15 @@ UPDATE kpi_action
SET action_id = %s, kpi_id = %s, kpi_name = %s, use_case_id = %s, absolute = %s, relative = %s, geographical = %s, zoneId = %s
WHERE kpi_id = %s
"""
sql_update_action_kpi_area = """
UPDATE kpi_action
SET action_id = %s, kpi_id = %s, geographical = %s, zoneId = %s
WHERE kpi_id = %s
"""
sql_update_action_kpi_relative_area = """
UPDATE kpi_action
SET action_id = %s, kpi_id = %s, relative = %s
WHERE kpi_id = %s and zoneId = %s
"""
......@@ -65,7 +65,7 @@ def database_save_plan(cnx, request):
intermediate_nodes)
return constants.ALL_RIGHT
def database_store_kpi(cnx, request, relative=False):
def database_store_kpi(cnx, request, relative=False, area=False):
"""
Stores kpi data from select json file in BD
:return:
......@@ -80,6 +80,8 @@ def database_store_kpi(cnx, request, relative=False):
kpis_bd_bio_data = {}
pos = 0
for key in keys_1_level:
if area == True and key == 'area':
area = parsed_json['area']
if key == 'bilbao':
cursor.execute(constants.sql_get_kpi_from_case_ids, ['BIO'])
ret = cursor.fetchall()
......@@ -94,7 +96,7 @@ def database_store_kpi(cnx, request, relative=False):
'use_case_id': row[6],
}
pos += 1
load_json_kpi_data(cnx, parsed_json['bilbao'], 0, kpis_bd_bio_data, 'BIO', relative)
load_json_kpi_data(cnx, parsed_json['bilbao'], 0, kpis_bd_bio_data, 'BIO', relative=relative, area=area)
return constants.ALL_RIGHT
def kpi_by_name_level(cnx, kpi_name, kpi_level, case_id, parent_id=None):
......@@ -179,20 +181,22 @@ 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 get_action_kpi(cnx, action_kpi_id):
def get_action_kpi(cnx, action_kpi_id, zone_id=False):
cursor = cnx.cursor(buffered=True)
cursor.execute(constants.sql_action_kpi, [action_kpi_id])
if zone_id:
cursor.execute(constants.sql_action_kpi_zone, [action_kpi_id, zone_id])
else:
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],
'action_id': result[0],
'kpi_id': result[1],
'kpi_name': result[2],
'use_case_id': result[3],
'absolute': result[4],
# 'relative': ret[6],
# 'geographical': ret[7],
# 'zone_id': ret[8],
......@@ -207,17 +211,75 @@ def update_kpi_action_relative(cnx, value, kpi_name, case_id, relative, parent_i
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):
def update_kpi_action_area(cnx, value, kpi_name, case_id, area, parent_id):
cursor = cnx.cursor(buffered=True)
kpi_action = get_action_kpi(cnx, parent_id)
if kpi_action and value != 0:
cursor.execute(constants.sql_update_action_kpi_area, [6, parent_id, value, area, parent_id])
cnx.commit()
def update_kpi_action_relative_area(cnx, value, kpi_name, case_id, relative_area, parent_id):
cursor = cnx.cursor(buffered=True)
kpi_action = get_action_kpi(cnx, parent_id, zone_id=relative_area)
if kpi_action and value != 0:
relative_data = (kpi_action['absolute'] - value) / (value * 100)
cursor.execute(constants.sql_update_action_kpi_relative_area,
[14, parent_id, relative_data, parent_id, relative_area])
cnx.commit()
def load_json_kpi_data(
cnx, json, level, kpis_bd_bio_data, case_id, relative=False, area=False, relative_area=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, relative, key_data['id'])
load_json_kpi_data(cnx, json[json_key], level+1, kpis_bd_bio_data, case_id, relative=relative, area=area,
relative_area=relative_area, parent_id=key_data['id'])
else:
parent_dict = get_kpi_data_by_id(parent_id, kpis_bd_bio_data)
if parent_dict:
if relative:
update_kpi_action_relative(cnx, json, parent_dict['kpi_name'], case_id, relative, parent_dict['id'])
update_kpi_action_relative(cnx, json, parent_dict['kpi_name'], case_id, relative,
parent_id=parent_dict['id'])
elif area:
update_kpi_action_area(cnx, json, parent_dict['kpi_name'], case_id, area, parent_id=parent_dict['id'])
elif relative_area:
update_kpi_action_relative_area(cnx, json, parent_dict['kpi_name'], case_id, relative_area,
parent_id=parent_dict['id'])
else:
update_kpi_action(cnx, json, parent_dict['kpi_name'], case_id, parent_dict['id'])
def database_store_kpi_relative_area(cnx, request):
"""
Stores kpi data from select json file in BD
:return:
"""
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")
file_contents= file1.read()
parsed_json = json.loads(file_contents)
keys_1_level = parsed_json.keys()
kpis_bd_bio_data = {}
pos = 0
for key in keys_1_level:
if key == 'area':
relative_area = parsed_json['area']
if key == 'bilbao':
cursor.execute(constants.sql_get_kpi_from_case_ids, ['BIO'])
ret = cursor.fetchall()
for row in ret:
kpis_bd_bio_data[pos] = {
'id': row[0],
'kpi_name': row[1],
'kpi_full_name': row[2],
'parent_id': row[3],
'kpi_level': row[4],
'popularity': row[5],
'use_case_id': row[6],
}
pos += 1
load_json_kpi_data(cnx, parsed_json['bilbao'], 0, kpis_bd_bio_data, 'BIO', relative_area=relative_area)
return constants.ALL_RIGHT
......@@ -223,8 +223,7 @@ 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>
<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>
......@@ -247,7 +246,53 @@ Created on: 16/01/2023
</tr>
</tbody>
</table>
<h4>/database/store/kpi</h4>
<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_area</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_area_relative</h4>
<p>
Takes a json file that matches the structure that /store/kpi creates and it stores it into the database.<br>
</p>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment