forked from DGNum/gestioCOF
Fewer queries on stats/scales + Fix
Scales: - Fix #chunks when used with std_chunk=True (there was one too many at the beginning) - Scale.end gives the end of the last chunk (instead of its start) So scale.begin -> scale.end gives the full range of the scale. `kfet_day` now returns an aware datetime. ScaleMixin: - new method `get_by_chunks` which use only one query and ranks elements according to the scale. Elements are returned by a generator for each scale chunk (and all chunks are returned as a generator too). ArticlesStatSales and AccountStatOperations use this new method to avoid issuing #scale_chunks queries. ArticleStat: - fixed on Chrome
This commit is contained in:
parent
e97e0081d7
commit
3f4a1adbb9
4 changed files with 140 additions and 32 deletions
|
@ -2369,13 +2369,19 @@ class AccountStatOperation(ScaleMixin, PkUrlMixin, JSONDetailView):
|
|||
# à l'article en question et qui ne sont pas annulées
|
||||
# puis on choisi pour chaques intervalle les opérations
|
||||
# effectuées dans ces intervalles de temps
|
||||
all_operations = (Operation.objects
|
||||
.filter(group__on_acc=self.object)
|
||||
.filter(canceled_at=None)
|
||||
)
|
||||
all_operations = (
|
||||
Operation.objects
|
||||
.filter(group__on_acc=self.object,
|
||||
canceled_at=None)
|
||||
.values('article_nb', 'group__at')
|
||||
.order_by('group__at')
|
||||
)
|
||||
if types is not None:
|
||||
all_operations = all_operations.filter(type__in=types)
|
||||
chunks = self.chunkify_qs(all_operations, scale, field='group__at')
|
||||
chunks = self.get_by_chunks(
|
||||
all_operations, scale, field_db='group__at',
|
||||
field_callback=(lambda d: d['group__at']),
|
||||
)
|
||||
return chunks
|
||||
|
||||
def get_context_data(self, *args, **kwargs):
|
||||
|
@ -2391,7 +2397,8 @@ class AccountStatOperation(ScaleMixin, PkUrlMixin, JSONDetailView):
|
|||
# On compte les opérations
|
||||
nb_ventes = []
|
||||
for chunk in operations:
|
||||
nb_ventes.append(tot_ventes(chunk))
|
||||
ventes = sum(ope['article_nb'] for ope in chunk)
|
||||
nb_ventes.append(ventes)
|
||||
|
||||
context['charts'] = [{"color": "rgb(255, 99, 132)",
|
||||
"label": "NB items achetés",
|
||||
|
@ -2442,29 +2449,39 @@ class ArticleStatSales(ScaleMixin, JSONDetailView):
|
|||
context = {'labels': old_ctx['labels']}
|
||||
scale = self.scale
|
||||
|
||||
# On selectionne les opérations qui correspondent
|
||||
# à l'article en question et qui ne sont pas annulées
|
||||
# puis on choisi pour chaques intervalle les opérations
|
||||
# effectuées dans ces intervalles de temps
|
||||
all_operations = (
|
||||
all_purchases = (
|
||||
Operation.objects
|
||||
.filter(type=Operation.PURCHASE,
|
||||
article=self.object,
|
||||
canceled_at=None,
|
||||
)
|
||||
.filter(
|
||||
type=Operation.PURCHASE,
|
||||
article=self.object,
|
||||
canceled_at=None,
|
||||
)
|
||||
.values('group__at', 'article_nb')
|
||||
.order_by('group__at')
|
||||
)
|
||||
chunks = self.chunkify_qs(all_operations, scale, field='group__at')
|
||||
liq_only = all_purchases.filter(group__on_acc__trigramme='LIQ')
|
||||
liq_exclude = all_purchases.exclude(group__on_acc__trigramme='LIQ')
|
||||
|
||||
chunks_liq = self.get_by_chunks(
|
||||
liq_only, scale, field_db='group__at',
|
||||
field_callback=lambda d: d['group__at'],
|
||||
)
|
||||
chunks_no_liq = self.get_by_chunks(
|
||||
liq_exclude, scale, field_db='group__at',
|
||||
field_callback=lambda d: d['group__at'],
|
||||
)
|
||||
|
||||
# On compte les opérations
|
||||
nb_ventes = []
|
||||
nb_accounts = []
|
||||
nb_liq = []
|
||||
for qs in chunks:
|
||||
nb_ventes.append(
|
||||
tot_ventes(qs))
|
||||
nb_liq.append(
|
||||
tot_ventes(qs.filter(group__on_acc__trigramme='LIQ')))
|
||||
nb_accounts.append(
|
||||
tot_ventes(qs.exclude(group__on_acc__trigramme='LIQ')))
|
||||
for chunk_liq, chunk_no_liq in zip(chunks_liq, chunks_no_liq):
|
||||
sum_accounts = sum(ope['article_nb'] for ope in chunk_no_liq)
|
||||
sum_liq = sum(ope['article_nb'] for ope in chunk_liq)
|
||||
nb_ventes.append(sum_accounts + sum_liq)
|
||||
nb_accounts.append(sum_accounts)
|
||||
nb_liq.append(sum_liq)
|
||||
|
||||
context['charts'] = [{"color": "rgb(255, 99, 132)",
|
||||
"label": "Toutes consommations",
|
||||
"values": nb_ventes},
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue