Commit 90b1895163e17b86ac662e08bc18fa1dfcdeeb05
1 parent
17b05589
Exists in
master
and in
4 other branches
New db method: charges by project for stacked data
Showing
1 changed file
with
48 additions
and
1 deletions
Show diff stats
app/db_mgr.py
@@ -60,7 +60,6 @@ def charges_by_project(project_id): | @@ -60,7 +60,6 @@ def charges_by_project(project_id): | ||
60 | where project_id = {project_id} | 60 | where project_id = {project_id} |
61 | order by c.period_id | 61 | order by c.period_id |
62 | """ | 62 | """ |
63 | - print(req_sql) | ||
64 | req_res = db.session.execute(req_sql) | 63 | req_res = db.session.execute(req_sql) |
65 | results = list(req_res) | 64 | results = list(req_res) |
66 | # Remove comma | 65 | # Remove comma |
@@ -73,6 +72,54 @@ def charges_by_project(project_id): | @@ -73,6 +72,54 @@ def charges_by_project(project_id): | ||
73 | return nocomma_results | 72 | return nocomma_results |
74 | 73 | ||
75 | 74 | ||
75 | +def charges_by_project_stacked(project_id, category="service"): | ||
76 | + """ | ||
77 | + Build the list of charges for one project, period by period | ||
78 | + :param project_id: the project's id we want to return data for | ||
79 | + :param category: what dict to build for each period, 'service' or 'capacity' ? | ||
80 | + :return: a 2 dim table with header as first line and datas next, of the form | ||
81 | + period, category_0, category_1, ....., category_n, | ||
82 | + sem_0, value_00, value_01, ....., value_0n, | ||
83 | + sem_1, value_10, value_11, ....., value_1n, | ||
84 | + . | ||
85 | + . | ||
86 | + sem_n, value_n0, value_n1, ....., value_nn, | ||
87 | + """ | ||
88 | + if category == 'service': | ||
89 | + category_table = 'service' | ||
90 | + sql_req = """ | ||
91 | + select c.charge_rate | ||
92 | + from capacity c1 left join | ||
93 | + charge c on c1.id = c.capacity_id and project_id = {} and period_id={} | ||
94 | + order by c1.id | ||
95 | + """ | ||
96 | + elif category == 'capacity': | ||
97 | + category_table = 'capacity' | ||
98 | + sql_req = """ | ||
99 | + select c.charge_rate | ||
100 | + from service s left join | ||
101 | + charge c on s.id = c.service_id and project_id = {} and period_id={} | ||
102 | + order by s.id | ||
103 | + """ | ||
104 | + else: | ||
105 | + raise ValueError("Waiting for 'service' or 'capacity'") | ||
106 | + categories_req = "select name from {} order by id".format(category) | ||
107 | + categories = [c for (c,) in db.session.execute(categories_req)] | ||
108 | + headers = ['period']+categories | ||
109 | + all_charges = [headers] | ||
110 | + for (period_id, period_name) in db.session.execute("select id, name from period order by id"): | ||
111 | + # build the request from the string previously set | ||
112 | + charge_by_categorie_req = sql_req.format(project_id, period_id) | ||
113 | + # build the charges line for the current period | ||
114 | + category_charges = [period_name] | ||
115 | + for (category_rate,) in db.session.execute(charge_by_categorie_req): | ||
116 | + category_rate = '0' if category_rate is None else str(category_rate) | ||
117 | + category_charges.append(category_rate) | ||
118 | + all_charges.append(category_charges) | ||
119 | + | ||
120 | + return all_charges | ||
121 | + | ||
122 | + | ||
76 | def charges_by_agent_tabled(agent_id): | 123 | def charges_by_agent_tabled(agent_id): |
77 | """ | 124 | """ |
78 | Build the flatten list of charges for one agent, period by period | 125 | Build the flatten list of charges for one agent, period by period |