From 5d6bd9ea2b36dda56fa991464dd1d6c351b36c03 Mon Sep 17 00:00:00 2001 From: "Edgar P. Burkhart" Date: Mon, 30 Dec 2024 17:56:55 +0100 Subject: [PATCH] Improve history view using tenth centile times 125% for min max --- nummi/history/utils.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) 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, }, }