Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] Fixed slow query for WifiSession dashboard pie chart #528 #542

Merged
merged 3 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 6 additions & 15 deletions openwisp_monitoring/device/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django.apps import AppConfig
from django.conf import settings
from django.core.cache import cache
from django.db.models import Case, Count, Sum, When
from django.db.models import Count
from django.db.models.signals import post_delete, post_save
from django.urls import reverse_lazy
from django.utils.translation import gettext_lazy as _
Expand Down Expand Up @@ -350,30 +350,21 @@ def register_dashboard_items(self):
'query_params': {
'app_label': WifiSession._meta.app_label,
'model': WifiSession._meta.model_name,
'annotate': {
'active': Count(
Case(
When(
stop_time__isnull=True,
then=1,
)
)
),
},
'filter': {'stop_time__isnull': True},
'aggregate': {
'active__sum': Sum('active'),
'active__count': Count('id'),
},
'organization_field': 'device__organization_id',
},
'filters': {
'key': 'stop_time__isnull',
'active__sum': 'true',
'active__count': 'true',
},
'colors': {
'active__sum': '#267126',
'active__count': '#267126',
},
'labels': {
'active__sum': _('Currently Active WiFi Sessions'),
'active__count': _('Currently Active WiFi Sessions'),
},
'quick_link': {
'url': reverse_lazy(
Expand Down
17 changes: 17 additions & 0 deletions openwisp_monitoring/device/tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.contrib.auth.models import Permission
from django.contrib.contenttypes.forms import generic_inlineformset_factory
from django.core.cache import cache
from django.db import connection
from django.test import TestCase
from django.urls import reverse
from django.utils.timezone import datetime, now, timedelta
Expand Down Expand Up @@ -118,6 +119,22 @@ def test_dashboard_map_on_index(self):
self.assertContains(response, "geoJsonUrl: \'http://testserver/api")
self.assertContains(response, "locationDeviceUrl: \'http://testserver/api")

def test_wifisession_dashboard_chart_query(self):
url = reverse('admin:index')
with self.assertNumQueries(10):
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
device_monitoring_app_label = WifiSession._meta.app_label
for query in connection.queries:
if query['sql'] == (
f'SELECT COUNT("{device_monitoring_app_label}_wifisession"."id") '
f'AS "active__count" FROM "{device_monitoring_app_label}_wifisession" '
f'WHERE "{device_monitoring_app_label}_wifisession"."stop_time" IS NULL'
):
break
else:
self.fail('WiFiSession dashboard query not found')

def test_status_data(self):
d = self._create_device(organization=self._create_org())
data = self._data()
Expand Down
Loading