diff --git a/nummi/history/utils.py b/nummi/history/utils.py index 3606959..c61fc88 100644 --- a/nummi/history/utils.py +++ b/nummi/history/utils.py @@ -1,6 +1,6 @@ import datetime -from django.db.models import Avg, Q, StdDev, Sum +from django.db.models import Q, Sum from django.db.models.functions import Abs, TruncMonth @@ -36,12 +36,16 @@ def history(transaction_set): return { "data": _data, "max": { - "pm": max( - _history.aggregate( - max=Avg("sum_p", default=0) + StdDev("sum_p", default=0), - min=Avg("sum_m", default=0) - StdDev("sum_m", default=0), - ).values(), - ), - "sum": _history.aggregate(max=Avg(Abs("sum")) + StdDev(Abs("sum")))["max"], + "pm": 125 + * max( + _history.order_by("-sum_p")[len(_history) // 10]["sum_p"], + _history.order_by("-sum_m")[len(_history) // 10]["sum_m"], + ) + / 100, + "sum": 125 + * _history.annotate(abs_sum=Abs("sum")).order_by("-abs_sum")[ + len(_history) // 10 + ]["abs_sum"] + / 100, }, }