From fc7c16ca9866c63ce15e1cc757ca186bd8b35d97 Mon Sep 17 00:00:00 2001 From: Valentin Deniaud Date: Wed, 24 Aug 2022 11:06:25 +0200 Subject: [PATCH 1/2] statistics: compute group labels sooner (#63377) --- wcs/statistics/views.py | 54 ++++++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 20 deletions(-) diff --git a/wcs/statistics/views.py b/wcs/statistics/views.py index b0a9f8f05..82274d328 100644 --- a/wcs/statistics/views.py +++ b/wcs/statistics/views.py @@ -194,6 +194,7 @@ class FormsCountView(RestrictedView): category_slug = request.GET.get('category', '_all') formdef_slug = request.GET.get('form', '_all' if self.has_global_count_support else '_nothing') group_by = request.GET.get('group-by') + group_labels = {} subfilters = [] if formdef_slug != '_all' and not formdef_slug.startswith('category:'): try: @@ -207,12 +208,7 @@ class FormsCountView(RestrictedView): totals_kwargs['criterias'].append(Equal('formdef_klass', self.formdef_class)) totals_kwargs['criterias'].append(Equal('formdef_id', formdef.id)) totals_kwargs['criterias'].extend(self.get_filters_criterias(formdef, form_page)) - if group_by: - group_by_field = self.get_group_by_field(form_page, group_by) - if group_by_field and group_by_field.type == 'status': - totals_kwargs['group_by'] = 'status' - elif group_by_field: - totals_kwargs['group_by'] = sql.get_field_id(group_by_field) + self.set_group_by_parameters(group_by, formdef, form_page, totals_kwargs, group_labels) subfilters = self.get_subfilters(form_page) else: totals_kwargs['criterias'].append(StrictNotEqual('status', 'draft')) @@ -245,7 +241,7 @@ class FormsCountView(RestrictedView): x_labels = [x[0] for x in totals] series = [{'label': self.label, 'data': [x[1] for x in totals]}] else: - x_labels, series = self.get_grouped_data(totals, group_by_field, formdef, form_page) + x_labels, series = self.get_grouped_data(totals, group_labels) return JsonResponse( {'data': {'x_labels': x_labels, 'series': series, 'subfilters': subfilters}, 'err': 0} @@ -342,7 +338,37 @@ class FormsCountView(RestrictedView): if not hasattr(fields[0], 'block_field'): # block fields are not supported return fields[0] - def get_grouped_data(self, totals, group_by_field, formdef, form_page): + def get_group_labels(self, group_by_field, formdef, form_page): + group_labels = {} + if group_by_field.type == 'status': + group_labels = {'wf-%s' % status.id: status.name for status in formdef.workflow.possible_status} + elif group_by_field.type == 'bool': + group_labels = {True: _('Yes'), False: _('No')} + elif group_by_field.type in ('item', 'items'): + options = form_page.get_item_filter_options( + group_by_field, selected_filter='all', anonymised=True + ) + group_labels = {option[0]: option[1] for option in options} + + group_labels[None] = _('None') + return group_labels + + def set_group_by_parameters(self, group_by, formdef, form_page, totals_kwargs, group_labels): + if not group_by: + return + + group_by_field = self.get_group_by_field(form_page, group_by) + if not group_by_field: + return + + if group_by_field.type == 'status': + totals_kwargs['group_by'] = 'status' + else: + totals_kwargs['group_by'] = sql.get_field_id(group_by_field) + + group_labels.update(self.get_group_labels(group_by_field, formdef, form_page)) + + def get_grouped_data(self, totals, group_labels): totals_by_time = collections.OrderedDict( # time1: {group1: total_11, group2: total_12}, # time2: {group1: total_21} @@ -369,18 +395,6 @@ class FormsCountView(RestrictedView): for group in seen_group_values: totals_by_group[group] = [totals.get(group) for totals in totals_by_time.values()] - group_labels = {} - if group_by_field.type == 'status': - group_labels = {'wf-%s' % status.id: status.name for status in formdef.workflow.possible_status} - elif group_by_field.type == 'bool': - group_labels = {True: _('Yes'), False: _('No')} - elif group_by_field.type in ('item', 'items'): - options = form_page.get_item_filter_options( - group_by_field, selected_filter='all', anonymised=True - ) - group_labels = {option[0]: option[1] for option in options} - group_labels[None] = _('None') - x_labels = list(totals_by_time) series = [ {'label': group_labels.get(group, group), 'data': data} for group, data in totals_by_group.items() -- 2.30.2