Improve history view using tenth centile times 125% for min max

This commit is contained in:
Edgar P. Burkhart 2024-12-30 17:56:55 +01:00
parent 28ac9c8ef7
commit 5d6bd9ea2b
Signed by: edpibu
GPG key ID: 9833D3C5A25BD227

View file

@ -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,
},
}