Exemplo n.º 1
0
    def test_exporter_sends_second_email_after_2_hours(self):

        message_count = len(TEST_MESSAGES)

        log = DataTransferLog(last_updated=datetime.datetime.now() -
                              datetime.timedelta(minutes=30),
                              total_records=2,
                              type="export")
        db.session.add(log)
        db.session.commit()
        ExportService.send_alert_if_exports_not_running()
        self.assertGreater(len(TEST_MESSAGES), message_count)
        self.assertEqual(
            "Star Drive: Error - 30 minutes since last successful export",
            self.decode(TEST_MESSAGES[-1]['subject']))

        log.last_updated = datetime.datetime.now() - datetime.timedelta(
            minutes=120)
        db.session.add(log)
        db.session.commit()
        ExportService.send_alert_if_exports_not_running()
        self.assertGreater(len(TEST_MESSAGES), message_count + 1,
                           "another email should have gone out")
        self.assertEqual(
            "Star Drive: Error - 2 hours since last successful export",
            self.decode(TEST_MESSAGES[-1]['subject']))
Exemplo n.º 2
0
 def log_for_export(self, exportables, date_started):
     total = 0
     for e in exportables:
         total += e.size
     if total > 0:
         log = DataTransferLog(type="import", total_records=total)
     else:
         log = self.db.session.query(DataTransferLog).filter(DataTransferLog.type == 'import') \
             .order_by(desc(DataTransferLog.last_updated)).limit(1).first()
         log.last_updated = date_started
     return log
Exemplo n.º 3
0
 def test_exporter_notifies_PI_after_24_hours(self):
     message_count = len(TEST_MESSAGES)
     log = DataTransferLog(last_updated=datetime.datetime.utcnow() - datetime.timedelta(hours=24), total_records=2,
                           type="export")
     db.session.add(log)
     db.session.commit()
     ExportService.send_alert_if_exports_not_running()
     self.assertTrue("*****@*****.**" in TEST_MESSAGES[-1]['To'])
Exemplo n.º 4
0
 def test_exporter_sends_20_emails_over_first_48_hours(self):
     message_count = len(TEST_MESSAGES)
     log = DataTransferLog(last_updated=datetime.datetime.utcnow() - datetime.timedelta(days=2), total_records=2,
                           type="export")
     db.session.add(log)
     db.session.commit()
     for i in range(20):
         ExportService.send_alert_if_exports_not_running()
     self.assertEqual(message_count + 20, len(TEST_MESSAGES), msg="20 emails should have gone out.")
Exemplo n.º 5
0
    def send_alert_if_exports_not_running():
        """
        If more than 30 minutes pass without an export from the Public Mirror to the Private Mirror, an email will be
        sent to an administrative email address. Emails to this address will occur every two (2) hours for the first
        24 hours and every four hours after that until the fault is corrected or the system taken down. After 24
        hours, the PI will also be emailed notifications every 8 hours until the fault is corrected or the system
        taken down.
        """
        alert_principal_investigator = False
        last_log = db.session.query(DataTransferLog).filter(DataTransferLog.type == 'export')\
            .order_by(desc(DataTransferLog.last_updated)).limit(1).first()
        if not last_log:
            # If the export logs are empty, create one with the current date.
            seed_log = DataTransferLog(total_records=0)
            db.session.add(seed_log)
            db.session.commit()
        else:
            msg = None
            subject = "Autism DRIVE: Error - "
            now = datetime.datetime.utcnow().replace(
                tzinfo=datetime.timezone.utc)  # Make date timezone aware
            time_difference = now - last_log.last_updated
            hours = int(time_difference.total_seconds() / 3600)
            minutes = int(time_difference.total_seconds() / 60)
            if hours >= 24 and hours % 4 == 0 and last_log.alerts_sent < (
                    hours / 4 + 12):
                alert_principal_investigator = hours % 8 == 0
                subject = subject + str(
                    hours) + " hours since last successful export"
                msg = "Exports should occur every 5 minutes.  It has been " + str(hours) + \
                    " hours since the last export was requested. This is the " + str(last_log.alerts_sent) + \
                    " email about this issue.  You will receive an email every 2 hours for the first " + \
                    "24 hours, and every 4 hours there-after."

            elif hours >= 2 and hours % 2 == 0 and hours / 2 >= last_log.alerts_sent:
                subject = subject + str(
                    hours) + " hours since last successful export"
                msg = "Exports should occur every 5 minutes.  It has been " + str(hours) + \
                    " hours since the last export was requested. This is the " + str(last_log.alerts_sent) + \
                    " email about this issue.  You will receive an email every 2 hours for the first " \
                    "24 hours, and every 4 hours there-after."

            elif minutes >= 30 and last_log.alerts_sent == 0:
                subject = subject + str(
                    minutes) + " minutes since last successful export"
                msg = "Exports should occur every 5 minutes.  It has been " + str(minutes) + \
                    " minutes since the last export was requested."
            if msg:
                email_server = EmailService(app)
                email_server.admin_alert_email(
                    subject,
                    msg,
                    alert_principal_investigator=alert_principal_investigator)
                last_log.alerts_sent = last_log.alerts_sent + 1
                db.session.add(last_log)
                db.session.commit()
Exemplo n.º 6
0
    def test_exporter_sends_no_email_alert_if_less_than_30_minutes_pass_without_export(self):

        message_count = len(TEST_MESSAGES)

        log = DataTransferLog(last_updated=datetime.datetime.utcnow() - datetime.timedelta(minutes=28), total_records=2,
                              type="export")
        db.session.add(log)
        db.session.commit()
        ExportService.send_alert_if_exports_not_running()
        self.assertEqual(len(TEST_MESSAGES), message_count)
Exemplo n.º 7
0
    def get(self):

        date_started = datetime.datetime.now()
        info_list = ExportService.get_table_info(get_date_arg())

        # Remove items that are not exportable, or that are identifying
        info_list = [item for item in info_list if item.exportable]
        info_list = [item for item in info_list if item.question_type != ExportService.TYPE_IDENTIFYING]

        # Get a count of the records, and log it.
        log = DataTransferLog(type="export")
        total_records_for_export = 0
        for item in info_list:
            total_records_for_export += item.size
            if item.size > 0:
                log_detail = DataTransferLogDetail(date_started=date_started, class_name=item.class_name,
                                                   successful=True, success_count=item.size)
                log.details.append(log_detail)
        log.total_records = total_records_for_export;

        # If we find we aren't exporting anything, don't create a new log, just update the last one.
        if total_records_for_export == 0:
            log = db.session.query(DataTransferLog).filter(DataTransferLog.type == 'export')\
                .order_by(desc(DataTransferLog.last_updated)).limit(1).first()
            if log is None: log = DataTransferLog(type="export", total_records=0)
            log.last_updated = datetime.datetime.now()
        db.session.add(log)
        db.session.commit()

        return self.schema.dump(info_list)
Exemplo n.º 8
0
    def test_exporter_sends_second_email_after_2_hours(self):
        """
        If more than 2 hours pass without an export from the Public Mirror to the Private Mirror, an email will be
        sent to an administrative email address at the 30 minute and 2 hour marks.
        """
        message_count = len(TEST_MESSAGES)

        log = DataTransferLog(last_updated=datetime.datetime.utcnow() - datetime.timedelta(minutes=30), total_records=2, type="export")
        db.session.add(log)
        db.session.commit()
        ExportService.send_alert_if_exports_not_running()
        print('@ 30 minutes:', len(TEST_MESSAGES), 'messages')
        self.assertGreater(len(TEST_MESSAGES), message_count)
        self.assertEqual("Autism DRIVE: Error - 30 minutes since last successful export", self.decode(TEST_MESSAGES[-1]['subject']))

        log.last_updated = datetime.datetime.utcnow() - datetime.timedelta(minutes=120)
        db.session.add(log)
        db.session.commit()
        ExportService.send_alert_if_exports_not_running()
        print('@ 2 hours:', len(TEST_MESSAGES), 'messages')
        self.assertGreater(len(TEST_MESSAGES), message_count + 1, "another email should have gone out")
        self.assertEqual("Autism DRIVE: Error - 2 hours since last successful export", self.decode(TEST_MESSAGES[-1]['subject']))
Exemplo n.º 9
0
 def test_exporter_sends_12_emails_over_first_24_hours(self):
     """
     If more than 24 hours pass without an export from the Public Mirror to the Private Mirror, an email will be
     sent to an administrative email address at the 30 minute and then every 2 hours after that.
     """
     message_count = len(TEST_MESSAGES)
     date = datetime.datetime.utcnow() - datetime.timedelta(hours=22)
     log = DataTransferLog(last_updated=date,
                           total_records=2, type="export")
     db.session.add(log)
     db.session.commit()
     for i in range(20):
         ExportService.send_alert_if_exports_not_running()
     self.assertEqual(message_count + 12, len(TEST_MESSAGES), msg="12 emails should have gone out.")
Exemplo n.º 10
0
    def test_export_logs_endpoint(self):
        for i in range(20):
            elog = DataTransferLog()
            for x in range(2):
                dlog = DataTransferLogDetail()
                elog.details.append(dlog)
            db.session.add(elog)

        rv = self.app.get('/api/data_transfer_log?pageSize=10',
                          follow_redirects=True,
                          content_type="application/json",
                          headers=self.logged_in_headers())
        self.assert_success(rv)
        response = json.loads(rv.get_data(as_text=True))
        self.assertEquals(20, response['total'])
        self.assertEquals(2, response['pages'])
        self.assertEquals(10, len(response['items']))
        results = DataTransferLogSchema(many=True, session=db.session).load(response['items']).data
        self.assertEquals(10, len(results))
Exemplo n.º 11
0
    def test_exporter_sends_email_alert_if_30_minutes_pass_without_export(self):
        """
        If more than 30 minutes pass without an export from the Public Mirror to the Private Mirror, an email should be
        sent to an administrative email address.
        """
        message_count = len(TEST_MESSAGES)

        log = DataTransferLog(last_updated=datetime.datetime.utcnow() - datetime.timedelta(minutes=45), total_records=2,
                              type="export")
        db.session.add(log)
        db.session.commit()

        ExportService.send_alert_if_exports_not_running()
        self.assertGreater(len(TEST_MESSAGES), message_count)
        self.assertEqual("Autism DRIVE: Error - 45 minutes since last successful export",
                         self.decode(TEST_MESSAGES[-1]['subject']))
        ExportService.send_alert_if_exports_not_running()
        ExportService.send_alert_if_exports_not_running()
        ExportService.send_alert_if_exports_not_running()
        self.assertEqual(message_count + 1, len(TEST_MESSAGES), msg="No more messages should be sent.")
        self.assertEqual("*****@*****.**", TEST_MESSAGES[-1]['To'])
Exemplo n.º 12
0
    def test_exporter_sends_email_alert_if_30_minutes_pass_without_export(
            self):

        message_count = len(TEST_MESSAGES)

        log = DataTransferLog(last_updated=datetime.datetime.now() -
                              datetime.timedelta(minutes=45),
                              total_records=2,
                              type="export")
        db.session.add(log)
        db.session.commit()

        ExportService.send_alert_if_exports_not_running()
        self.assertGreater(len(TEST_MESSAGES), message_count)
        self.assertEqual(
            "Star Drive: Error - 45 minutes since last successful export",
            self.decode(TEST_MESSAGES[-1]['subject']))
        ExportService.send_alert_if_exports_not_running()
        ExportService.send_alert_if_exports_not_running()
        ExportService.send_alert_if_exports_not_running()
        self.assertEqual(message_count + 1,
                         len(TEST_MESSAGES),
                         msg="No more messages should be sent.")
        self.assertEqual("*****@*****.**", TEST_MESSAGES[-1]['To'])