def run():
        """ Schedule main_task to execute once a day """
        apis = Scheduler.get_apis()
        for api in apis:
            if not ImporterStatuses.find_by_api_id(api.id):
                class_name = api.api_class.split('.')[2]
                new_entry = ImporterStatuses(api.id, class_name, 'pending', '',
                                             '', False, datetime.now())
                new_entry.save()
                new_entry.commit()

        all_attribute = Attributes.get_all()
        for attribute in all_attribute:
            if not AttributeRange.get_by_attr_id(attribute.id):
                attr_min = Attributes.attribute_min(attribute.table_name)
                attr_max = Attributes.attribute_max(attribute.table_name)
                try:
                    new_range = AttributeRange(attribute.id, attr_min.s_id,
                                               attr_min.value,
                                               attr_min.timestamp,
                                               attr_max.s_id, attr_max.value,
                                               attr_max.timestamp,
                                               datetime.now())
                except AttributeError:
                    new_range = AttributeRange(attribute.id, None, None,
                                               None, None, None, None,
                                               datetime.now())

                new_range.save()
                new_range.commit()

        sched.add_job(Scheduler.main_task,
                      'interval',
                      start_date=datetime.now() + timedelta(seconds=5),
                      days=1,
                      name='Primary_Scheduler',
                      replace_existing=True,
                      id='Primary_Scheduler',
                      jobstore='sqlalchemy')

        try:
            # This is here to simulate application activity (which keeps
            # the main thread alive).
            while True:
                time.sleep(2)
        except (KeyboardInterrupt, SystemExit):
            sched.shutdown()
Esempio n. 2
0
    def range_wrapper(*args: Any, **kwargs: dict):
        """
        Execute importer function and then compute minimum and maximum
        values of attributes
        :param args: Arguments of import_function parameter
        :param kwargs: Keyword Arguments of import_function parameter
        """
        import_function(*args, **kwargs)

        attribute_entries = Attributes.get_all()
        for attribute in attribute_entries:
            attribute_range = AttributeRange.get_by_attr_id(attribute.id)
            if attribute_range:
                most_recent_entry = Attributes.most_recent_timestamp(
                    attribute.table_name)
                if most_recent_entry:
                    if attribute_range.latest_update < most_recent_entry:
                        attr_min = Attributes.attribute_min(
                            attribute.table_name)
                        attr_max = Attributes.attribute_max(
                            attribute.table_name)
                        try:
                            attribute_range.minimum_sensor_id = \
                                attr_min.s_id
                            attribute_range.minimum = attr_min.value
                            attribute_range.minimum_recorded_date = \
                                attr_min.timestamp
                            attribute_range.maximum_sensor_id = \
                                attr_max.s_id
                            attribute_range.maximum = attr_max.value
                            attribute_range.maximum_recorded_date = \
                                attr_max.timestamp
                            attribute_range.latest_update = datetime.now()
                            attribute_range.save()
                            attribute_range.commit()

                            PushAlert.check_alerts(attribute_range)
                            check_min_and_max_alert_widgets(attribute_range)
                        except AttributeError:
                            pass
            else:
                attr_min = Attributes.attribute_min(attribute.table_name)
                attr_max = Attributes.attribute_max(attribute.table_name)
                try:
                    new_range_entry = \
                        AttributeRange(attribute.id,
                                       attr_min.s_id,
                                       attr_min.value,
                                       attr_min.timestamp,
                                       attr_max.s_id,
                                       attr_max.value,
                                       attr_max.timestamp,
                                       datetime.now())
                    new_range_entry.save()
                    new_range_entry.commit()
                    check_min_and_max_alert_widgets(new_range_entry)
                    PushAlert.check_alerts(new_range_entry)
                except AttributeError:
                    new_range_entry = AttributeRange(attribute.id, None, None,
                                                     None, None, None, None,
                                                     datetime.now())
                    new_range_entry.save()
                    new_range_entry.commit()