Skip to content

Commit

Permalink
[minor] Requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
nepython committed Jul 31, 2020
1 parent 5d3e70c commit 9a6ed40
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 17 deletions.
12 changes: 5 additions & 7 deletions openwisp_monitoring/check/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down
16 changes: 7 additions & 9 deletions openwisp_monitoring/db/backends/influxdb/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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]},
{
Expand All @@ -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}')
Expand Down
10 changes: 9 additions & 1 deletion openwisp_monitoring/db/backends/influxdb/tests.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import timedelta
from datetime import datetime, timedelta

from django.core.exceptions import ValidationError
from django.test import TestCase
Expand Down Expand Up @@ -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)
1 change: 1 addition & 0 deletions requirements-test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ redis
django-redis
mock-ssh-server>=0.8.0,<0.9.0
channels_redis
freezegun

0 comments on commit 9a6ed40

Please sign in to comment.