diff --git a/openwisp_monitoring/check/tests/test_models.py b/openwisp_monitoring/check/tests/test_models.py index 64f820ef2..0b44b601d 100644 --- a/openwisp_monitoring/check/tests/test_models.py +++ b/openwisp_monitoring/check/tests/test_models.py @@ -4,6 +4,7 @@ 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 @@ -126,19 +127,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..ddacfe49c 100644 --- a/openwisp_monitoring/db/backends/influxdb/client.py +++ b/openwisp_monitoring/db/backends/influxdb/client.py @@ -6,6 +6,7 @@ from django.conf import settings from django.core.exceptions import ValidationError +from django.utils.timezone import now from django.utils.functional import cached_property 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 + 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}')