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] Both 'rx_bytes' and 'tx_bytes' are present #595 #603

Merged
merged 4 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
34 changes: 34 additions & 0 deletions openwisp_monitoring/device/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,40 @@ def test_calculate_increment(self):
result = dd.writer._calculate_increment('wlan0', 'rx_bytes', 1234.56)
self.assertEqual(result, 1234)

def test_save_data_missing_tx_bytes(self):
"""
Test that device data is saved successfully when tx_bytes is missing
while rx_bytes is present in interface statistics
"""
dd = self._create_device_data()
data = deepcopy(self._sample_data)
del data['interfaces'][0]['statistics']['tx_bytes']
dd.data = data
dd.save_data()
dd_read = DeviceData(pk=dd.pk)
interface_stats = dd_read.data['interfaces'][0]['statistics']
self.assertEqual(interface_stats['rx_bytes'], 0)
self.assertNotIn('tx_bytes', interface_stats)
self.assertEqual(interface_stats['collisions'], 0)
self.assertEqual(interface_stats['multicast'], 0)

def test_save_data_missing_rx_bytes(self):
"""
Test that device data is saved successfully when rx_bytes is missing
while tx_bytes is present in interface statistics
"""
dd = self._create_device_data()
data = deepcopy(self._sample_data)
del data['interfaces'][0]['statistics']['rx_bytes']
dd.data = data
dd.save_data()
dd_read = DeviceData(pk=dd.pk)
interface_stats = dd_read.data['interfaces'][0]['statistics']
self.assertEqual(interface_stats['tx_bytes'], 864)
self.assertNotIn('rx_bytes', interface_stats)
self.assertEqual(interface_stats['collisions'], 0)
self.assertEqual(interface_stats['multicast'], 0)


class TestDeviceMonitoring(CreateConnectionsMixin, BaseTestCase):
"""Test openwisp_monitoring.device.models.DeviceMonitoring"""
Expand Down
2 changes: 1 addition & 1 deletion openwisp_monitoring/device/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def write(self, data, time=None, current=False):
# Explicitly stated None to avoid skipping in case the stats are zero
if (
ifstats.get('rx_bytes') is not None
and ifstats.get('rx_bytes') is not None
and ifstats.get('tx_bytes') is not None
):
field_value = self._calculate_increment(
ifname, 'rx_bytes', ifstats['rx_bytes']
Expand Down