backoffice: add graphs on formdef statistics pages (#6879)

This commit is contained in:
Frédéric Péters 2015-04-01 15:59:29 +02:00
parent 8c6f9e8ad0
commit d899cb5950
2 changed files with 30 additions and 10 deletions

View File

@ -33,7 +33,7 @@ from qommon import errors
from qommon import ods
from qommon.form import *
from qommon.admin.menu import is_accessible
from qommon.storage import NotEqual, LessOrEqual, GreaterOrEqual
from qommon.storage import Equal, NotEqual, LessOrEqual, GreaterOrEqual
from wcs.forms.common import FormStatusPage
@ -105,13 +105,13 @@ def get_stats_criteria(request, parsed_values=None):
return criterias
def do_graphs_section(period_start, period_end):
def do_graphs_section(period_start=None, period_end=None, criterias=None):
from wcs import sql
r = TemplateIO(html=True)
monthly_totals = sql.get_monthly_totals(period_start, period_end)[-12:]
yearly_totals = sql.get_yearly_totals(period_start, period_end)[-10:]
monthly_totals = sql.get_monthly_totals(period_start, period_end, criterias)[-12:]
yearly_totals = sql.get_yearly_totals(period_start, period_end, criterias)[-10:]
weekday_totals = sql.get_weekday_totals(period_start, period_end)
weekday_totals = sql.get_weekday_totals(period_start, period_end, criterias)
weekday_line = []
weekday_names = [_('Sunday'), _('Monday'), _('Tuesday'),
_('Wednesday'), _('Thursday'), _('Friday'), _('Saturday')]
@ -121,7 +121,7 @@ def do_graphs_section(period_start, period_end):
# move Sunday to the last place
weekday_line = weekday_line[1:] + [weekday_line[0]]
hour_totals = sql.get_hour_totals(period_start, period_end)
hour_totals = sql.get_hour_totals(period_start, period_end, criterias)
if len(yearly_totals) > 1:
r += htmltext('<h3>%s</h3>') % _('Submissions by year')
@ -997,9 +997,14 @@ class FormPage(Directory):
get_response().filter['sidebar'] = self.get_stats_sidebar()
r += htmltext('<h2>%s - %s</h2>') % (self.formdef.name, _('Statistics'))
do_graphs = get_publisher().is_using_postgresql()
criterias = get_stats_criteria(get_request())
values = self.formdef.data_class().select(criterias)
if do_graphs:
r += htmltext('<div class="splitcontent-left">')
no_forms = len(values)
r += htmltext('<div class="bo-block">')
r += htmltext('<p>%s %d</p>') % (_('Total number of records:'), no_forms)
@ -1024,6 +1029,13 @@ class FormPage(Directory):
r += stats_times
r += htmltext('</div>')
if do_graphs:
r += htmltext('</div>')
r += htmltext('<div class="splitcontent-right">')
criterias.append(Equal('formdef_id', int(self.formdef.id)))
r += do_graphs_section(criterias=criterias)
r += htmltext('</div>')
r += htmltext('<a href=".">%s</a>') % _('Back')
return r.getvalue()

View File

@ -1365,11 +1365,13 @@ def get_period_clauses(period_start=None, period_end=None):
return clause
@guard_postgres
def get_weekday_totals(period_start=None, period_end=None):
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)
@ -1391,11 +1393,13 @@ def get_weekday_totals(period_start=None, period_end=None):
@guard_postgres
def get_hour_totals(period_start=None, period_end=None):
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)
@ -1418,11 +1422,13 @@ def get_hour_totals(period_start=None, period_end=None):
@guard_postgres
def get_monthly_totals(period_start=None, period_end=None):
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)
@ -1450,11 +1456,13 @@ def get_monthly_totals(period_start=None, period_end=None):
@guard_postgres
def get_yearly_totals(period_start=None, period_end=None):
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)