Projet

Général

Profil

0004-dataviz-refresh-statistics-list-more-frequently-usin.patch

Valentin Deniaud, 17 février 2021 11:47

Télécharger (3,29 ko)

Voir les différences:

Subject: [PATCH 4/4] dataviz: refresh statistics list more frequently using
 spooler (#50891)

 combo/apps/dataviz/forms.py  | 16 +++++++++++++++-
 combo/apps/dataviz/models.py |  3 +++
 combo/utils/spooler.py       | 22 ++++++++++++++++++++++
 3 files changed, 40 insertions(+), 1 deletion(-)
combo/apps/dataviz/forms.py
14 14
# You should have received a copy of the GNU Affero General Public License
15 15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 16

  
17
import sys
17 18
from collections import OrderedDict
18 19

  
19 20
from django import forms
20 21
from django.conf import settings
22
from django.db import transaction, connection
21 23
from django.db.models import Q
22 24

  
23
from combo.utils import requests
25
from combo.utils import requests, cache_during_request
24 26

  
25 27
from .models import ChartCell, ChartNgCell
26 28

  
......
45 47
        self.fields['url'].widget = forms.Select(choices=available_charts)
46 48

  
47 49

  
50
@cache_during_request
51
def trigger_statistics_list_refresh():
52
    if 'uwsgi' in sys.modules:
53
        from combo.utils.spooler import refresh_statistics_list
54

  
55
        tenant = getattr(connection, 'tenant', None)
56
        transaction.on_commit(
57
            lambda: refresh_statistics_list.spool(domain=getattr(tenant, 'domain_url', None))
58
        )
59

  
60

  
48 61
class ChartNgForm(forms.ModelForm):
49 62
    blank_choice = ('', '---------')
50 63

  
......
67 80
        }
68 81

  
69 82
    def __init__(self, *args, **kwargs):
83
        trigger_statistics_list_refresh()
70 84
        super().__init__(*args, **kwargs)
71 85

  
72 86
        field_ids = list(self._meta.fields)
combo/apps/dataviz/models.py
161 161
        null=True,
162 162
        on_delete=models.SET_NULL,
163 163
        related_name='cells',
164
        help_text=_(
165
            'This list may take a few seconds to be updated, please refresh the page if an item is missing.'
166
        ),
164 167
    )
165 168
    filter_params = JSONField(default=dict)
166 169
    title = models.CharField(_('Title'), max_length=150, blank=True)
combo/utils/spooler.py
13 13
#
14 14
# You should have received a copy of the GNU Affero General Public License
15 15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16

  
17
from django.db import connection
18

  
19
from uwsgidecorators import spool
20

  
21
from combo.apps.dataviz.utils import update_available_statistics
22

  
23

  
24
def set_connection(domain):
25
    from hobo.multitenant.middleware import TenantMiddleware
26

  
27
    tenant = TenantMiddleware.get_tenant_by_hostname(domain)
28
    connection.set_tenant(tenant)
29

  
30

  
31
@spool
32
def refresh_statistics_list(args):
33
    if args.get('domain'):
34
        # multitenant installation
35
        set_connection(args['domain'])
36

  
37
    update_available_statistics()
16
-