Fix bug in history

This commit is contained in:
Edgar P. Burkhart 2024-12-30 19:20:53 +01:00
parent 886e682650
commit 7109142b4e
Signed by: edpibu
GPG key ID: 9833D3C5A25BD227
2 changed files with 31 additions and 21 deletions

View file

@ -9,6 +9,8 @@ register = template.Library()
@register.simple_tag @register.simple_tag
def calendar_opacity(v, vmax): def calendar_opacity(v, vmax):
if v is None:
return "0%"
return f"{math.sin(min(1, math.fabs(v/vmax))*math.pi/2):.0%}" return f"{math.sin(min(1, math.fabs(v/vmax))*math.pi/2):.0%}"
@ -29,6 +31,8 @@ def empty_calendar_cells_end(n):
@register.simple_tag @register.simple_tag
def up_down_icon(val): def up_down_icon(val):
if val is None:
return ""
if val > 0: if val > 0:
return remix("arrow-up-s", "green") return remix("arrow-up-s", "green")
elif val < 0: elif val < 0:
@ -41,15 +45,18 @@ def up_down_icon(val):
def plot_bar(s, sum_pm, s_max): def plot_bar(s, sum_pm, s_max):
_res = "" _res = ""
if sum_pm: if s_max:
_w = abs(sum_pm / s_max) if sum_pm:
_res += f"""<div style="width: {_w:.1%}"></div>""" _w = abs(sum_pm / s_max)
if sum_pm is not None and s * sum_pm > 0: _res += f"""<div style="width: {_w:.1%}"></div>"""
_w = abs(s / s_max) if sum_pm is not None and s * sum_pm > 0:
_res += ( _w = abs(s / s_max)
f"""<div class="tot" style="width: {_w:.1%}">""" _res += (
f"""<span>{pmrvalue(s)}</span></div>""" f"""<div class="tot" style="width: {_w:.1%}">"""
) f"""<span>{pmrvalue(s)}</span></div>"""
)
else:
_res += "<div></div>"
return mark_safe(_res) return mark_safe(_res)
@ -64,4 +71,4 @@ def calendar_head():
@register.filter @register.filter
def sum_year(y_data): def sum_year(y_data):
return sum(y["sum"] for y in y_data) return sum(y["sum"] or 0 for y in y_data)

View file

@ -1,7 +1,7 @@
import datetime import datetime
from django.db.models import Q, Sum from django.db.models import Q, Sum
from django.db.models.functions import Abs, TruncMonth from django.db.models.functions import Abs, Greatest, TruncMonth
def history(transaction_set): def history(transaction_set):
@ -14,15 +14,19 @@ def history(transaction_set):
_first_month = _transaction_month.last()["month"] _first_month = _transaction_month.last()["month"]
_last_month = _transaction_month.first()["month"] _last_month = _transaction_month.first()["month"]
_history = _transaction_month.annotate( _history = (
sum_p=Sum("value", filter=Q(value__gt=0)), _transaction_month.annotate(
sum_m=Sum("value", filter=Q(value__lt=0)), sum_p=Sum("value", filter=Q(value__gt=0), default=0),
sum=Sum("value"), sum_m=Sum("value", filter=Q(value__lt=0), default=0),
).order_by("-month") sum=Sum("value"),
)
.annotate(max_sum=Greatest("sum_p", Abs("sum_m")))
.order_by("-month")
)
_data = [ _data = [
_history.filter(month=datetime.date(y, m + 1, 1)).first() _history.filter(month=datetime.date(y, m + 1, 1)).first()
or {"month": datetime.date(y, m + 1, 1), "sum": 0} or {"month": datetime.date(y, m + 1, 1), "sum": None}
for y in range( for y in range(
_first_month.year, _first_month.year,
_last_month.year + 1, _last_month.year + 1,
@ -37,10 +41,9 @@ def history(transaction_set):
"data": _data, "data": _data,
"max": { "max": {
"pm": 125 "pm": 125
* max( * _history.order_by("-max_sum")[len(_history.exclude(max_sum=0)) // 10][
_history.order_by("-sum_p")[len(_history) // 10]["sum_p"], "max_sum"
_history.order_by("-sum_m")[len(_history) // 10]["sum_m"], ]
)
/ 100, / 100,
"sum": 125 "sum": 125
* _history.annotate(abs_sum=Abs("sum")).order_by("-abs_sum")[ * _history.annotate(abs_sum=Abs("sum")).order_by("-abs_sum")[