async def notify(log_level: int = None) -> NoReturn: """Notify our users periodically of the number of red metrics.""" logging.getLogger().setLevel(log_level or logging.ERROR) sleep_duration = int(os.environ.get("NOTIFIER_SLEEP_DURATION", 60)) api_version = "v3" reports_url = ( f"http://{os.environ.get('SERVER_HOST', 'localhost')}:" f"{os.environ.get('SERVER_PORT', '5001')}/api/{api_version}/reports") data_model = await retrieve_data_model(api_version) most_recent_measurement_seen = datetime.max.replace(tzinfo=timezone.utc) outbox = Outbox() notification_finder = NotificationFinder(data_model) while True: record_health() logging.info("Determining notifications...") try: async with aiohttp.ClientSession(raise_for_status=True, trust_env=True) as session: response = await session.get(reports_url) json = await response.json() except Exception as reason: # pylint: disable=broad-except logging.error("Could not get reports from %s: %s", reports_url, reason) json = dict(reports=[]) notifications = notification_finder.get_notifications( json, most_recent_measurement_seen) outbox.add_notifications(notifications) outbox.send_notifications() most_recent_measurement_seen = most_recent_measurement_timestamp(json) logging.info("Sleeping %.1f seconds...", sleep_duration) await asyncio.sleep(sleep_duration)
def test_merge_notifications_with_same_destination_but_different_report( self): """Test that the metrics are merged into the correct notification.""" report = dict(title="different_title", url="https://differentreport") metric1 = dict(metric_name="new_metric 1") metric2 = dict(metric_name="new_metric 2") metrics1 = [metric1, metric2] new_notifications = [Notification(report, metrics1, "uuid1", {})] outbox = Outbox(self.notifications) outbox.add_notifications(new_notifications) self.assertEqual( outbox.notifications[0].metrics, [self.metric_notification_data1, self.metric_notification_data2], )
def test_merge_nothing_into_notifications(self): """Test that notifications are merged, even if no new metrics are given to be added.""" outbox = Outbox(self.notifications) outbox.add_notifications([]) self.assertEqual(outbox.notifications, self.notifications)
def test_merge_nothing_into_nothing(self): """Test that notifications are merged, even if the destination is empty.""" outbox = Outbox() outbox.add_notifications([]) self.assertEqual(outbox.notifications, [])
def test_merge_notifications_into_nothing(self): """Test that notifications are merged, even if the outbox is currently empty.""" outbox = Outbox() outbox.add_notifications(self.notifications) self.assertEqual(outbox.notifications, self.notifications)