diff --git a/combo/apps/dataviz/models.py b/combo/apps/dataviz/models.py index 4f15e9bb..f75e26d7 100644 --- a/combo/apps/dataviz/models.py +++ b/combo/apps/dataviz/models.py @@ -16,11 +16,12 @@ import copy import os +import sys from datetime import date from requests.exceptions import RequestException from django.urls import reverse -from django.db import models +from django.db import models, connection, transaction from django.utils import timezone from django.utils.encoding import force_text from django.utils.translation import ugettext_lazy as _, ungettext, gettext @@ -281,7 +282,7 @@ class ChartNgCell(CellBase): ctx['table'] = ctx['table'].replace('', '
') return ctx - def get_statistic_data(self, raise_if_not_cached=False): + def get_statistic_data(self, raise_if_not_cached=False, invalidate_cache=False): return requests.get( self.statistic.url, params=self.get_filter_params(), @@ -290,9 +291,19 @@ class ChartNgCell(CellBase): without_user=True, raise_if_not_cached=raise_if_not_cached, log_errors=False, + invalidate_cache=invalidate_cache, ) def get_chart(self, width=None, height=None, raise_if_not_cached=False): + if 'uwsgi' in sys.modules: + from combo.utils.spooler import refresh_statistics_data + + tenant = getattr(connection, 'tenant', None) + transaction.on_commit( + lambda: refresh_statistics_data.spool( + cell_pk=str(self.pk), domain=getattr(tenant, 'domain_url', None) + ) + ) response = self.get_statistic_data(raise_if_not_cached) response.raise_for_status() response = response.json() diff --git a/combo/utils/spooler.py b/combo/utils/spooler.py index 0e9e813c..ad42b220 100644 --- a/combo/utils/spooler.py +++ b/combo/utils/spooler.py @@ -18,7 +18,9 @@ from django.db import connection from uwsgidecorators import spool +from combo.apps.dataviz.models import ChartNgCell from combo.apps.dataviz.utils import update_available_statistics +from combo.utils import requests def set_connection(domain): @@ -35,3 +37,13 @@ def refresh_statistics_list(args): set_connection(args['domain']) update_available_statistics() + + +@spool +def refresh_statistics_data(args): + if args.get('domain'): + # multitenant installation + set_connection(args['domain']) + + cell = ChartNgCell.objects.get(pk=args['cell_pk']) + cell.get_statistic_data(invalidate_cache=True)