sql: use criterias to create periodic stats (#6876)
This commit is contained in:
parent
a9b5fde14f
commit
c96be5c72e
92
wcs/sql.py
92
wcs/sql.py
|
@ -98,6 +98,18 @@ class Contains(Criteria):
|
|||
class NotContains(Contains):
|
||||
sql_op = 'NOT IN'
|
||||
|
||||
class NotNull(Criteria):
|
||||
sql_op = 'IS NOT NULL'
|
||||
|
||||
def __init__(self, attribute):
|
||||
self.attribute = attribute
|
||||
|
||||
def as_sql(self):
|
||||
return '%s %s' % (self.attribute, self.sql_op)
|
||||
|
||||
def as_sql_param(self):
|
||||
return {}
|
||||
|
||||
class Or(Criteria):
|
||||
def __init__(self, criterias):
|
||||
self.criterias = []
|
||||
|
@ -1343,21 +1355,25 @@ 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 = []
|
||||
if period_start:
|
||||
clause.append(GreaterOrEqual('receipt_time', period_start))
|
||||
if period_end:
|
||||
clause.append(LessOrEqual('receipt_time', period_end))
|
||||
return clause
|
||||
|
||||
@guard_postgres
|
||||
def get_weekday_totals(period_start=None, period_end=None):
|
||||
conn, cur = get_connection_and_cursor()
|
||||
statement = '''SELECT DATE_PART('dow', receipt_time) AS weekday, COUNT(*)
|
||||
FROM wcs_all_forms WHERE receipt_time is not NULL '''
|
||||
if period_start or period_end:
|
||||
statement += ' AND '
|
||||
if period_start:
|
||||
statement += ' receipt_time >= %(period_start)s '
|
||||
if period_start and period_end:
|
||||
statement += ' AND '
|
||||
if period_end:
|
||||
statement += ' receipt_time <= %(period_end)s '
|
||||
statement += 'GROUP BY weekday ORDER BY weekday'''
|
||||
cur.execute(statement, {'period_start': period_start, 'period_end': period_end})
|
||||
FROM wcs_all_forms'''
|
||||
clause = [NotNull('receipt_time')]
|
||||
clause.extend(get_period_clauses(period_start, period_end))
|
||||
where_clauses, parameters, func_clause = parse_clause(clause)
|
||||
statement += ' WHERE ' + ' AND '.join(where_clauses)
|
||||
statement += ' GROUP BY weekday ORDER BY weekday'''
|
||||
cur.execute(statement, parameters)
|
||||
|
||||
result = cur.fetchall()
|
||||
result = [(int(x), y) for x, y in result]
|
||||
|
@ -1377,17 +1393,13 @@ def get_weekday_totals(period_start=None, period_end=None):
|
|||
def get_hour_totals(period_start=None, period_end=None):
|
||||
conn, cur = get_connection_and_cursor()
|
||||
statement = '''SELECT DATE_PART('hour', receipt_time) AS hour, COUNT(*)
|
||||
FROM wcs_all_forms WHERE receipt_time is not NULL '''
|
||||
if period_start or period_end:
|
||||
statement += ' AND '
|
||||
if period_start:
|
||||
statement += ' receipt_time >= %(period_start)s '
|
||||
if period_start and period_end:
|
||||
statement += ' AND '
|
||||
if period_end:
|
||||
statement += ' receipt_time <= %(period_end)s '
|
||||
statement += 'GROUP BY hour ORDER BY hour'''
|
||||
cur.execute(statement, {'period_start': period_start, 'period_end': period_end})
|
||||
FROM wcs_all_forms'''
|
||||
clause = [NotNull('receipt_time')]
|
||||
clause.extend(get_period_clauses(period_start, period_end))
|
||||
where_clauses, parameters, func_clause = parse_clause(clause)
|
||||
statement += ' WHERE ' + ' AND '.join(where_clauses)
|
||||
statement += ' GROUP BY hour ORDER BY hour'
|
||||
cur.execute(statement, parameters)
|
||||
|
||||
result = cur.fetchall()
|
||||
result = [(int(x), y) for x, y in result]
|
||||
|
@ -1408,17 +1420,13 @@ def get_hour_totals(period_start=None, period_end=None):
|
|||
def get_monthly_totals(period_start=None, period_end=None):
|
||||
conn, cur = get_connection_and_cursor()
|
||||
statement = '''SELECT DATE_TRUNC('month', receipt_time) AS month, COUNT(*)
|
||||
FROM wcs_all_forms WHERE receipt_time is not NULL '''
|
||||
if period_start or period_end:
|
||||
statement += ' AND '
|
||||
if period_start:
|
||||
statement += ' receipt_time >= %(period_start)s '
|
||||
if period_start and period_end:
|
||||
statement += ' AND '
|
||||
if period_end:
|
||||
statement += ' receipt_time <= %(period_end)s '
|
||||
statement += 'GROUP BY month ORDER BY month'''
|
||||
cur.execute(statement, {'period_start': period_start, 'period_end': period_end})
|
||||
FROM wcs_all_forms '''
|
||||
clause = [NotNull('receipt_time')]
|
||||
clause.extend(get_period_clauses(period_start, period_end))
|
||||
where_clauses, parameters, func_clause = parse_clause(clause)
|
||||
statement += ' WHERE ' + ' AND '.join(where_clauses)
|
||||
statement += ' GROUP BY month ORDER BY month'''
|
||||
cur.execute(statement, parameters)
|
||||
|
||||
raw_result = cur.fetchall()
|
||||
result = [('%d-%02d' % x.timetuple()[:2], y) for x, y in raw_result]
|
||||
|
@ -1444,17 +1452,13 @@ def get_monthly_totals(period_start=None, period_end=None):
|
|||
def get_yearly_totals(period_start=None, period_end=None):
|
||||
conn, cur = get_connection_and_cursor()
|
||||
statement = '''SELECT DATE_TRUNC('year', receipt_time) AS year, COUNT(*)
|
||||
FROM wcs_all_forms WHERE receipt_time is not NULL '''
|
||||
if period_start or period_end:
|
||||
statement += ' AND '
|
||||
if period_start:
|
||||
statement += ' receipt_time >= %(period_start)s '
|
||||
if period_start and period_end:
|
||||
statement += ' AND '
|
||||
if period_end:
|
||||
statement += ' receipt_time <= %(period_end)s '
|
||||
statement += 'GROUP BY year ORDER BY year'''
|
||||
cur.execute(statement, {'period_start': period_start, 'period_end': period_end})
|
||||
FROM wcs_all_forms'''
|
||||
clause = [NotNull('receipt_time')]
|
||||
clause.extend(get_period_clauses(period_start, period_end))
|
||||
where_clauses, parameters, func_clause = parse_clause(clause)
|
||||
statement += ' WHERE ' + ' AND '.join(where_clauses)
|
||||
statement += ' GROUP BY year ORDER BY year'
|
||||
cur.execute(statement, parameters)
|
||||
|
||||
raw_result = cur.fetchall()
|
||||
result = [(str(x.year), y) for x, y in raw_result]
|
||||
|
|
Loading…
Reference in New Issue