From 67bc8867e677b21de2d9c342670fd16fffb00b8d Mon Sep 17 00:00:00 2001 From: Federico Capoano Date: Tue, 13 Sep 2022 13:59:29 +0200 Subject: [PATCH] [fix] Fixed mobile signal missing case In some cases, the mobile signal info may be missing, therefore we need the code to be resilient and not crash in this case (simply do not try to write mobile signal data). --- openwisp_monitoring/device/api/views.py | 4 +++ openwisp_monitoring/device/tests/test_api.py | 36 ++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/openwisp_monitoring/device/api/views.py b/openwisp_monitoring/device/api/views.py index 04bac7890..e8d1055ab 100644 --- a/openwisp_monitoring/device/api/views.py +++ b/openwisp_monitoring/device/api/views.py @@ -207,6 +207,8 @@ def _get_extra_tags(self, device): return tags def _get_mobile_signal_type(self, signal): + if not signal: + return # if only one access technology in use, return that sections = list(signal.keys()) if len(sections) == 1: @@ -221,6 +223,8 @@ def _get_mobile_signal_type(self, signal): def _write_mobile_signal(self, interface, ifname, ct, pk, current=False, time=None): access_type = self._get_mobile_signal_type(interface['mobile']['signal']) + if not access_type: + return data = interface['mobile']['signal'][access_type] signal_power = signal_strength = None extra_values = {} diff --git a/openwisp_monitoring/device/tests/test_api.py b/openwisp_monitoring/device/tests/test_api.py index 95eaf4eac..8fb0c3549 100644 --- a/openwisp_monitoring/device/tests/test_api.py +++ b/openwisp_monitoring/device/tests/test_api.py @@ -906,6 +906,42 @@ def test_gsm_charts(self): self.assertEqual(charts[0]['summary']['signal_power'], None) self.assertEqual(charts[0]['summary']['signal_strength'], -70.0) + def test_mobile_signal_missing(self): + org = self._create_org() + device = self._create_device(organization=org) + data = { + 'type': 'DeviceMonitoring', + 'interfaces': [ + { + 'name': 'mobile0', + 'mac': '00:00:00:00:00:00', + 'mtu': 1900, + 'multicast': True, + 'txqueuelen': 1000, + 'type': 'modem-manager', + 'up': True, + 'mobile': { + 'connection_status': 'connected', + 'imei': '865847055230161', + 'manufacturer': 'QUALCOMM INCORPORATED', + 'model': 'QUECTEL Mobile Broadband Module', + 'operator_code': '22250', + 'operator_name': 'Iliad', + 'power_status': 'on', + 'signal': {}, + }, + } + ], + } + self._post_data(device.id, device.key, data) + response = self.client.get(self._url(device.pk.hex, device.key)) + self.assertEqual(response.status_code, 200) + self.assertEqual(response.data['charts'], []) + dd = DeviceData(name=device.name, pk=device.pk) + self.assertEqual( + dd.data['interfaces'][0]['mobile'], data['interfaces'][0]['mobile'] + ) + def test_pre_metric_write_signal(self): d = self._create_device(organization=self._create_org()) data = {'type': 'DeviceMonitoring', 'resources': {'cpus': 1, 'load': [0, 0, 0]}}