Skip to content

Commit

Permalink
Bank update (#212)
Browse files Browse the repository at this point in the history
* Include new bank as dependency, update connection using DBConnection

* Refactor how SU amounts are pulled from the database

* Update proposal end wrapper

* Build usage output from AccountServices object

* Include Shell again

* update to use functions from bank

* update crc_sus test

* trailing whitespace

* use grp + os instead of shell, get proposal end date in app logic

* update crc-bank requirement

* specify version for bank

* check version 0.0.0

* Use 2.1.0 for bank

* Use the right version this time

* bump crc-bank dependency version

* Make bank DB location in documentation build

* Create bank DB location in Package Test

* adjust mkdir for bank db location

* Try adding execute permissions to Documentation Build workflow file

* Use sudo on the mkdir for bank DB location
  • Loading branch information
Comeani authored Aug 29, 2023
1 parent 94cb821 commit 6a3b219
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 57 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/DocumentationBuild.yml
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ jobs:
with:
virtualenvs-create: false

- name: Set Up Bank DB Location
run: sudo mkdir -p /ihome/crc/bank

- name: Install dependencies
shell: bash
run: poetry install --with docs
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/PackageTest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ jobs:
- name: Setup environment
run: /usr/local/bin/entrypoint.sh

- name: Set Up Bank DB Location
run: mkdir -p /ihome/crc/bank

- name: Checkout repository
uses: actions/checkout@v3

Expand Down
40 changes: 9 additions & 31 deletions apps/crc_proposal_end.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,26 @@
and will not work without a running bank installation.
"""

import grp
import os
from argparse import Namespace
from datetime import datetime

import dataset
from bank.account_logic import AccountServices

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 = 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']
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)

def app_logic(self, args: Namespace) -> None:
"""Logic to evaluate when executing the application
Expand All @@ -54,8 +32,8 @@ def app_logic(self, args: Namespace) -> None:
args: Parsed command line arguments
"""

end_date = self.get_proposal_end_date(args.account)
acct = AccountServices(args.account)
end_date = acct._get_active_proposal_end_date()

# Format the account name and end date as an easy-to-read string
date_str = end_date.strftime("%m/%d/%y")
print(f"Proposal ends on {args.account} for account {date_str} on H2P")
print(f"The active proposal for account {args.account} ends on {end_date}")
30 changes: 11 additions & 19 deletions apps/crc_sus.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,25 @@
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

import dataset
from bank.account_logic import AccountServices

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 = "slurm account name (defaults to the current user's primary group 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)

def get_allocation_info(self, account: str) -> Dict[str, int]:
Expand All @@ -38,20 +34,12 @@ def get_allocation_info(self, account: str) -> Dict[str, int]:
A dictionary mapping cluster names to the number of service units
"""

# 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')
acct = AccountServices(account)
allocs = acct._get_active_proposal_allocation_info()

# Convert the DB record into a dictionary
allocations = dict()
for cluster in Slurm.get_cluster_names():
if cluster in db_record:
allocations[cluster] = db_record[cluster]
for cluster in allocs:
allocations[cluster.cluster_name] = cluster.service_units_total - cluster.service_units_used

return allocations

Expand All @@ -72,7 +60,11 @@ 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():
output_lines.append(f' cluster {cluster:>{cluster_name_length}} has {sus:,} SUs')
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)

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,15 +8,14 @@
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 @@ -37,4 +36,5 @@ def app_logic(self, args: Namespace) -> None:
if not account_exists:
raise RuntimeError(f"No slurm account was found with the name '{args.account}'.")

print(Shell.run_command(f'{self.banking_executable} {args.account}'))
account = AccountServices(args.account)
print(account._build_usage_table())
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"
dataset = "1.6.0"
crc-bank = "^0.2.3"

[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\n'
' cluster htc has 20 SUs'
' cluster smp has 10 SUs remaining\n'
' cluster htc has 20 SUs remaining'
)

self.assertEqual(expected_string, output_string)

0 comments on commit 6a3b219

Please sign in to comment.