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()
Пример #2
0
    def test_min_max_for_all_attributes(self):
        """
        Test whether an AttributeRange entry is created for all Attribute
        entries
        """

        response = self.testing_client.post('/importer_retry', data=dict(
            api_id=1), headers=self.access_token_header)
        self.assertEqual(response.status_code, 200)

        attributes = Attributes.get_all()
        attribute_range_entries = AttributeRange.get_all()
        attribute_ids = [attr_range.attribute_id for attr_range in
                         attribute_range_entries]

        for attr in attributes:
            self.assertIn(attr.id, attribute_ids)
    def get(self) -> [db.Model]:
        """
        Fetch Attributes from the database
        :param attribute_id:    Attribute id
        :param subtheme_id:    SubTheme id
        :return:   A list of Attributes with an HTTPstatus code OK (200) or an error message and a the appropriate
                    HTTPStatus code
        """
        args = self.reqpaser.parse_args()

        # Fetch by attribute_id
        if "attribute_id" in args and "subtheme_id" not in args:
            attribute = Attributes.get_by_id(args["attribute_id"])
            if not attribute:
                return {
                    "error": "Attribute not found",
                    "id": args["attribute_id"]
                }, HTTPStatus.NOT_FOUND
            content = [attribute]
            return [attr.json() for attr in content], HTTPStatus.OK

        # Fetch by subtheme_id
        elif "attribute_id" not in args and "subtheme_id" in args:
            attributes = Attributes.get_by_sub_theme_id(args["subtheme_id"])
            if not attributes:
                return {
                    "error": "Attributes not found",
                    "subtheme_id": args["subtheme_id"]
                }, HTTPStatus.NOT_FOUND
            if isinstance(attributes, Attributes):
                content = [attributes]
                return [attr.json() for attr in content], HTTPStatus.OK
            return [attr.json() for attr in attributes], HTTPStatus.OK

        # Fetch all attribute
        attributes = Attributes.get_all()
        return [attr.json() for attr in attributes], HTTPStatus.OK
Пример #4
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()