source: remit/finance_core/views.py @ 3f1e074

client
Last change on this file since 3f1e074 was 3f1e074, checked in by Alex Dehnert <adehnert@…>, 15 years ago

Calculate totals using annotate, not aggregate

For reports like "all the accounts ever", this can
increase performance by a factor of about three
and reduce the number of database queries by a
factor of about a hundred (aka the number of rows
in the table produced).

It is substantially more complicated, though.
The original code is in a "slow = False; if slow:"
block. In theory, it'd be good to keep both codepaths
working and test them against each other.

  • Property mode set to 100644
File size: 4.0 KB
Line 
1from django.http import HttpResponse
2import finance_core.models
3from django.contrib.auth.decorators import user_passes_test
4from django.shortcuts import render_to_response, get_object_or_404
5from django.template import RequestContext
6from django.http import Http404, HttpResponseRedirect
7from django.db.models import Q, Sum
8from decimal import Decimal
9
10def display_tree(request):
11    root = finance_core.models.BudgetArea.get_by_path(['Accounts'])
12    return HttpResponse(root.dump_to_html())
13
14def reporting(request):
15    line_items = finance_core.models.LineItem.objects.all()
16    term_name = 'All'
17    term_primary_Q = Q()
18    if 'term' in request.REQUEST:
19        term_obj = get_object_or_404(finance_core.models.BudgetTerm, slug=request.REQUEST['term'])
20        term_name = term_obj.name
21        line_items = line_items.filter(budget_term=term_obj)
22        term_primary_Q = Q(lineitem__budget_term=term_obj)
23    if 'area' in request.REQUEST:
24        base_area_obj = get_object_or_404(finance_core.models.BudgetArea, pk=request.REQUEST['area'])
25    else:
26        base_area_obj = finance_core.models.BudgetArea.get_by_path(['Accounts'])
27    line_items = line_items.filter(budget_area__in=base_area_obj.get_descendants())
28    base_area_depth = base_area_obj.depth
29    print base_area_obj
30
31    primary_name = 'Budget Areas'
32    primary_axis = [
33        (area.pk, area.indented_name(base_area_depth), Q(budget_area=area), ) for area in base_area_obj.get_descendants()
34    ]
35    primary_axis_objs = base_area_obj.get_descendants()
36    secondary_name = 'Layers'
37    secondary_axis = [
38        (
39            finance_core.models.layer_name(layer),
40            Q(layer=finance_core.models.layer_num(layer)),
41            Q(lineitem__layer=finance_core.models.layer_num(layer)),
42        )
43        for layer in finance_core.models.layers
44    ]
45
46    secondary_axis.append(('Total', Q(), Q()))
47
48    primary_labels = [ ]
49    arcprimary = {}
50    table = []
51    zero = Decimal('0.00')
52    for num, (pk, label, qobj, ) in enumerate(primary_axis):
53        primary_labels.append(label)
54        arcprimary[pk] = num
55        table.append([zero]*len(secondary_axis))
56    print arcprimary
57
58    secondary_labels = [ secondary[0] for secondary in secondary_axis ]
59
60    def lineitem_total(obj):
61        if obj.lineitem__amount__sum is None: return zero
62        else: return obj.lineitem__amount__sum
63    for num, (label, qobj_lineitem, qobj_primary) in enumerate(secondary_axis):
64        secondary_results = (primary_axis_objs.filter(qobj_primary, term_primary_Q).annotate(Sum('lineitem__amount')))
65        for cell in secondary_results:
66            print cell, cell.pk, arcprimary[cell.pk], num, table[arcprimary[cell.pk]]
67            table[arcprimary[cell.pk]][num] = lineitem_total(cell)
68
69    slow = False
70    if slow:
71        # This uses a simpler but probably slower method
72        # In theory, if we grow unit tests, comparing this method with
73        # the one above using annotate would be a good idea
74        def total_amount(queryset):
75            amount = queryset.aggregate(Sum('amount'))['amount__sum']
76            if amount is None: return zero
77            else: return amount
78        table = [ # Primary axis
79                [ # Secondary axis
80                    total_amount(line_items.filter(primary[1], secondary[1]))
81                for secondary in secondary_axis]
82            for primary in primary_axis]
83
84    debug = False
85    if debug:
86        from django.db import connection
87        print connection.queries
88        print "Number of queries:\t%d" % (len(connection.queries),)
89        print "Table size:\t%dx%d" % (len(primary_labels), len(secondary_labels), )
90
91    context = {
92        'pagename':'reporting',
93        'term_name': term_name,
94        'area': base_area_obj,
95        'primary_name': primary_name,
96        'secondary_name': secondary_name,
97        'primary_labels': primary_labels,
98        'secondary_labels': secondary_labels,
99        'table': table,
100        'table_with_row_labels': zip(primary_labels, table),
101    }
102    return render_to_response('finance_core/reporting.html', context, context_instance=RequestContext(request), )
Note: See TracBrowser for help on using the repository browser.