Skip to content

Commit

Permalink
reana-admin: list quota usage of users
Browse files Browse the repository at this point in the history
  • Loading branch information
Adelina Lintuluoto authored and Diego Rodriguez committed Oct 20, 2020
1 parent 33aac1f commit e603c11
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 2 deletions.
85 changes: 84 additions & 1 deletion reana_server/reana_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import tablib
from flask.cli import with_appcontext
from invenio_accounts.utils import register_user
from reana_commons.config import REANAConfig
from reana_commons.config import REANAConfig, REANA_RESOURCE_HEALTH_COLORS
from reana_commons.email import send_email
from reana_commons.errors import REANAEmailNotificationError
from reana_commons.utils import click_table_printer
Expand Down Expand Up @@ -381,3 +381,86 @@ def _format_statuses(type_, statuses):
fg="red",
err=True,
)


@reana_admin.command("quota-usage", help="List quota usage of users.")
@click.option("--id", help="The id of the user.")
@click.option("-e", "--email", help="The email of the user.")
@click.option("--user-access-token", help="The access token of the user.")
@click.option(
"--json",
"output_format",
flag_value="json",
default=None,
help="Get output in JSON format.",
)
@click.option(
"-h",
"--human-readable",
"human_readable",
is_flag=True,
default=False,
callback=lambda ctx, param, value: "human_readable" if value else "raw",
help="Show quota usage values in human readable format.",
)
@admin_access_token_option
@click.pass_context
def list_quota_usage(
ctx, id, email, user_access_token, admin_access_token, output_format, human_readable
):
"""List quota usage of users."""
try:
response = _get_users(id, email, user_access_token, admin_access_token)
headers = ["id", "email", "cpu-used", "cpu-limit", "disk-used", "disk-limit"]
health_order = {"healthy": 0, "warning": 1, "critical": 2}
data = []
colours = []
health = []
for user in response:
(disk, cpu,) = user.get_quota_usage().values()
data.append(
(
str(user.id_),
user.email,
cpu.get("usage").get(human_readable),
cpu.get("limit", {}).get(human_readable) or "-",
disk.get("usage").get(human_readable),
disk.get("limit", {}).get(human_readable) or "-",
)
)

health_ordered = max(
[disk.get("health"), cpu.get("health")],
key=lambda key: health_order[key],
)
colours.append(REANA_RESOURCE_HEALTH_COLORS[health_ordered])
health.append(health_ordered)

data, colours, _ = (
list(t)
for t in zip(
*sorted(
zip(data, colours, health),
key=lambda t: health_order[t[2]],
reverse=True,
)
)
)

if output_format:
tablib_data = tablib.Dataset()
tablib_data.headers = headers
for row in data:
tablib_data.append(row)

click.echo(tablib_data.export(output_format))
else:
click_table_printer(headers, [], data, colours)

except Exception as e:
logging.debug(traceback.format_exc())
logging.debug(str(e))
click.echo(
click.style("User could not be retrieved: \n{}".format(str(e)), fg="red"),
err=True,
)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
install_requires = [
"marshmallow>2.13.0,<=2.20.1",
"pyOpenSSL==17.5.0",
"reana-commons[kubernetes]>=0.8.0a4,<0.9.0",
"reana-commons[kubernetes]>=0.8.0a5,<0.9.0",
"reana-db>=0.8.0a5,<0.9.0",
"requests==2.20.0",
"rfc3987==1.3.7",
Expand Down

0 comments on commit e603c11

Please sign in to comment.