diff --git a/openwisp_monitoring/device/apps.py b/openwisp_monitoring/device/apps.py index 74b78c213..fb6685ff6 100644 --- a/openwisp_monitoring/device/apps.py +++ b/openwisp_monitoring/device/apps.py @@ -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 _ @@ -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( diff --git a/openwisp_monitoring/device/tests/test_admin.py b/openwisp_monitoring/device/tests/test_admin.py index c156ccc96..679685412 100644 --- a/openwisp_monitoring/device/tests/test_admin.py +++ b/openwisp_monitoring/device/tests/test_admin.py @@ -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 @@ -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()