From 055b355df224035a518d4529bb672675d318e653 Mon Sep 17 00:00:00 2001 From: Daniel Perrefort Date: Fri, 18 Aug 2023 12:53:24 -0400 Subject: [PATCH] Revert "Update Wrappers to utilize New CRC Bank Code (#195)" This reverts commit de647a3aeb7af230ee746530c98f367c88488276. --- apps/crc_proposal_end.py | 40 +++++++++++++++++++++++++++++++--------- apps/crc_sus.py | 30 +++++++++++++++++++----------- apps/crc_usage.py | 8 ++++---- pyproject.toml | 2 +- tests/test_crc_sus.py | 4 ++-- 5 files changed, 57 insertions(+), 27 deletions(-) diff --git a/apps/crc_proposal_end.py b/apps/crc_proposal_end.py index 1070a66..b8508c7 100755 --- a/apps/crc_proposal_end.py +++ b/apps/crc_proposal_end.py @@ -4,26 +4,48 @@ and will not work without a running bank installation. """ -import grp -import os from argparse import Namespace from datetime import datetime -from bank.account_logic import AccountServices +import dataset from .utils.cli import BaseParser +from .utils.system_info import Shell class CrcProposalEnd(BaseParser): """Display the end date for an account's current CRC proposal.""" + banking_db_path = 'sqlite:////ihome/crc/bank/crc_bank.db' + def __init__(self) -> None: """Define arguments for the command line interface""" super(CrcProposalEnd, self).__init__() - default_group = grp.getgrgid(os.getgid()).gr_name - help_text = f"SLURM account name [defaults to your primary group: {default_group}]" - self.add_argument('account', nargs='?', default=default_group, help=help_text) + + default_group = Shell.run_command("id -gn") + self.add_argument( + 'account', default=default_group, nargs='?', + help=f'the name of the Slurm account [default: {default_group}]') + + def get_proposal_end_date(self, account: str) -> datetime: + """Get the proposal end date for a given account + + Args: + account: The name of the account + + Returns: + The proposal end date as a ``datetime`` object + """ + + database = dataset.connect(self.banking_db_path, sqlite_wal_mode=False) + table = database['proposal'] + + db_record = table.find_one(account=account) + if db_record is None: + self.error(f"The account: {account} doesn't appear to exist") + + return db_record['end_date'] def app_logic(self, args: Namespace) -> None: """Logic to evaluate when executing the application @@ -32,8 +54,8 @@ def app_logic(self, args: Namespace) -> None: args: Parsed command line arguments """ - acct = AccountServices(args.account) - end_date = acct._get_active_proposal_end_date() + end_date = self.get_proposal_end_date(args.account) # Format the account name and end date as an easy-to-read string - print(f"The active proposal for account {args.account} ends on {end_date}") + date_str = end_date.strftime("%m/%d/%y") + print(f"Proposal ends on {args.account} for account {date_str} on H2P") diff --git a/apps/crc_sus.py b/apps/crc_sus.py index 606cb71..fe97df5 100755 --- a/apps/crc_sus.py +++ b/apps/crc_sus.py @@ -3,25 +3,29 @@ This application is designed to interface with the CRC banking application and will not work without a running bank installation. """ + import grp import os from argparse import Namespace from typing import Dict -from bank.account_logic import AccountServices +import dataset from .utils.cli import BaseParser +from .utils.system_info import Slurm class CrcSus(BaseParser): """Display the number of service units allocated to an account.""" + banking_db_path = 'sqlite:////ihome/crc/bank/crc_bank.db' + def __init__(self) -> None: """Define the application commandline interface""" super().__init__() default_group = grp.getgrgid(os.getgid()).gr_name - help_text = f"SLURM account name [defaults to your primary group: {default_group}]" + help_text = "slurm account name (defaults to the current user's primary group name)" self.add_argument('account', nargs='?', default=default_group, help=help_text) def get_allocation_info(self, account: str) -> Dict[str, int]: @@ -34,12 +38,20 @@ def get_allocation_info(self, account: str) -> Dict[str, int]: A dictionary mapping cluster names to the number of service units """ - acct = AccountServices(account) - allocs = acct._get_active_proposal_allocation_info() + # Connect to the database and get the table with proposal service units + database = dataset.connect(self.banking_db_path, sqlite_wal_mode=False) + table = database['proposal'] + + # Ensure a proposal exists for the given account + db_record = table.find_one(account=account) + if db_record is None: + raise ValueError('ERROR: No proposal for the given account was found') + # Convert the DB record into a dictionary allocations = dict() - for cluster in allocs: - allocations[cluster.cluster_name] = cluster.service_units_total - cluster.service_units_used + for cluster in Slurm.get_cluster_names(): + if cluster in db_record: + allocations[cluster] = db_record[cluster] return allocations @@ -60,11 +72,7 @@ def build_output_string(account: str, **allocation: int) -> str: # Right justify cluster names to the same length cluster_name_length = max(len(cluster) for cluster in allocation) for cluster, sus in allocation.items(): - if sus > 0: - out = f' cluster {cluster:>{cluster_name_length}} has {sus:,} SUs remaining' - else: - out = f" cluster {cluster:>{cluster_name_length}} is LOCKED due to exceeding usage limits" - output_lines.append(out) + output_lines.append(f' cluster {cluster:>{cluster_name_length}} has {sus:,} SUs') return '\n'.join(output_lines) diff --git a/apps/crc_usage.py b/apps/crc_usage.py index c4d7537..77db686 100755 --- a/apps/crc_usage.py +++ b/apps/crc_usage.py @@ -8,14 +8,15 @@ import os from argparse import Namespace -from bank.account_logic import AccountServices - from .utils.cli import BaseParser from .utils.system_info import Shell + class CrcUsage(BaseParser): """Display a Slurm account's cluster usage.""" + banking_executable = '/ihome/crc/bank/crc_bank.py usage' + def __init__(self) -> None: """Define the application commandline interface""" @@ -36,5 +37,4 @@ def app_logic(self, args: Namespace) -> None: if not account_exists: raise RuntimeError(f"No slurm account was found with the name '{args.account}'.") - account = AccountServices(args.account) - print(account._build_usage_table()) + print(Shell.run_command(f'{self.banking_executable} {args.account}')) diff --git a/pyproject.toml b/pyproject.toml index a808768..2c69881 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -43,7 +43,7 @@ crc-usage = "apps.crc_usage:CrcUsage.execute" [tool.poetry.dependencies] python = "^3.8.0" -crc-bank = "^0.2.1" +dataset = "1.6.0" [tool.poetry.group.tests] optional = true diff --git a/tests/test_crc_sus.py b/tests/test_crc_sus.py index 3ad2b0a..b6b0a7d 100644 --- a/tests/test_crc_sus.py +++ b/tests/test_crc_sus.py @@ -33,8 +33,8 @@ def test_output_matches_string(self) -> None: output_string = CrcSus().build_output_string(account='sam', smp=10, htc=20) expected_string = ( 'Account sam\n' - ' cluster smp has 10 SUs remaining\n' - ' cluster htc has 20 SUs remaining' + ' cluster smp has 10 SUs\n' + ' cluster htc has 20 SUs' ) self.assertEqual(expected_string, output_string)