Esempio n. 1
0
def update_users_disk_quota(user=None):
    """Update users disk quota usage.

    :param user: User whose disk quota will be updated. If None, applies to all users.
    :type user: reana_db.models.User

    """
    from reana_commons.utils import get_disk_usage

    from reana_db.config import DEFAULT_QUOTA_RESOURCES
    from reana_db.models import Resource, User, UserResource

    users = [user] if user else User.query.all()

    for u in users:
        workspace_path = u.get_user_workspace()
        disk_usage_bytes = get_disk_usage(workspace_path, summarize=True)
        disk_usage_bytes = int(disk_usage_bytes[0]["size"]["raw"])

        disk_resource = Resource.query.filter_by(
            name=DEFAULT_QUOTA_RESOURCES["disk"]).one_or_none()

        if disk_resource:
            from .database import Session

            user_resource_quota = UserResource.query.filter_by(
                user_id=u.id_, resource_id=disk_resource.id_).first()
            user_resource_quota.quota_used = disk_usage_bytes
            Session.commit()
Esempio n. 2
0
    def get_workspace_disk_usage(self, summarize=False, search=None):
        """Retrieve disk usage information of a workspace."""
        from functools import partial

        if not summarize:
            # size break down per directory so we can't query DB (`r-client du`)
            return get_disk_usage(
                self.workspace_path,
                summarize,
                search,
                to_human_readable_units=partial(
                    ResourceUnit.human_readable_unit, ResourceUnit.bytes_),
            )

        disk_usage = self.get_quota_usage().get("disk", {}).get("usage", {})
        if not disk_usage:
            # recalculate disk workflow resource
            workflow_resource = store_workflow_disk_quota(self)
            disk_usage = dict(
                raw=workflow_resource.quota_used,
                to_human_readable_units=ResourceUnit.human_readable_unit(
                    ResourceUnit.bytes_, workflow_resource.quota_used),
            )

        return [{"name": "", "size": disk_usage}]
Esempio n. 3
0
 def _get_disk_usage_or_zero(workflow):
     """Get disk usage for a workflow if the workspace exists, zero if not."""
     try:
         disk_bytes = get_disk_usage(workflow.workspace_path, summarize=True)
         return int(disk_bytes[0]["size"]["raw"])
     except REANAMissingWorkspaceError:
         return 0
Esempio n. 4
0
def get_disk_usage_or_zero(workspace_path):
    """Get disk usage for the workspace if exists, zero if not."""
    from reana_commons.utils import get_disk_usage
    from reana_commons.errors import REANAMissingWorkspaceError

    try:
        disk_bytes = get_disk_usage(workspace_path, summarize=True)
        return int(disk_bytes[0]["size"]["raw"])
    except REANAMissingWorkspaceError:
        return 0
Esempio n. 5
0
def get_disk_usage_or_zero(workspace_path) -> int:
    """Get disk usage for the workspace if exists, zero if not."""
    from reana_db.models import ResourceType

    if (ResourceType.disk.name not in WORKFLOW_TERMINATION_QUOTA_UPDATE_POLICY
            and not PERIODIC_RESOURCE_QUOTA_UPDATE_POLICY):
        return 0

    try:
        disk_bytes = get_disk_usage(workspace_path, summarize=True)
        return int(disk_bytes[0]["size"]["raw"])
    except REANAMissingWorkspaceError:
        return 0