Project

General

Profile

0001-statistics-improve-label-sorting-on-group-by-71656.patch

Valentin Deniaud, 08 December 2022 03:41 PM

Download (4.58 KB)

View differences:

Subject: [PATCH] statistics: improve label sorting on group by (#71656)

 tests/api/test_statistics.py | 17 +++++++++++++++--
 wcs/statistics/views.py      | 28 +++++++++++++++++++++++-----
 2 files changed, 38 insertions(+), 7 deletions(-)
tests/api/test_statistics.py
738 738

  
739 739
    # group by item field without time interval
740 740
    resp = get_app(pub).get(sign_uri(url + '&group-by=test-item&time_interval=none'))
741
    assert resp.json['data']['x_labels'] == ['baz', 'Foo', 'None']
742
    assert resp.json['data']['series'] == [{'data': [3, 13, 4], 'label': 'Forms Count'}]
741
    # Foo is first because it has a display value, baz is second because it has not, None is always last
742
    assert resp.json['data']['x_labels'] == ['Foo', 'baz', 'None']
743
    assert resp.json['data']['series'] == [{'data': [13, 3, 4], 'label': 'Forms Count'}]
743 744

  
744 745
    # group by items field without time interval
745 746
    resp = get_app(pub).get(sign_uri(url + '&group-by=test-items&time_interval=none'))
......
751 752
    assert resp.json['data']['x_labels'] == ['Mail', 'Web']
752 753
    assert resp.json['data']['series'] == [{'data': [3, 17], 'label': 'Forms Count'}]
753 754

  
755
    # group by status without time interval
756
    resp = get_app(pub).get(sign_uri(url + '&group-by=status&time_interval=none'))
757
    assert resp.json['data']['x_labels'] == ['New status', 'End status']
758
    assert resp.json['data']['series'] == [{'data': [17, 3], 'label': 'Forms Count'}]
759

  
760
    # check statuses order
761
    formdef.workflow.possible_status = list(reversed(formdef.workflow.possible_status))
762
    formdef.workflow.store()
763
    resp = get_app(pub).get(sign_uri(url + '&group-by=status&time_interval=none'))
764
    assert resp.json['data']['x_labels'] == ['End status', 'New status']
765
    assert resp.json['data']['series'] == [{'data': [3, 17], 'label': 'Forms Count'}]
766

  
754 767
    # group by on block field is not supported
755 768
    resp = get_app(pub).get(sign_uri(url + '&group-by=blockdata_bool'))
756 769
    assert resp.json['data']['series'] == [{'data': [16, 0, 4], 'label': 'Forms Count'}]
wcs/statistics/views.py
457 457
        for group in seen_group_values:
458 458
            totals_by_group[group] = [totals.get(group) for totals in totals_by_time.values()]
459 459

  
460
        totals_by_label = self.get_totals_by_label(totals_by_group, group_labels)
461

  
460 462
        x_labels = list(totals_by_time)
461
        series = [
462
            {'label': group_labels.get(group, group), 'data': data} for group, data in totals_by_group.items()
463
        ]
463
        series = [{'label': label, 'data': data} for label, data in totals_by_label.items()]
464 464
        series.sort(key=lambda x: x['label'].lower())
465 465
        return x_labels, series
466 466

  
......
472 472
            for group in groups:
473 473
                totals_by_group[group] += total
474 474

  
475
        x_labels = [group_labels.get(group, group) for group in totals_by_group]
476
        series = [{'label': self.label, 'data': [total for total in totals_by_group.values()]}]
475
        totals_by_label = self.get_totals_by_label(totals_by_group, group_labels)
476

  
477
        x_labels = list(totals_by_label)
478
        series = [{'label': self.label, 'data': [total for total in totals_by_label.values()]}]
477 479
        return x_labels, series
478 480

  
481
    def get_totals_by_label(self, totals_by_group, group_labels):
482
        groups = list(totals_by_group)
483
        group_label_indexes = {group: i for i, group in enumerate(group_labels)}
484

  
485
        def get_group_order(group):
486
            if group is None:
487
                # None choice should always be last
488
                return len(group_label_indexes) + 1
489
            if group not in group_label_indexes:
490
                # unknown group should be last but before none
491
                return len(group_label_indexes)
492
            return group_label_indexes[group]
493

  
494
        groups.sort(key=get_group_order)
495
        return {group_labels.get(group, group): totals_by_group[group] for group in groups}
496

  
479 497

  
480 498
class CardsCountView(FormsCountView):
481 499
    formdef_class = CardDef
482
-