diff options
author | René 'Necoro' Neumann <necoro@necoro.net> | 2014-01-17 00:36:10 +0100 |
---|---|---|
committer | René 'Necoro' Neumann <necoro@necoro.net> | 2014-01-17 00:36:10 +0100 |
commit | 7dc8868361e0f174e211c9a3d158082df5c21a3c (patch) | |
tree | dd3fe351f8c953fe4756a6774bed32fe7467e0de /app/views/stats.py | |
parent | f7d4c3c2c8bbcad07c41498e71c5a311f01e0f4b (diff) | |
download | kosten-7dc8868361e0f174e211c9a3d158082df5c21a3c.tar.gz kosten-7dc8868361e0f174e211c9a3d158082df5c21a3c.tar.bz2 kosten-7dc8868361e0f174e211c9a3d158082df5c21a3c.zip |
First statistics :)
Diffstat (limited to '')
-rw-r--r-- | app/views/stats.py | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/app/views/stats.py b/app/views/stats.py new file mode 100644 index 0000000..b1e9b1a --- /dev/null +++ b/app/views/stats.py @@ -0,0 +1,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 } |