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 b07ac63
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
11 changes: 5 additions & 6 deletions openwisp_monitoring/check/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

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

0 comments on commit b07ac63

Please sign in to comment.