Reduce computations in history generation
This commit is contained in:
parent
9dbbd3d48e
commit
6f631607b8
5 changed files with 63 additions and 69 deletions
|
@ -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 %}
|
||||||
|
|
|
@ -19,7 +19,9 @@
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
{% spaceless %}
|
{% spaceless %}
|
||||||
{% for date in history.data %}
|
{% for y in history.years reversed %}
|
||||||
|
{% for date in y.d reversed %}
|
||||||
|
{% if date %}
|
||||||
<tr>
|
<tr>
|
||||||
<td class="icon">
|
<td class="icon">
|
||||||
<span class="ri-{% if date.sum > 0 %}arrow-up-s-line green{% elif date.sum < 0 %}arrow-down-s-line red{% endif %}"></span>
|
<span class="ri-{% if date.sum > 0 %}arrow-up-s-line green{% elif date.sum < 0 %}arrow-down-s-line red{% endif %}"></span>
|
||||||
|
@ -56,6 +58,12 @@
|
||||||
</td>
|
</td>
|
||||||
<td class="value">{{ date.sum_p|pmrvalue }}</td>
|
<td class="value">{{ date.sum_p|pmrvalue }}</td>
|
||||||
</tr>
|
</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 }}
|
||||||
|
|
|
@ -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(
|
||||||
|
|
|
@ -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 {
|
||||||
|
|
|
@ -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 %}
|
||||||
|
|
Loading…
Reference in a new issue