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

Revert "Update Wrappers to utilize New CRC Bank Code (#195)" #211

Merged
merged 1 commit into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 31 additions & 9 deletions apps/crc_proposal_end.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
30 changes: 19 additions & 11 deletions apps/crc_sus.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand All @@ -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

Expand All @@ -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)

Expand Down
8 changes: 4 additions & 4 deletions apps/crc_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""

Expand All @@ -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}'))
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/test_crc_sus.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)