Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change quota input to TB #1579

Draft
wants to merge 13 commits into
base: dev
Choose a base branch
from
1 change: 1 addition & 0 deletions SPRINTLOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -468,3 +468,4 @@ _Nothing merged during this sprint_
- Update documentation regarding 'Upload' or 'Download' added to end of delivery directory name depending on command ([#1580](https://github.com/ScilifelabDataCentre/dds_web/pull/1580))
- Modify the monitor usage command to send warning to the affected unit as well as Data Centre([#1562](https://github.com/ScilifelabDataCentre/dds_web/pull/1562))
- Run npm audit fix to solve node cve's ([#1577](https://github.com/ScilifelabDataCentre/dds_web/pull/1577)
- Change quota input to TB ([#1575](https://github.com/ScilifelabDataCentre/dds_web/pull/1579))
42 changes: 28 additions & 14 deletions dds_web/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def fill_db_wrapper(db_type):
@click.option("--safespring_secret", "-ss", type=str, required=True)
@click.option("--days_in_available", "-da", type=int, required=False, default=90)
@click.option("--days_in_expired", "-de", type=int, required=False, default=30)
@click.option("--quota", "-q", type=int, required=True)
@click.option("--quota", "-q", type=float, required=True, help="Quota in TB")
@click.option("--warn-at", "-w", type=click.FloatRange(0.0, 1.0), required=False, default=0.8)
@flask.cli.with_appcontext
def create_new_unit(
Expand Down Expand Up @@ -128,6 +128,9 @@ def create_new_unit(
flask.current_app.logger.error(error_message)
return

# The quota input is in TB, convert to bytes
quota_bytes = int(quota * 1000**4)

new_unit = models.Unit(
name=name,
public_id=public_id,
Expand All @@ -141,7 +144,7 @@ def create_new_unit(
sto4_secret=safespring_secret,
days_in_available=days_in_available,
days_in_expired=days_in_expired,
quota=quota,
quota=quota_bytes,
warning_level=warn_at,
)
db.session.add(new_unit)
Expand Down Expand Up @@ -207,10 +210,10 @@ def update_unit_sto4(unit_id, sto4_endpoint, sto4_name, sto4_access, sto4_secret

@click.command("update-unit-quota")
@click.option("--unit-id", "-u", type=str, required=True)
@click.option("--quota", "-q", type=int, required=True)
@click.option("--quota", "-q", type=float, required=True, help="Quota in TB")
@flask.cli.with_appcontext
def update_unit_quota(unit_id, quota):
"""Update unit quota. The input is in GB."""
"""Update unit quota. The input is in TB."""
# Imports
import rich.prompt
from dds_web import db
Expand All @@ -222,21 +225,26 @@ def update_unit_quota(unit_id, quota):
flask.current_app.logger.error(f"There is no unit with the public ID '{unit_id}'.")
sys.exit(1)

# calculate quotas for logging purposes
old_quota_tb = round(unit.quota / 1000**4, 4)
old_quota_gb = round(unit.quota / 1000**3, 4)
new_quota_gb = round(quota * 1000, 4)

# ask the user for confirmation
do_update = rich.prompt.Confirm.ask(
f"Current quota for unit '{unit_id}' is {round(unit.quota / 1000 ** 3,2)} GB. \n"
f"You are about to update the quota to {quota} GB ({quota * 1000 ** 3} bytes). \n"
f"Current quota for unit '{unit_id}' is {old_quota_tb} TB ({old_quota_gb} GB). \n"
f"You are about to update the quota to {quota} TB ({new_quota_gb} GB). \n"
"Are you sure you want to continue?"
)
if not do_update:
flask.current_app.logger.info(
f"Cancelling quota update for unit '{unit_id}'. The quota is still {round(unit.quota / 1000 ** 3,2)} GB. ({unit.quota} bytes.)"
f"Cancelling quota update for unit '{unit_id}'. The quota is still {old_quota_tb} TB. ({old_quota_gb} GB)"
)
return

# Set sto4 info
quota_bytes = quota * 1000**3
unit.quota = quota_bytes
# Set new quota info
new_quota_bytes = int(quota * 1000**4)
unit.quota = new_quota_bytes
db.session.commit()

flask.current_app.logger.info(f"Unit '{unit_id}' updated successfully")
Expand Down Expand Up @@ -1277,10 +1285,16 @@ def monitor_usage():
flask.current_app.logger.info(f"Checking quotas and usage for: {unit.name}")

# Get info from database
quota: int = unit.quota
quota: int = unit.quota # in bytes
warn_after: float = unit.warning_level
current_usage: int = unit.size

# convert to TB and GB, only for logs
quota_tb: float = round(quota / 1000**4, 4)
quota_gb: float = round(quota / 1000**3, 4)
current_usage_tb: float = round(current_usage / 1000**4, 4)
current_usage_gb: float = round(current_usage / 1000**3, 4)

# Check if 0 and then skip the next steps
if not current_usage:
flask.current_app.logger.info(
Expand All @@ -1294,9 +1308,9 @@ def monitor_usage():

# Information to log and potentially send
info_string: str = (
f"- Quota:{quota} bytes\n"
f"- Warning level: {int(warn_after*quota)} bytes ({int(warn_after*100)}%)\n"
f"- Current usage: {current_usage} bytes ({perc_used}%)\n"
f"- Quota: {quota_tb} TB // {quota_gb} GB\n"
f"- Warning level: {round(warn_after*quota_tb,4)} TB // {round(warn_after*quota_gb,4)} GB ({int(warn_after*100)}%)\n"
f"- Current usage: {current_usage_tb} TB // {current_usage_gb} GB ({perc_used}%)\n"
)
flask.current_app.logger.debug(
f"Monitoring the usage for unit '{unit.name}' showed the following:\n" + info_string
Expand Down
10 changes: 5 additions & 5 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def create_command_options_from_dict(options: typing.Dict) -> typing.List:
"safespring_secret": "newsafespringsecret",
"days_in_available": 45,
"days_in_expired": 15,
"quota": 80,
"quota": 0.8, # in TB
}


Expand Down Expand Up @@ -292,7 +292,7 @@ def test_create_new_unit_success(client, runner, capfd: LogCaptureFixture) -> No
assert new_unit.sto4_secret == correct_unit["safespring_secret"]
assert new_unit.days_in_available
assert new_unit.days_in_expired
assert new_unit.quota == correct_unit["quota"]
assert new_unit.quota == correct_unit["quota"] * 1000**4 # TB to bytes
assert new_unit.warning_level


Expand Down Expand Up @@ -502,7 +502,7 @@ def test_update_unit_quota_confirm_prompt_False(client, runner, capfd: LogCaptur
"--unit-id",
unit_id,
"--quota",
2, # 2 GB,
2, # 2 TB,
]

# Run command
Expand Down Expand Up @@ -541,7 +541,7 @@ def test_update_unit_quota_confirm_prompt_true(client, runner, capfd: LogCapture
"--unit-id",
unit_id,
"--quota",
2, # 2 GB,
2, # 2 TB,
]

# Run command
Expand All @@ -563,7 +563,7 @@ def test_update_unit_quota_confirm_prompt_true(client, runner, capfd: LogCapture
unit: models.Unit = models.Unit.query.filter_by(public_id=unit_id).first()
assert unit
assert unit.quota != quota_original
assert unit.quota == command_options[3] * 1000**3 # GB to bytes
assert unit.quota == command_options[3] * 1000**4 # TB to bytes


# update_uploaded_file_with_log
Expand Down
Loading