Fewer queries on stats of an account balance.

- Remove labels, should be replaced to an anchor to the relative
  operation in history.
- Add select_related as necessary.
This commit is contained in:
Aurélien Delobelle 2017-04-11 23:13:54 +02:00
parent b5cc26bb1b
commit e97e0081d7

View file

@ -2219,10 +2219,13 @@ class AccountStatBalance(PkUrlMixin, JSONDetailView):
# prepare querysets
# TODO: retirer les opgroup dont tous les op sont annulées
opegroups = OperationGroup.objects.filter(on_acc=account)
recv_transfers = Transfer.objects.filter(to_acc=account,
canceled_at=None)
sent_transfers = Transfer.objects.filter(from_acc=account,
canceled_at=None)
transfers = (
Transfer.objects
.filter(canceled_at=None)
.select_related('group')
)
recv_transfers = transfers.filter(to_acc=account)
sent_transfers = transfers.filter(from_acc=account)
# apply filters
if begin_date is not None:
@ -2250,13 +2253,11 @@ class AccountStatBalance(PkUrlMixin, JSONDetailView):
actions.append({
'at': (begin_date or account.created_at).isoformat(),
'amount': 0,
'label': 'début',
'balance': 0,
})
actions.append({
'at': (end_date or timezone.now()).isoformat(),
'amount': 0,
'label': 'fin',
'balance': 0,
})
@ -2264,21 +2265,18 @@ class AccountStatBalance(PkUrlMixin, JSONDetailView):
{
'at': ope_grp.at.isoformat(),
'amount': ope_grp.amount,
'label': str(ope_grp),
'balance': 0,
} for ope_grp in opegroups
] + [
{
'at': tr.group.at.isoformat(),
'amount': tr.amount,
'label': str(tr),
'balance': 0,
} for tr in recv_transfers
] + [
{
'at': tr.group.at.isoformat(),
'amount': -tr.amount,
'label': str(tr),
'balance': 0,
} for tr in sent_transfers
]