summaryrefslogtreecommitdiff
path: root/app/views/stats.py
blob: b1e9b1a46ca4bb2e9f98ce0a64d4b3fa7e824f86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from . import Blueprint, flash, db, \
        current_user, login_required, \
        assert_authorisation, templated, redirect, request, \
        today

from .. import forms as F
from ..model import ConstExpense
import sqlalchemy as sql
import time
from collections import defaultdict

mod = Blueprint('stats', __name__)

def next_date(d):
    if d.month == 12:
        return d.replace(year = d.year + 1, month = 1)
    else:
        return d.replace(month = d.month + 1)

@mod.route('/')
@login_required
@templated
def show():
    # easy way: fetch them all and then do some computation
    expenses = defaultdict(int)
    t = next_date (today().replace(day = 1))
    for e in ConstExpense.of(current_user):
        cur = e.start
        end = min(e.end, t)
        while cur <= end:
            expenses[time.mktime(cur.timetuple()) * 1000] += e.monthly
            cur = next_date(cur)

    expenses = list(sorted((int(k),v) for k,v in expenses.iteritems()))

    return { 'consts': expenses }