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 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
7 changes: 5 additions & 2 deletions openwisp_monitoring/device/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,20 @@ def assertDataDict(self, dd_data, data):
data = self._transform_wireless_interface_test_data(data)
self.assertDictEqual(dd_data, data)

def create_test_data(self, no_resources=False):
def create_test_data(self, no_resources=False, data=None, assertions=True):
o = self._create_org()
d = self._create_device(organization=o)
data = self._data()
if not data:
data = self._data()
# creation of resources metrics can be avoided in tests not involving them
# this speeds up those tests by reducing requests made
if no_resources:
del data['resources']
r = self._post_data(d.id, d.key, data)
self.assertEqual(r.status_code, 200)
dd = DeviceData(pk=d.pk)
if not assertions:
return dd
self.assertDataDict(dd.data, data)
if no_resources:
metric_count, chart_count = 4, 4
Expand Down
30 changes: 30 additions & 0 deletions openwisp_monitoring/device/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1378,6 +1378,36 @@ def _assert_chart_group(url, status_code, expected):
self.assertEqual(rows[-2].strip().split(','), ['ssh', '100.0'])
self.assertEqual(rows[-1].strip().split(','), ['http2', '90.0'])

def test_missing_rx_bytes(self):
"""
Regression test for:
https://github.com/openwisp/openwisp-monitoring/issues/595
"""
data = self._data()
del data['interfaces'][0]['statistics']['rx_bytes']
dd = self.create_test_data(no_resources=True, data=data, assertions=False)
qs = Metric.objects.filter(
object_id=dd.pk, key='traffic', main_tags={'ifname': 'wlan0'}
)
self.assertEqual(qs.count(), 1)
m = qs.first()
points = self._read_metric(m, limit=1, extra_fields=['tx_bytes'])
self.assertEqual(points[0].get('tx_bytes'), 145)
self.assertEqual(points[0].get('rx_bytes'), 0)

def test_missing_tx_bytes(self):
data = self._data()
del data['interfaces'][0]['statistics']['tx_bytes']
dd = self.create_test_data(no_resources=True, data=data, assertions=False)
qs = Metric.objects.filter(
object_id=dd.pk, key='traffic', main_tags={'ifname': 'wlan0'}
)
self.assertEqual(qs.count(), 1)
m = qs.first()
points = self._read_metric(m, limit=1, extra_fields=['tx_bytes'])
self.assertEqual(points[0].get('rx_bytes'), 324)
self.assertEqual(points[0].get('tx_bytes'), 0)


class TestGeoApi(TestGeoMixin, AuthenticationMixin, DeviceMonitoringTestCase):
location_model = Location
Expand Down
6 changes: 3 additions & 3 deletions openwisp_monitoring/device/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,14 @@ 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
or ifstats.get('tx_bytes') is not None
):
field_value = self._calculate_increment(
ifname, 'rx_bytes', ifstats['rx_bytes']
ifname, 'rx_bytes', ifstats.get('rx_bytes', 0)
)
extra_values = {
'tx_bytes': self._calculate_increment(
ifname, 'tx_bytes', ifstats['tx_bytes']
ifname, 'tx_bytes', ifstats.get('tx_bytes', 0)
)
}
name = f'{ifname} traffic'
Expand Down