summaryrefslogtreecommitdiff
path: root/app/views/stats.py
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--app/views/stats.py17
1 files changed, 12 insertions, 5 deletions
diff --git a/app/views/stats.py b/app/views/stats.py
index 83c8154..639f73b 100644
--- a/app/views/stats.py
+++ b/app/views/stats.py
@@ -4,10 +4,11 @@ from . import Blueprint, flash, db, \
today
from .. import forms as F
-from ..model import ConstExpense
+from ..model import ConstExpense, SingleExpense
import sqlalchemy as sql
import calendar
from collections import defaultdict
+from datetime import date
from flask import jsonify
mod = Blueprint('stats', __name__)
@@ -35,15 +36,21 @@ def const_dialog(year,month):
@templated
def show():
# easy way: fetch them all and then do some computation
- expenses = defaultdict(int)
+ consts = defaultdict(int)
t = today().replace(day = 1)
for e in ConstExpense.of(current_user):
cur = e.start
end = min(e.end, t)
while cur <= end:
- expenses[date_to_ms(cur)] += e.monthly
+ consts[date_to_ms(cur)] += e.monthly
cur = next_date(cur)
- expenses = list(sorted(expenses.iteritems()))
+ consts = list(sorted(consts.iteritems()))
- return { 'consts': expenses }
+ expQuery = SingleExpense.of(current_user)\
+ .group_by(SingleExpense.year, SingleExpense.month)\
+ .values(SingleExpense.year, SingleExpense.month, sql.func.sum(SingleExpense.expense))
+
+ expenses = list(sorted((date_to_ms(date(year,month,1)), exp) for (year, month, exp) in expQuery))
+
+ return { 'consts': consts, 'expenses' : expenses }