backoffice: use SQL views to compute counts in management view (#21949)

This commit is contained in:
Frédéric Péters 2018-02-18 14:31:38 +01:00
parent b0ecbc544f
commit ba5936593e
2 changed files with 42 additions and 3 deletions

View File

@ -461,10 +461,20 @@ class ManagementDirectory(Directory):
forms_without_pending_stuff = []
forms_with_pending_stuff = []
using_postgresql = get_publisher().is_using_postgresql()
if using_postgresql:
from wcs import sql
actionable_counts = sql.get_actionable_counts(user_roles)
total_counts = sql.get_total_counts()
def append_form_entry(formdef):
formdef_data_class = formdef.data_class()
count_forms = formdef_data_class.count() - len(formdef_data_class.get_ids_with_indexed_value('status', 'draft'))
waiting_forms_count = formdef_data_class.get_actionable_count(user_roles)
if using_postgresql:
count_forms = total_counts.get(formdef.id) or 0
waiting_forms_count = actionable_counts.get(formdef.id) or 0
else:
formdef_data_class = formdef.data_class()
count_forms = formdef_data_class.count() - len(formdef_data_class.get_ids_with_indexed_value('status', 'draft'))
waiting_forms_count = formdef_data_class.get_actionable_count(user_roles)
if waiting_forms_count == 0:
forms_without_pending_stuff.append((formdef, waiting_forms_count, count_forms))
else:

View File

@ -1818,6 +1818,35 @@ def get_period_query(period_start=None, period_end=None, criterias=None, paramet
statement += ' WHERE ' + ' AND '.join(where_clauses)
return statement
@guard_postgres
def get_actionable_counts(user_roles):
conn, cur = get_connection_and_cursor()
criterias = [Equal('is_at_endpoint', False),
Intersects('actions_roles_array', user_roles)]
where_clauses, parameters, func_clause = parse_clause(criterias)
statement = '''SELECT formdef_id, COUNT(*)
FROM wcs_all_forms
WHERE %s
GROUP BY formdef_id''' % ' AND '.join(where_clauses)
cur.execute(statement, parameters)
counts = {str(x): y for x, y in cur.fetchall()}
conn.commit()
cur.close()
return counts
@guard_postgres
def get_total_counts():
conn, cur = get_connection_and_cursor()
statement = '''SELECT formdef_id, COUNT(*)
FROM wcs_all_forms
WHERE status != 'draft'
GROUP BY formdef_id'''
cur.execute(statement)
counts = {str(x): y for x, y in cur.fetchall()}
conn.commit()
cur.close()
return counts
@guard_postgres
def get_weekday_totals(period_start=None, period_end=None, criterias=None):
conn, cur = get_connection_and_cursor()