Reduce computations in history generation

This commit is contained in:
Edgar P. Burkhart 2023-12-28 16:31:13 +01:00
parent 9dbbd3d48e
commit 6f631607b8
Signed by: edpibu
GPG Key ID: 9833D3C5A25BD227
5 changed files with 63 additions and 69 deletions

View File

@ -11,7 +11,7 @@
{% block tables %} {% block tables %}
<h3>{% translate "Transactions" %}</h3> <h3>{% translate "Transactions" %}</h3>
{% include "transaction/transaction_table.html" %} {% include "transaction/transaction_table.html" %}
{% if history.data %} {% if history %}
<h3>{% translate "History" %}</h3> <h3>{% translate "History" %}</h3>
{% include "history/plot.html" %} {% include "history/plot.html" %}
{% endif %} {% endif %}

View File

@ -19,43 +19,51 @@
</thead> </thead>
<tbody> <tbody>
{% spaceless %} {% spaceless %}
{% for date in history.data %} {% for y in history.years reversed %}
<tr> {% for date in y.d reversed %}
<td class="icon"> {% if date %}
<span class="ri-{% if date.sum > 0 %}arrow-up-s-line green{% elif date.sum < 0 %}arrow-down-s-line red{% endif %}"></span> <tr>
</td> <td class="icon">
<th class="date" scope="row"> <span class="ri-{% if date.sum > 0 %}arrow-up-s-line green{% elif date.sum < 0 %}arrow-down-s-line red{% endif %}"></span>
{% if date.has_transactions %} </td>
{% if account %} <th class="date" scope="row">
<a href="{% url "transaction_month" account=account.pk year=date.month.year month=date.month.month %}">{{ date.month|date:"Y-m" }}</a> {% if date.has_transactions %}
{% elif category %} {% if account %}
<a href="{% url "transaction_month" category=category.pk year=date.month.year month=date.month.month %}">{{ date.month|date:"Y-m" }}</a> <a href="{% url "transaction_month" account=account.pk year=date.month.year month=date.month.month %}">{{ date.month|date:"Y-m" }}</a>
{% else %} {% elif category %}
<a href="{% url "transaction_month" year=date.month.year month=date.month.month %}">{{ date.month|date:"Y-m" }}</a> <a href="{% url "transaction_month" category=category.pk year=date.month.year month=date.month.month %}">{{ date.month|date:"Y-m" }}</a>
{% endif %} {% else %}
{% else %} <a href="{% url "transaction_month" year=date.month.year month=date.month.month %}">{{ date.month|date:"Y-m" }}</a>
{{ date.month|date:"Y-m" }} {% endif %}
{% endif %} {% else %}
</th> {{ date.month|date:"Y-m" }}
<td class="value">{{ date.sum_m|pmrvalue }}</td> {% endif %}
<td class="bar m"> </th>
<div style="width: {% widthratio date.sum_m history.max -100 %}%"></div> <td class="value">{{ date.sum_m|pmrvalue }}</td>
{% if date.sum < 0 %} <td class="bar m">
<div class="tot" style="width:{% widthratio date.sum history.max -100 %}%"> <div style="width: {% widthratio date.sum_m history.max -100 %}%"></div>
<span>{{ date.sum|pmrvalue }}</span> {% if date.sum < 0 %}
</div> <div class="tot" style="width:{% widthratio date.sum history.max -100 %}%">
{% endif %} <span>{{ date.sum|pmrvalue }}</span>
</td> </div>
<td class="bar p"> {% endif %}
<div style="width: {% widthratio date.sum_p history.max 100 %}%"></div> </td>
{% if date.sum > 0 %} <td class="bar p">
<div class="tot" style="width:{% widthratio date.sum history.max 100 %}%"> <div style="width: {% widthratio date.sum_p history.max 100 %}%"></div>
<span>{{ date.sum|pmrvalue }}</span> {% if date.sum > 0 %}
</div> <div class="tot" style="width:{% widthratio date.sum history.max 100 %}%">
{% endif %} <span>{{ date.sum|pmrvalue }}</span>
</td> </div>
<td class="value">{{ date.sum_p|pmrvalue }}</td> {% endif %}
</tr> </td>
<td class="value">{{ date.sum_p|pmrvalue }}</td>
</tr>
{% else %}
<tr class="empty">
<td colspan="5" class="empty"></td>
</tr>
{% endif %}
{% endfor %}
{% endfor %} {% endfor %}
{% endspaceless %} {% endspaceless %}
</tbody> </tbody>
@ -82,7 +90,7 @@
</thead> </thead>
<tbody> <tbody>
{% spaceless %} {% spaceless %}
{% for year in history.years %} {% for year in history.years reversed %}
<tr> <tr>
<th>{{ year.y }}</th> <th>{{ year.y }}</th>
{% for m in year.d %} {% for m in year.d %}
@ -100,3 +108,4 @@
</tbody> </tbody>
</table> </table>
</div> </div>
{{ history.years|json_script }}

View File

@ -1,8 +1,7 @@
import datetime import datetime
from django.db import models
from django.db.models import Func, Max, Min, Q, Sum, Value from django.db.models import Func, Max, Min, Q, Sum, Value
from django.db.models.functions import Abs, Now, TruncMonth from django.db.models.functions import Abs, TruncMonth
class GenerateMonth(Func): class GenerateMonth(Func):
@ -13,31 +12,13 @@ class GenerateMonth(Func):
def history(transaction_set): def history(transaction_set):
if not transaction_set.exists(): if not transaction_set.exists():
return None return None
_transaction_month = transaction_set.values(month=TruncMonth("date")).order_by( _transaction_month = transaction_set.values(month=TruncMonth("date")).order_by(
"-date" "-date"
) )
_months = ( _first_month = _transaction_month.last()["month"]
transaction_set.values( _last_month = _transaction_month.first()["month"]
month=GenerateMonth(
_transaction_month.last()["month"],
Now(output_field=models.DateField()),
)
)
.annotate(
sum_m=Value(0),
sum_p=Value(0),
sum=Value(0),
has_transactions=Value(0),
)
.difference(
_transaction_month.annotate(
sum_m=Value(0),
sum_p=Value(0),
sum=Value(0),
has_transactions=Value(0),
)
)
)
_history = _transaction_month.annotate( _history = _transaction_month.annotate(
sum_p=Sum("value", filter=Q(value__gt=0)), sum_p=Sum("value", filter=Q(value__gt=0)),
sum_m=Sum("value", filter=Q(value__lt=0)), sum_m=Sum("value", filter=Q(value__lt=0)),
@ -46,19 +27,20 @@ def history(transaction_set):
).order_by("-month") ).order_by("-month")
return { return {
"data": _history.union(_months).order_by("-month"),
"years": [ "years": [
{ {
"y": y, "y": y,
"d": [ "d": [
_history.filter(month=datetime.date(y, m + 1, 1)).first() _history.filter(month=datetime.date(y, m + 1, 1)).first()
for m in range(12) for m in range(
0,
_last_month.month if _last_month.year == y else 12,
)
], ],
} }
for y in range( for y in range(
_transaction_month.first()["month"].year, _first_month.year,
_transaction_month.last()["month"].year - 1, _last_month.year + 1,
-1,
) )
], ],
"max": max( "max": max(

View File

@ -63,6 +63,9 @@ table.full-width col.bar {
.plot td.bar.m div.tot span { .plot td.bar.m div.tot span {
right: 0; right: 0;
} }
.plot tr.empty {
height: 0.5rem;
}
@media (width < 720px) { @media (width < 720px) {
.plot .bar { .plot .bar {

View File

@ -40,7 +40,7 @@
<h2>{% translate "Statements" %}</h2> <h2>{% translate "Statements" %}</h2>
{% include "statement/statement_table.html" %} {% include "statement/statement_table.html" %}
{% endif %} {% endif %}
{% if history.data %} {% if history %}
<h2>{% translate "History" %}</h2> <h2>{% translate "History" %}</h2>
{% include "history/plot.html" %} {% include "history/plot.html" %}
{% endif %} {% endif %}