Beispiel #1
0
 def setUp(self):
     """Set variables for the tests."""
     self.old_timestamp = "2019-03-01T00:00:00+00:00"
     self.new_timestamp = "2020-03-01T00:00:00+00:00"
     self.metric_uuid = "metric_uuid"
     self.red_metric = self.metric(
         status="target_not_met",
         recent_measurements=[],
         status_start=str(datetime.fromisoformat(datetime.min.isoformat())),
     )
     self.notification_finder = NotificationFinder(self.data_model)
     self.metric_with_recent_measurements = self.metric(
         recent_measurements=[
             dict(start=self.old_timestamp,
                  end=self.old_timestamp,
                  count=dict(status="target_not_met", value="10")),
             dict(
                 start=self.old_timestamp,
                 end=self.new_timestamp,
                 count=dict(
                     status="target_not_met",
                     value="0",
                     status_start=self.old_timestamp,
                 ),
             ),
         ],
         status_start=self.old_timestamp,
     )
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)
Beispiel #3
0
 def setUp(self):
     """Override to set up a metric fixture."""
     self.old_timestamp = "2019-02-01T00:00:00+00:00"
     self.new_timestamp = "2020-02-01T00:00:00+00:00"
     self.notification_finder = NotificationFinder(self.data_model)
     self.red_metric = self.metric(
         status="target_not_met",
         recent_measurements=[
             dict(start=self.old_timestamp, end=self.new_timestamp)
         ],
         status_start=self.old_timestamp,
     )
Beispiel #4
0
 def setUp(self):
     """."""
     self.most_recent_measurement_seen = datetime.datetime.min.isoformat()
     self.old_timestamp = "2019-02-01T00:23:59+59:00"
     self.new_timestamp = "2020-02-01T00:23:59+59:00"
     self.notification_finder = NotificationFinder(self.data_model)
     count = dict(status="target_not_met", value="34")
     self.red_metric = self.metric(
         status="target_not_met",
         recent_measurements=[dict(start=self.old_timestamp, end=self.new_timestamp, count=count)],
         status_start=str(datetime.datetime.fromisoformat(self.most_recent_measurement_seen)),
     )
Beispiel #5
0
 def setUp(self):
     """Set variables for the other testcases."""
     self.most_recent_measurement_seen = datetime.datetime.min.isoformat()
     self.old_timestamp = "2019-01-01T00:23:59+59:00"
     self.new_timestamp = "2020-01-01T00:23:59+59:00"
     self.report_url = "https://report1"
     self.white_metric_status = "unknown"
     self.notification_finder = NotificationFinder(self.data_model)
     count = dict(status="target_not_met", value="10")
     self.red_metric = self.metric(
         status="target_not_met",
         recent_measurements=[dict(start=self.old_timestamp, end=self.new_timestamp, count=count)],
         status_start=str(datetime.datetime.fromisoformat(self.most_recent_measurement_seen)),
     )