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'
)
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()
@ -724,10 +727,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

View File

@ -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)

View File

@ -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