Example #1
0
    def index(self):
        """
        Default controller providing access to the catalogue of user model runs
        :return: Rendered catalogue page
        """
        # all non-created runs for the user
        c.user = self.current_user
        c.model_runs = [model
                        for model in self._model_run_service.get_models_for_user(self.current_user)
                        if model.status.name != constants.MODEL_RUN_STATUS_CREATED]

        total_user_storage = 0
        for model in c.model_runs:
            if model.status.name != constants.MODEL_RUN_STATUS_PUBLISHED and \
               model.status.name != constants.MODEL_RUN_STATUS_PUBLIC:
                if model.storage_in_mb is not None:
                    total_user_storage += model.storage_in_mb

        c.storage_total_used_in_gb = utils.convert_mb_to_gb_and_round(total_user_storage)
        c.storage_percent_used = round(c.storage_total_used_in_gb / c.user.storage_quota_in_gb * 100.0, 0)
        c.bar_class = helpers.get_progress_bar_class_name(c.storage_percent_used)
        c.showing = "mine"

        c.user_has_model_run_being_created = \
            self._model_run_service.get_user_has_model_run_being_created(self.current_user)

        return render("model_run/catalogue.html")
Example #2
0
    def index(self):
        """Allow admin-user to see all users of the system. If user is non-admin, redirect to page not found.
        """

        if self.current_user is not None and self.current_user.is_admin():
            c.all_users = self._user_service.get_all_users()
            user_map = {}
            for user in c.all_users:
                user_map[user.id] = user
                user.storage_in_mb = 0
                user.published_storage_in_mb = 0

            c.storage_total_used_in_gb = 0
            for user_id, status, storage_mb in self._model_run_service.get_storage_used():
                c.storage_total_used_in_gb += int(storage_mb)
                if status == constants.MODEL_RUN_STATUS_PUBLISHED or status == constants.MODEL_RUN_STATUS_PUBLIC:
                    user_map[user_id].published_storage_in_mb += int(storage_mb)
                else:
                    user_map[user_id].storage_in_mb += int(storage_mb)
            c.storage_total_used_in_gb = utils.convert_mb_to_gb_and_round(c.storage_total_used_in_gb)

            c.core_user = None
            for user in c.all_users:
                if user.username == constants.CORE_USERNAME:
                    c.core_user = user

                user.quota_status = ''
                percentage = round(utils.convert_mb_to_gb_and_round(user.storage_in_mb)
                                   / user.storage_quota_in_gb * 100.0, 1)
                if percentage >= constants.QUOTA_ABSOLUTE_LIMIT_PERCENT:
                    user.quota_status = 'error'
                elif percentage >= constants.QUOTA_WARNING_LIMIT_PERCENT:
                    user.quota_status = 'warning'

            c.core_user.quota_status = 'info'

            c.total_storage_percent_used = c.storage_total_used_in_gb / c.core_user.storage_quota_in_gb * 100.0
            c.bar_class = helpers.get_progress_bar_class_name(c.total_storage_percent_used)

            return render('user/list_of_users.html')

        else:

            return render('not_found.html')
    def test_GIVEN_null_storage_WHEN_list_THEN_returns_current_storages_corectly_added_up(self):
            user = self.login(access_level=constants.USER_ACCESS_LEVEL_ADMIN)

            self.create_run_model(None, "test1", user, constants.MODEL_RUN_STATUS_PUBLISHED)

            response = self.app.get(
                url=url(controller='user', action=''),
                expect_errors=True
            )
            assert_that(response.normal_body, contains_string(str(utils.convert_mb_to_gb_and_round(0))))
    def test_GIVEN_multiple_runs_some_published_some_public_WHEN_list_THEN_returns_current_storages_corectly_added_up(self):
            user = self.login(access_level=constants.USER_ACCESS_LEVEL_ADMIN)

            failed_storage1 = 90000
            self.create_run_model(failed_storage1, "test", user, constants.MODEL_RUN_STATUS_FAILED)
            failed_storage2 = 30000
            self.create_run_model(failed_storage2, "tes2", user, constants.MODEL_RUN_STATUS_UNKNOWN)

            pub_storage1 = 80000
            self.create_run_model(pub_storage1, "test3", user, constants.MODEL_RUN_STATUS_PUBLISHED)
            pub_storage2 = 100000
            self.create_run_model(pub_storage2, "test4", user, constants.MODEL_RUN_STATUS_PUBLISHED)
            pub_storage3 = 110000
            self.create_run_model(pub_storage3, "test5", user, constants.MODEL_RUN_STATUS_PUBLIC)

            response = self.app.get(
                url=url(controller='user', action=''),
                expect_errors=True
            )
            assert_that(response.normal_body, contains_string(str(utils.convert_mb_to_gb_and_round(failed_storage1 + failed_storage2))))
            assert_that(response.normal_body, contains_string(str(utils.convert_mb_to_gb_and_round(pub_storage1 + pub_storage2 + pub_storage3))))
            assert_that(response.normal_body, contains_string(str(utils.convert_mb_to_gb_and_round((pub_storage1 + pub_storage2 + pub_storage3 + failed_storage1 + failed_storage2)))))
    def _find_storage_use(self):
        """
        Find the total storage use
        :return: tuple of quota, total used, percent used
        """

        with self.readonly_scope() as session:
            core_user = session.query(User).filter(User.username == constants.CORE_USERNAME).one()

        storage_total_used_in_gb = 0
        for user_id, status, storage_mb in self._model_run_service.get_storage_used():
            if storage_mb is not None:
                storage_total_used_in_gb += int(storage_mb)
        storage_total_used_in_gb = utils.convert_mb_to_gb_and_round(storage_total_used_in_gb)

        total_storage_percent_used = storage_total_used_in_gb / core_user.storage_quota_in_gb * 100.0

        return core_user.storage_quota_in_gb, storage_total_used_in_gb, total_storage_percent_used
    def check_user_quota(self, current_user):
        """
        Check that the user can create a new model run
        :param current_user: the current user
        :return: the model run being created
        """
        total = 0
        for user_id, status_name, storage_in_mb in self._model_run_service.get_storage_used(current_user):
            if status_name != constants.MODEL_RUN_STATUS_PUBLISHED and status_name != constants.MODEL_RUN_STATUS_PUBLIC:
                total += storage_in_mb

        total_in_gb = utils.convert_mb_to_gb_and_round(total)

        storage_percent_used = round(total_in_gb / current_user.storage_quota_in_gb * 100.0, 0)

        if storage_percent_used >= constants.QUOTA_ABSOLUTE_LIMIT_PERCENT:
            helpers.error_flash(constants.ERROR_MESSAGE_QUOTA_EXCEEDED)
            redirect(url(controller="model_run", action="index"))