def test_check_status(self, now_mock, requests_post_mock): """ Check whether downtime of the datalogger triggers notifications. """ now_mock.return_value = timezone.make_aware( timezone.datetime(2018, 1, 1, hour=12)) requests_post_mock.return_value = mock.MagicMock(status_code=200, text='OK') StatusNotificationSetting.get_solo() notification_settings = NotificationSetting.get_solo() notification_settings.notification_service = NotificationSetting.NOTIFICATION_PROWL notification_settings.prowl_api_key = self.API_KEY notification_settings.save() # Schedule ahead. StatusNotificationSetting.objects.update(next_check=timezone.now() + timezone.timedelta(minutes=1)) dsmr_notification.services.check_status() # No data. StatusNotificationSetting.objects.update(next_check=timezone.now()) DsmrReading.objects.all().delete() dsmr_notification.services.check_status() self.assertGreater(StatusNotificationSetting.get_solo().next_check, timezone.now()) # Recent data. StatusNotificationSetting.objects.update(next_check=timezone.now()) DsmrReading.objects.create( timestamp=timezone.now() - timezone.timedelta(minutes=45), electricity_delivered_1=1, electricity_returned_1=2, electricity_delivered_2=3, electricity_returned_2=4, electricity_currently_delivered=5, electricity_currently_returned=6, ) dsmr_notification.services.check_status() self.assertGreater(StatusNotificationSetting.get_solo().next_check, timezone.now()) self.assertFalse(requests_post_mock.called) # Data from a while ago. StatusNotificationSetting.objects.update(next_check=timezone.now()) DsmrReading.objects.all().delete() DsmrReading.objects.create( timestamp=timezone.now() - timezone.timedelta(hours=24), electricity_delivered_1=1, electricity_returned_1=2, electricity_delivered_2=3, electricity_returned_2=4, electricity_currently_delivered=5, electricity_currently_returned=6, ) StatusNotificationSetting.objects.update(next_check=timezone.now()) self.assertFalse(requests_post_mock.called) dsmr_notification.services.check_status() self.assertTrue(requests_post_mock.called)
def check_status(): """ Checks the status of the application. """ status_settings = StatusNotificationSetting.get_solo() notification_settings = NotificationSetting.get_solo() if notification_settings.notification_service is None or \ not dsmr_backend.services.is_timestamp_passed(timestamp=status_settings.next_check): return if not DsmrReading.objects.exists(): return StatusNotificationSetting.objects.update( next_check=timezone.now() + timezone.timedelta(minutes=5)) # Check for recent data. has_recent_reading = DsmrReading.objects.filter( timestamp__gt=timezone.now() - timezone.timedelta(minutes=settings. DSMRREADER_STATUS_READING_OFFSET_MINUTES)).exists() if has_recent_reading: return StatusNotificationSetting.objects.update( next_check=timezone.now() + timezone.timedelta(minutes=5)) # Alert! logger.debug(' - Sending notification about datalogger lagging behind...') send_notification( str( _('It has been over {} hour(s) since the last reading received. Please check your datalogger.' )), str(_('Datalogger check'))) StatusNotificationSetting.objects.update( next_check=timezone.now() + timezone.timedelta( hours=settings.DSMRREADER_STATUS_NOTIFICATION_COOLDOWN_HOURS))
def setUp(self): self.instance = StatusNotificationSetting().get_solo()