sql: count stats on formdef table instead of global view when relevant (#6893)
This commit is contained in:
parent
5797c76e47
commit
368ed699b0
65
wcs/sql.py
65
wcs/sql.py
|
@ -1356,25 +1356,35 @@ class TrackingCode(SqlMixin, wcs.tracking_code.TrackingCode):
|
|||
get_data_fields = classmethod(get_data_fields)
|
||||
|
||||
|
||||
def get_period_clauses(period_start=None, period_end=None):
|
||||
clause = []
|
||||
def get_period_query(period_start=None, period_end=None, criterias=None, parameters=None):
|
||||
clause = [NotNull('receipt_time')]
|
||||
table_name = 'wcs_all_forms'
|
||||
if criterias:
|
||||
for criteria in criterias:
|
||||
if criteria.__class__.__name__ == 'Equal' and \
|
||||
criteria.attribute == 'formdef_id':
|
||||
# if there's a formdef_id specified, switch to using the
|
||||
# specific table so we have access to all fields
|
||||
from wcs.formdef import FormDef
|
||||
table_name = get_formdef_table_name(FormDef.get(criteria.value))
|
||||
continue
|
||||
clause.append(criteria)
|
||||
if period_start:
|
||||
clause.append(GreaterOrEqual('receipt_time', period_start))
|
||||
if period_end:
|
||||
clause.append(LessOrEqual('receipt_time', period_end))
|
||||
return clause
|
||||
where_clauses, params, func_clause = parse_clause(clause)
|
||||
parameters.update(params)
|
||||
statement = ' FROM %s ' % table_name
|
||||
statement += ' WHERE ' + ' AND '.join(where_clauses)
|
||||
return statement
|
||||
|
||||
@guard_postgres
|
||||
def get_weekday_totals(period_start=None, period_end=None, criterias=None):
|
||||
conn, cur = get_connection_and_cursor()
|
||||
statement = '''SELECT DATE_PART('dow', receipt_time) AS weekday, COUNT(*)
|
||||
FROM wcs_all_forms'''
|
||||
clause = [NotNull('receipt_time')]
|
||||
if criterias:
|
||||
clause.extend(criterias)
|
||||
clause.extend(get_period_clauses(period_start, period_end))
|
||||
where_clauses, parameters, func_clause = parse_clause(clause)
|
||||
statement += ' WHERE ' + ' AND '.join(where_clauses)
|
||||
statement = '''SELECT DATE_PART('dow', receipt_time) AS weekday, COUNT(*)'''
|
||||
parameters = {}
|
||||
statement += get_period_query(period_start, period_end, criterias, parameters)
|
||||
statement += ' GROUP BY weekday ORDER BY weekday'''
|
||||
cur.execute(statement, parameters)
|
||||
|
||||
|
@ -1395,14 +1405,9 @@ def get_weekday_totals(period_start=None, period_end=None, criterias=None):
|
|||
@guard_postgres
|
||||
def get_hour_totals(period_start=None, period_end=None, criterias=None):
|
||||
conn, cur = get_connection_and_cursor()
|
||||
statement = '''SELECT DATE_PART('hour', receipt_time) AS hour, COUNT(*)
|
||||
FROM wcs_all_forms'''
|
||||
clause = [NotNull('receipt_time')]
|
||||
if criterias:
|
||||
clause.extend(criterias)
|
||||
clause.extend(get_period_clauses(period_start, period_end))
|
||||
where_clauses, parameters, func_clause = parse_clause(clause)
|
||||
statement += ' WHERE ' + ' AND '.join(where_clauses)
|
||||
statement = '''SELECT DATE_PART('hour', receipt_time) AS hour, COUNT(*)'''
|
||||
parameters = {}
|
||||
statement += get_period_query(period_start, period_end, criterias, parameters)
|
||||
statement += ' GROUP BY hour ORDER BY hour'
|
||||
cur.execute(statement, parameters)
|
||||
|
||||
|
@ -1424,14 +1429,9 @@ def get_hour_totals(period_start=None, period_end=None, criterias=None):
|
|||
@guard_postgres
|
||||
def get_monthly_totals(period_start=None, period_end=None, criterias=None):
|
||||
conn, cur = get_connection_and_cursor()
|
||||
statement = '''SELECT DATE_TRUNC('month', receipt_time) AS month, COUNT(*)
|
||||
FROM wcs_all_forms '''
|
||||
clause = [NotNull('receipt_time')]
|
||||
if criterias:
|
||||
clause.extend(criterias)
|
||||
clause.extend(get_period_clauses(period_start, period_end))
|
||||
where_clauses, parameters, func_clause = parse_clause(clause)
|
||||
statement += ' WHERE ' + ' AND '.join(where_clauses)
|
||||
statement = '''SELECT DATE_TRUNC('month', receipt_time) AS month, COUNT(*) '''
|
||||
parameters = {}
|
||||
statement += get_period_query(period_start, period_end, criterias, parameters)
|
||||
statement += ' GROUP BY month ORDER BY month'''
|
||||
cur.execute(statement, parameters)
|
||||
|
||||
|
@ -1458,14 +1458,9 @@ def get_monthly_totals(period_start=None, period_end=None, criterias=None):
|
|||
@guard_postgres
|
||||
def get_yearly_totals(period_start=None, period_end=None, criterias=None):
|
||||
conn, cur = get_connection_and_cursor()
|
||||
statement = '''SELECT DATE_TRUNC('year', receipt_time) AS year, COUNT(*)
|
||||
FROM wcs_all_forms'''
|
||||
clause = [NotNull('receipt_time')]
|
||||
if criterias:
|
||||
clause.extend(criterias)
|
||||
clause.extend(get_period_clauses(period_start, period_end))
|
||||
where_clauses, parameters, func_clause = parse_clause(clause)
|
||||
statement += ' WHERE ' + ' AND '.join(where_clauses)
|
||||
statement = '''SELECT DATE_TRUNC('year', receipt_time) AS year, COUNT(*)'''
|
||||
parameters = {}
|
||||
statement += get_period_query(period_start, period_end, criterias, parameters)
|
||||
statement += ' GROUP BY year ORDER BY year'
|
||||
cur.execute(statement, parameters)
|
||||
|
||||
|
|
Loading…
Reference in New Issue