Projet

Général

Profil

0001-dataviz-build-filter-parameters-outside-of-spooler-6.patch

Valentin Deniaud, 23 août 2022 16:38

Télécharger (4,99 ko)

Voir les différences:

Subject: [PATCH] dataviz: build filter parameters outside of spooler (#65882)

 combo/apps/dataviz/models.py | 15 +++++++++------
 combo/utils/spooler.py       | 10 +++++-----
 tests/test_dataviz.py        | 19 ++++++++-----------
 3 files changed, 22 insertions(+), 22 deletions(-)
combo/apps/dataviz/models.py
313 313
            resp, not_found_code='statistic_data_not_found', invalid_code='statistic_url_invalid'
314 314
        )
315 315

  
316
    def get_statistic_data(self, raise_if_not_cached=False, invalidate_cache=False):
316
    def get_statistic_data(self, filter_params=None, raise_if_not_cached=False, invalidate_cache=False):
317 317
        return requests.get(
318 318
            self.statistic.url,
319
            params=self.get_filter_params(),
319
            params=filter_params or self.get_filter_params(),
320 320
            cache_duration=300,
321 321
            remote_service='auto',
322 322
            without_user=True,
......
326 326
        )
327 327

  
328 328
    def get_chart(self, width=None, height=None, raise_if_not_cached=False):
329
        transaction.on_commit(lambda: spooler.refresh_statistics_data(cell_pk=self.pk))
330
        response = self.get_statistic_data(raise_if_not_cached)
329
        filter_params = self.get_filter_params()
330
        transaction.on_commit(
331
            lambda: spooler.refresh_statistics_data(cell_pk=self.pk, filter_params=filter_params)
332
        )
333
        response = self.get_statistic_data(filter_params, raise_if_not_cached)
331 334
        response.raise_for_status()
332 335
        response = response.json()
333 336

  
......
720 723
    def available_filters(self):
721 724
        return self.statistic.filters + self.subfilters
722 725

  
723
    def update_subfilters(self):
726
    def update_subfilters(self, filter_params=None):
724 727
        self._request = get_request()
725 728
        try:
726
            response = self.get_statistic_data()
729
            response = self.get_statistic_data(filter_params=filter_params)
727 730
        except (TemplateSyntaxError, VariableDoesNotExist):
728 731
            return
729 732

  
combo/utils/spooler.py
94 94

  
95 95

  
96 96
@tenantspool
97
def refresh_statistics_data(cell_pk):
98
    from combo.apps.dataviz.models import ChartNgCell, MissingRequest, MissingVariable
97
def refresh_statistics_data(cell_pk, filter_params):
98
    from combo.apps.dataviz.models import ChartNgCell, MissingVariable
99 99

  
100 100
    try:
101 101
        cell = ChartNgCell.objects.get(pk=cell_pk)
......
103 103
        return
104 104

  
105 105
    try:
106
        cell.get_statistic_data(invalidate_cache=True)
107
    except (MissingRequest, MissingVariable):
106
        cell.get_statistic_data(invalidate_cache=True, filter_params=filter_params)
107
    except MissingVariable:
108 108
        return
109 109

  
110 110
    if cell.statistic.service_slug != 'bijoe':
111
        cell.update_subfilters()
111
        cell.update_subfilters(filter_params)
tests/test_dataviz.py
2650 2650
    page = Page.objects.create(title='One', slug='index')
2651 2651
    cell = ChartNgCell(page=page, order=1, placeholder='content')
2652 2652
    cell.statistic = Statistic.objects.get(slug='one-serie')
2653
    cell.filter_params = {'abc': 'def'}
2653 2654
    cell.save()
2654 2655

  
2655
    refresh_statistics_data(cell.pk)
2656
    refresh_statistics_data(cell.pk, filter_params={'test': 'hop'})
2656 2657
    assert len(new_api_mock.call['requests']) == 1
2657 2658

  
2658
    refresh_statistics_data(cell.pk)
2659
    assert len(new_api_mock.call['requests']) == 2
2659
    request = new_api_mock.call['requests'][0]
2660
    assert 'abc=' not in request.url
2661
    assert 'test=hop' in request.url
2660 2662

  
2661
    # variables cannot be evaluated in spooler
2662
    page.extra_variables = {'test': 'test'}
2663
    page.save()
2664
    cell.filter_params = {'ou': 'variable:test'}
2665
    cell.save()
2666
    refresh_statistics_data(cell.pk)
2663
    refresh_statistics_data(cell.pk, filter_params={'test': 'hop'})
2667 2664
    assert len(new_api_mock.call['requests']) == 2
2668 2665

  
2669 2666
    ChartNgCell.objects.all().delete()
2670
    refresh_statistics_data(cell.pk)
2667
    refresh_statistics_data(cell.pk, filter_params={'test': 'hop'})
2671 2668
    assert len(new_api_mock.call['requests']) == 2
2672 2669

  
2673 2670

  
......
2678 2675
    cell.statistic = Statistic.objects.get(slug='example')
2679 2676
    cell.save()
2680 2677

  
2681
    refresh_statistics_data(cell.pk)
2678
    refresh_statistics_data(cell.pk, filter_params=cell.get_filter_params())
2682 2679
    assert len(bijoe_mock.call['requests']) == 1
2683 2680

  
2684 2681

  
2685
-