"""Statistics generator UI implementation.
Implements the statistics pages. These pages display various counts of
interest related to admissions. These pages are not currently used and
probably need work in order to operate correcty with the current database
schema.
"""
from itertools import groupby
from operator import attrgetter
from ll.xist.ns import html
from uw.web.wsgi import status
from uw.web.wsgi.function import return_html
from uw.local import termtools
@return_html
def stats_index_handler (cursor):
raise status.HTTPFound ("CS/")
[docs]def make_term_link (term, subpage, text):
return html.a (text % term.description (),
href="../%s/%s" % (term.code (), subpage or ""))
[docs]def make_navigation (items, css_class):
return html.table (
html.tr (
html.td (item) for item in items
),
class_=css_class,
)
@return_html
def stats_unit_handler (cursor, unit):
raise status.HTTPFound ("%s/" % (termtools.current () + 2).code ())
@return_html
def stats_display_handler (cursor, unit, term):
# TODO: still need to delegate based on subpage
subpage = None
subpages = (
('type', "Summary"),
('status', "Status"),
('forms', "Checklist"),
)
term_table = [
make_term_link (term - 3, subpage, '%s ← ←'),
make_term_link (term - 1, subpage, '%s ←'),
html.span (term.description ()),
make_term_link (term + 1, subpage, '→ %s'),
make_term_link (term + 3, subpage, '→ → %s'),
]
vars = {
'group': unit.group_code,
'unit': unit.unit_code,
'term': term.code (),
}
if subpage == 'type':
info = groupby (cursor.execute_tuples (
"select * from (SELECT (work_application_proglevel (wa.uw_id, wa.app_id)) as level_code, imm_status, COUNT(*) AS count, COUNT(status_code IN ('INO', 'DEC', 'OFA', 'ACR') OR NULL) AS count_offers, COUNT(status_code IN ('OFA', 'ACR') OR NULL) AS count_accept, COUNT(status_code IN ('DEC') OR NULL) AS count_decline FROM work_application AS wa NATURAL JOIN work_application_status_current NATURAL LEFT JOIN (select uw_id, citizenship_description as imm_status from std_citizenship natural join uw_citizenship where country_code = 'CAN') WHERE (wa.group_code, wa.unit_code, wa.admit_term) = (%(group)s, %(unit)s, %(term)s) GROUP BY level_code, imm_status ORDER BY level_code, imm_status) AS t NATURAL JOIN uw_level order by level_sequence, imm_status", **vars
), attrgetter ('level_code'))
table = html.table (
html.tr (
html.th ('Level'),
html.th ('Citizenship'),
html.th ('Applied'),
html.th ('Offerred'),
html.th ('Accepted'),
html.th ('Declined'),
),
class_="stats"
)
count = 0
count_offers = 0
count_accept = 0
count_decline = 0
for level, data in info:
data = list (data)
for i, item in enumerate (data):
table.append (
html.tr (
html.th (item.level_description, rowspan=len (data)) if i == 0 else None,
html.th (item.imm_status),
html.td (item.count),
html.td (item.count_offers),
html.td (item.count_accept),
html.td (item.count_decline),
),
)
count += item.count
count_offers += item.count_offers
count_accept += item.count_accept
count_decline += item.count_decline
table.append (
html.tr (
html.th ('Total', colspan=2),
html.td (count),
html.td (count_offers),
html.td (count_accept),
html.td (count_decline),
)
)
stats = table
elif subpage == 'status':
info = cursor.execute_tuples (
"select status_code, status_description, COALESCE (count, 0) AS count from (SELECT status_code, COUNT(*) AS count FROM work_application NATURAL JOIN work_application_status_current WHERE (wa.group_code, wa.unit_code, wa.admit_term) = (%(group)s, %(unit)s, %(term)s) GROUP BY admit_term, status_code) AS t NATURAL RIGHT JOIN work_application_status_code order by status_sequence", **vars
)
stats = html.table (
html.tr (
html.th ('Status'),
html.th ('Count'),
), [
html.tr (
html.td (row.status_description, class_="label"),
html.td (row.count),
) for row in info
],
html.tr (
html.td (html.b ('Total'), class_="label"),
html.td (html.b (sum (row.count for row in info))),
),
class_="stats"
)
elif subpage == 'forms':
info = dict (((row.section_code, row.status_code), row.count)
for row in cursor.execute_tuples (
"select section_code, status_code, COUNT(*) AS count from checklist_complete NATURAL JOIN work_application where (wa.group_code, wa.unit_code, wa.admit_term) = (%(group)s, %(unit)s, %(term)s) group by section_code, status_code", **vars
))
status_codes = cursor.execute_tuples ("select * from checklist_status order by status_description")
section_codes = cursor.execute_tuples ("select * from checklist_section order by section_sequence")
table = html.table (
html.tr (
html.th (),
[
html.th (section.section_description)
for section in section_codes
],
html.th ('Total')
),
class_="stats"
)
total = dict ((section.section_code, 0)
for section in section_codes)
complete_total = dict (total)
missing_total = dict (total)
for status in status_codes:
row = html.tr (html.th (status.status_description))
table.append (row)
row_total = 0
for section in section_codes:
count = info.get ((section.section_code,
status.status_code), 0)
row.append (html.td (count))
row_total += count
total[section.section_code] += count
if status.status_complete:
complete_total[section.section_code] += count
else:
missing_total[section.section_code] += count
row.append (html.td (row_total, class_="total"))
for title, totals, line_above in (('Complete Total', complete_total, True), ('Missing Total', missing_total, False), ('Overall Total', total, True)):
table.append (
html.tr (
html.th (title),
[
html.td (totals[section.section_code])
for section in section_codes
],
html.td (sum (totals.values ()), class_="total"),
class_="line" if line_above else None
)
)
stats = table
else:
stats = None
subpage_table = []
for page, label in subpages:
if page == subpage:
subpage_table.append (html.span (label))
else:
subpage_table.append (html.a (label, href=page))
return ('Statistics for %s' % term.description (), [
html.p (),
make_navigation (term_table, "viewpage"),
make_navigation (subpage_table, "viewpage"),
stats
])