dataviz: build filter parameters outside of spooler (#65882)

This commit is contained in:
Valentin Deniaud 2022-08-23 15:37:25 +02:00 committed by Frédéric Péters
parent 99c021a4c9
commit 2bfbfca2de
3 changed files with 22 additions and 22 deletions

View File

@ -313,10 +313,10 @@ class ChartNgCell(CellBase):
resp, not_found_code='statistic_data_not_found', invalid_code='statistic_url_invalid' 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( return requests.get(
self.statistic.url, self.statistic.url,
params=self.get_filter_params(), params=filter_params or self.get_filter_params(),
cache_duration=300, cache_duration=300,
remote_service='auto', remote_service='auto',
without_user=True, without_user=True,
@ -326,8 +326,11 @@ class ChartNgCell(CellBase):
) )
def get_chart(self, width=None, height=None, raise_if_not_cached=False): 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)) filter_params = self.get_filter_params()
response = self.get_statistic_data(raise_if_not_cached) 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.raise_for_status()
response = response.json() response = response.json()
@ -724,10 +727,10 @@ class ChartNgCell(CellBase):
def available_filters(self): def available_filters(self):
return self.statistic.filters + self.subfilters return self.statistic.filters + self.subfilters
def update_subfilters(self): def update_subfilters(self, filter_params=None):
self._request = get_request() self._request = get_request()
try: try:
response = self.get_statistic_data() response = self.get_statistic_data(filter_params=filter_params)
except (TemplateSyntaxError, VariableDoesNotExist): except (TemplateSyntaxError, VariableDoesNotExist):
return return

View File

@ -94,8 +94,8 @@ def refresh_statistics_list():
@tenantspool @tenantspool
def refresh_statistics_data(cell_pk): def refresh_statistics_data(cell_pk, filter_params):
from combo.apps.dataviz.models import ChartNgCell, MissingRequest, MissingVariable from combo.apps.dataviz.models import ChartNgCell, MissingVariable
try: try:
cell = ChartNgCell.objects.get(pk=cell_pk) cell = ChartNgCell.objects.get(pk=cell_pk)
@ -103,9 +103,9 @@ def refresh_statistics_data(cell_pk):
return return
try: try:
cell.get_statistic_data(invalidate_cache=True) cell.get_statistic_data(invalidate_cache=True, filter_params=filter_params)
except (MissingRequest, MissingVariable): except MissingVariable:
return return
if cell.statistic.service_slug != 'bijoe': if cell.statistic.service_slug != 'bijoe':
cell.update_subfilters() cell.update_subfilters(filter_params)

View File

@ -2650,24 +2650,21 @@ def test_spooler_refresh_statistics_data(new_api_statistics):
page = Page.objects.create(title='One', slug='index') page = Page.objects.create(title='One', slug='index')
cell = ChartNgCell(page=page, order=1, placeholder='content') cell = ChartNgCell(page=page, order=1, placeholder='content')
cell.statistic = Statistic.objects.get(slug='one-serie') cell.statistic = Statistic.objects.get(slug='one-serie')
cell.filter_params = {'abc': 'def'}
cell.save() cell.save()
refresh_statistics_data(cell.pk) refresh_statistics_data(cell.pk, filter_params={'test': 'hop'})
assert len(new_api_mock.call['requests']) == 1 assert len(new_api_mock.call['requests']) == 1
refresh_statistics_data(cell.pk) request = new_api_mock.call['requests'][0]
assert len(new_api_mock.call['requests']) == 2 assert 'abc=' not in request.url
assert 'test=hop' in request.url
# variables cannot be evaluated in spooler refresh_statistics_data(cell.pk, filter_params={'test': 'hop'})
page.extra_variables = {'test': 'test'}
page.save()
cell.filter_params = {'ou': 'variable:test'}
cell.save()
refresh_statistics_data(cell.pk)
assert len(new_api_mock.call['requests']) == 2 assert len(new_api_mock.call['requests']) == 2
ChartNgCell.objects.all().delete() 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 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.statistic = Statistic.objects.get(slug='example')
cell.save() 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 assert len(bijoe_mock.call['requests']) == 1