From bae2a38c985eb34f9485df9ad31b4106e3b3ead0 Mon Sep 17 00:00:00 2001 From: Valentin Deniaud Date: Tue, 23 Aug 2022 15:37:25 +0200 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(-) diff --git a/combo/apps/dataviz/models.py b/combo/apps/dataviz/models.py index 32f3bb7e..05c6b91c 100644 --- a/combo/apps/dataviz/models.py +++ b/combo/apps/dataviz/models.py @@ -313,10 +313,10 @@ class ChartNgCell(CellBase): resp, not_found_code='statistic_data_not_found', invalid_code='statistic_url_invalid' ) - def get_statistic_data(self, raise_if_not_cached=False, invalidate_cache=False): + def get_statistic_data(self, filter_params=None, raise_if_not_cached=False, invalidate_cache=False): return requests.get( self.statistic.url, - params=self.get_filter_params(), + params=filter_params or self.get_filter_params(), cache_duration=300, remote_service='auto', without_user=True, @@ -326,8 +326,11 @@ class ChartNgCell(CellBase): ) def get_chart(self, width=None, height=None, raise_if_not_cached=False): - transaction.on_commit(lambda: spooler.refresh_statistics_data(cell_pk=self.pk)) - response = self.get_statistic_data(raise_if_not_cached) + filter_params = self.get_filter_params() + transaction.on_commit( + lambda: spooler.refresh_statistics_data(cell_pk=self.pk, filter_params=filter_params) + ) + response = self.get_statistic_data(filter_params, raise_if_not_cached) response.raise_for_status() response = response.json() @@ -720,10 +723,10 @@ class ChartNgCell(CellBase): def available_filters(self): return self.statistic.filters + self.subfilters - def update_subfilters(self): + def update_subfilters(self, filter_params=None): self._request = get_request() try: - response = self.get_statistic_data() + response = self.get_statistic_data(filter_params=filter_params) except (TemplateSyntaxError, VariableDoesNotExist): return diff --git a/combo/utils/spooler.py b/combo/utils/spooler.py index ac63a0c5..808bbb6d 100644 --- a/combo/utils/spooler.py +++ b/combo/utils/spooler.py @@ -94,8 +94,8 @@ def refresh_statistics_list(): @tenantspool -def refresh_statistics_data(cell_pk): - from combo.apps.dataviz.models import ChartNgCell, MissingRequest, MissingVariable +def refresh_statistics_data(cell_pk, filter_params): + from combo.apps.dataviz.models import ChartNgCell, MissingVariable try: cell = ChartNgCell.objects.get(pk=cell_pk) @@ -103,9 +103,9 @@ def refresh_statistics_data(cell_pk): return try: - cell.get_statistic_data(invalidate_cache=True) - except (MissingRequest, MissingVariable): + cell.get_statistic_data(invalidate_cache=True, filter_params=filter_params) + except MissingVariable: return if cell.statistic.service_slug != 'bijoe': - cell.update_subfilters() + cell.update_subfilters(filter_params) diff --git a/tests/test_dataviz.py b/tests/test_dataviz.py index 8ef5b5ca..f2d0b58b 100644 --- a/tests/test_dataviz.py +++ b/tests/test_dataviz.py @@ -2650,24 +2650,21 @@ def test_spooler_refresh_statistics_data(new_api_statistics): page = Page.objects.create(title='One', slug='index') cell = ChartNgCell(page=page, order=1, placeholder='content') cell.statistic = Statistic.objects.get(slug='one-serie') + cell.filter_params = {'abc': 'def'} cell.save() - refresh_statistics_data(cell.pk) + refresh_statistics_data(cell.pk, filter_params={'test': 'hop'}) assert len(new_api_mock.call['requests']) == 1 - refresh_statistics_data(cell.pk) - assert len(new_api_mock.call['requests']) == 2 + request = new_api_mock.call['requests'][0] + assert 'abc=' not in request.url + assert 'test=hop' in request.url - # variables cannot be evaluated in spooler - page.extra_variables = {'test': 'test'} - page.save() - cell.filter_params = {'ou': 'variable:test'} - cell.save() - refresh_statistics_data(cell.pk) + refresh_statistics_data(cell.pk, filter_params={'test': 'hop'}) assert len(new_api_mock.call['requests']) == 2 ChartNgCell.objects.all().delete() - refresh_statistics_data(cell.pk) + refresh_statistics_data(cell.pk, filter_params={'test': 'hop'}) assert len(new_api_mock.call['requests']) == 2 @@ -2678,7 +2675,7 @@ def test_spooler_refresh_statistics_data_bijoe(statistics): cell.statistic = Statistic.objects.get(slug='example') cell.save() - refresh_statistics_data(cell.pk) + refresh_statistics_data(cell.pk, filter_params=cell.get_filter_params()) assert len(bijoe_mock.call['requests']) == 1 -- 2.30.2