From 90b1895163e17b86ac662e08bc18fa1dfcdeeb05 Mon Sep 17 00:00:00 2001 From: Richard Hitier Date: Fri, 2 Apr 2021 11:08:15 +0200 Subject: [PATCH] New db method: charges by project for stacked data --- app/db_mgr.py | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/app/db_mgr.py b/app/db_mgr.py index 48fd14e..f1085f3 100644 --- a/app/db_mgr.py +++ b/app/db_mgr.py @@ -60,7 +60,6 @@ def charges_by_project(project_id): where project_id = {project_id} order by c.period_id """ - print(req_sql) req_res = db.session.execute(req_sql) results = list(req_res) # Remove comma @@ -73,6 +72,54 @@ def charges_by_project(project_id): return nocomma_results +def charges_by_project_stacked(project_id, category="service"): + """ + Build the list of charges for one project, period by period + :param project_id: the project's id we want to return data for + :param category: what dict to build for each period, 'service' or 'capacity' ? + :return: a 2 dim table with header as first line and datas next, of the form + period, category_0, category_1, ....., category_n, + sem_0, value_00, value_01, ....., value_0n, + sem_1, value_10, value_11, ....., value_1n, + . + . + sem_n, value_n0, value_n1, ....., value_nn, + """ + if category == 'service': + category_table = 'service' + sql_req = """ + select c.charge_rate + from capacity c1 left join + charge c on c1.id = c.capacity_id and project_id = {} and period_id={} + order by c1.id + """ + elif category == 'capacity': + category_table = 'capacity' + sql_req = """ + select c.charge_rate + from service s left join + charge c on s.id = c.service_id and project_id = {} and period_id={} + order by s.id + """ + else: + raise ValueError("Waiting for 'service' or 'capacity'") + categories_req = "select name from {} order by id".format(category) + categories = [c for (c,) in db.session.execute(categories_req)] + headers = ['period']+categories + all_charges = [headers] + for (period_id, period_name) in db.session.execute("select id, name from period order by id"): + # build the request from the string previously set + charge_by_categorie_req = sql_req.format(project_id, period_id) + # build the charges line for the current period + category_charges = [period_name] + for (category_rate,) in db.session.execute(charge_by_categorie_req): + category_rate = '0' if category_rate is None else str(category_rate) + category_charges.append(category_rate) + all_charges.append(category_charges) + + return all_charges + + def charges_by_agent_tabled(agent_id): """ Build the flatten list of charges for one agent, period by period -- libgit2 0.21.2