From 9a6ed40acee15114bf132d88dc3eb79f1119c8a2 Mon Sep 17 00:00:00 2001 From: Hardik Jain Date: Fri, 31 Jul 2020 16:04:41 +0530 Subject: [PATCH] [minor] Requested changes --- openwisp_monitoring/check/tests/test_models.py | 12 +++++------- .../db/backends/influxdb/client.py | 16 +++++++--------- .../db/backends/influxdb/tests.py | 10 +++++++++- requirements-test.txt | 1 + 4 files changed, 22 insertions(+), 17 deletions(-) diff --git a/openwisp_monitoring/check/tests/test_models.py b/openwisp_monitoring/check/tests/test_models.py index 64f820ef2..2b8ccbcb0 100644 --- a/openwisp_monitoring/check/tests/test_models.py +++ b/openwisp_monitoring/check/tests/test_models.py @@ -4,10 +4,10 @@ from django.core.exceptions import ValidationError from django.test import TransactionTestCase from django.utils.timezone import now +from freezegun import freeze_time from swapper import load_model from ...device.tests import TestDeviceMonitoringMixin -from ...device.utils import SHORT_RP from .. import settings as app_settings from ..classes import ConfigApplied, Ping from ..tasks import auto_create_config_check, auto_create_ping @@ -126,19 +126,17 @@ def test_config_modified_device_problem(self): self.assertEqual(Metric.objects.count(), 0) self.assertEqual(AlertSettings.objects.count(), 0) check = Check.objects.filter(check=self._CONFIG_APPLIED).first() - check.perform_check() + with freeze_time(now() - timedelta(minutes=10)): + check.perform_check() self.assertEqual(Metric.objects.count(), 1) self.assertEqual(AlertSettings.objects.count(), 1) + # Check needs to be run again without mocking time for threshold crossed + check.perform_check() m = Metric.objects.first() self.assertEqual(m.content_object, d) self.assertEqual(m.key, 'config_applied') dm = d.monitoring - # Health status should not change as threshold time not crossed - self.assertEqual(dm.status, 'ok') - self.assertTrue(m.is_healthy) - m.write(0, retention_policy=SHORT_RP, time=now() - timedelta(minutes=10)) self.assertFalse(m.is_healthy) - dm.refresh_from_db() self.assertEqual(dm.status, 'problem') self.assertEqual(Notification.objects.count(), 1) diff --git a/openwisp_monitoring/db/backends/influxdb/client.py b/openwisp_monitoring/db/backends/influxdb/client.py index d1ed05119..b05477ecd 100644 --- a/openwisp_monitoring/db/backends/influxdb/client.py +++ b/openwisp_monitoring/db/backends/influxdb/client.py @@ -7,6 +7,7 @@ from django.conf import settings from django.core.exceptions import ValidationError from django.utils.functional import cached_property +from django.utils.timezone import now from django.utils.translation import gettext_lazy as _ from influxdb import InfluxDBClient @@ -112,11 +113,10 @@ def write(self, name, values, **kwargs): 'tags': kwargs.get('tags'), 'fields': values, } - timestamp = kwargs.get('timestamp') + timestamp = kwargs.get('timestamp') or now() if isinstance(timestamp, datetime): - timestamp = timestamp.strftime('%Y-%m-%dT%H:%M:%SZ') - if timestamp: - point['time'] = timestamp + timestamp = timestamp.isoformat(sep='T', timespec='microseconds') + point['time'] = timestamp self.get_db.write( {'points': [point]}, { @@ -130,15 +130,13 @@ def read(self, key, fields, tags, **kwargs): since = kwargs.get('since') order = kwargs.get('order') limit = kwargs.get('limit') - retention_policy = kwargs.get('retention_policy') + rp = kwargs.get('retention_policy') if extra_fields and extra_fields != '*': fields = ', '.join([fields] + extra_fields) elif extra_fields == '*': fields = '*' - if not retention_policy: - q = f'SELECT {fields} FROM {key}' - else: - q = f'SELECT {fields} FROM {retention_policy}.{key}' + from_clause = f'{rp}.{key}' if rp else key + q = f'SELECT {fields} FROM {from_clause}' conditions = [] if since: conditions.append(f'time >= {since}') diff --git a/openwisp_monitoring/db/backends/influxdb/tests.py b/openwisp_monitoring/db/backends/influxdb/tests.py index 5217e819e..b94703e4d 100644 --- a/openwisp_monitoring/db/backends/influxdb/tests.py +++ b/openwisp_monitoring/db/backends/influxdb/tests.py @@ -1,4 +1,4 @@ -from datetime import timedelta +from datetime import datetime, timedelta from django.core.exceptions import ValidationError from django.test import TestCase @@ -232,3 +232,11 @@ def test_read_with_rp(self): self.assertEqual(m.read(retention_policy=SHORT_RP)[0][m.field_name], 0) self.assertFalse(m.is_healthy) self.assertEqual(Notification.objects.count(), 1) + + def test_metric_write(self): + m = self._create_object_metric( + name='wlan0', key='wlan0', configuration='clients' + ) + m.write('00:14:5c:00:00:00', time=datetime(2020, 7, 31, 22, 5, 47, 235142)) + m.write('00:23:4a:00:00:00', time=datetime(2020, 7, 31, 22, 5, 47, 235152)) + self.assertEqual(len(m.read()), 2) diff --git a/requirements-test.txt b/requirements-test.txt index 2b15f2f56..46b35502b 100644 --- a/requirements-test.txt +++ b/requirements-test.txt @@ -3,3 +3,4 @@ redis django-redis mock-ssh-server>=0.8.0,<0.9.0 channels_redis +freezegun