From df79d86a2b1543acd548d553765fbc8ff6c53312 Mon Sep 17 00:00:00 2001 From: Comeani Date: Thu, 12 Dec 2024 13:03:38 -0500 Subject: [PATCH 1/2] adjust utils functions to use search and order where applicable --- apps/utils/keystone.py | 33 +++++++++++---------------------- 1 file changed, 11 insertions(+), 22 deletions(-) diff --git a/apps/utils/keystone.py b/apps/utils/keystone.py index 4d73f63..7b4d173 100644 --- a/apps/utils/keystone.py +++ b/apps/utils/keystone.py @@ -21,26 +21,13 @@ def get_request_allocations(session: KeystoneClient, request_pk: int) -> dict: return session.retrieve_allocation(filters={'request': request_pk}) -def get_active_requests(session: KeystoneClient, team_pk: int) -> [dict]: +def get_active_requests(session: KeystoneClient, account_name: str) -> [dict]: """Get all active AllocationRequest information from keystone for a given team""" today = date.today().isoformat() return session.retrieve_request( - filters={'team': team_pk, 'status': 'AP', 'active__lte': today, 'expire__gt': today}) - - -def get_team_id(session: KeystoneClient, account_name: str) -> int: - """Get the Team ID from keystone for the specified Slurm account""" - - # Attempt to get the primary key for the Team - try: - keystone_team_id = session.retrieve_team(filters={'name': account_name})[0]['id'] - except IndexError: - print(f"No Slurm Account found in the accounting system for '{account_name}'. \n" - f"Please submit a ticket to the CRC team to ensure your allocation was properly configured") - exit() - - return keystone_team_id + search=account_name, + filters={'status': 'AP', 'active__lte': today, 'expire__gt': today}) def get_earliest_startdate(alloc_requests: [dict]) -> date: @@ -56,12 +43,14 @@ def get_earliest_startdate(alloc_requests: [dict]) -> date: return max(earliest_date, RAWUSAGE_RESET_DATE) -def get_most_recent_expired_request(session: KeystoneClient, team_pk: int) -> [dict]: +def get_most_recent_expired_request(session: KeystoneClient, account_name: str) -> [dict]: """Get the single most recently expired AllocationRequest information from keystone for a given team""" today = date.today().isoformat() return session.retrieve_request( - filters={'team': team_pk, 'status': 'AP', 'ordering': '-expire', 'expire__lte': today})[0] + search=account_name, + order='-expire', + filters={'status': 'AP', 'expire__lte': today})[0] def get_enabled_cluster_ids(session: KeystoneClient) -> dict(): @@ -75,10 +64,10 @@ def get_enabled_cluster_ids(session: KeystoneClient) -> dict(): def get_per_cluster_totals(session: KeystoneClient, - alloc_requests: [dict], - clusters: dict, - per_request: bool = False -) -> dict: + alloc_requests: [dict], + clusters: dict, + per_request: bool = False + ) -> dict: """Gather the awarded totals across the given requests on each cluster into a dictionary""" per_cluster_totals = {} From 3771b273d87b00c4e44cb617754129d0c0d0cbfe Mon Sep 17 00:00:00 2001 From: Comeani Date: Thu, 12 Dec 2024 13:04:40 -0500 Subject: [PATCH 2/2] pull gathering team_ids from keystone-interfacing wrappers in favor of searching on the slurm account name --- apps/crc_proposal_end.py | 9 ++++----- apps/crc_sus.py | 5 ++--- apps/crc_usage.py | 5 ++--- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/apps/crc_proposal_end.py b/apps/crc_proposal_end.py index 5890da7..1edb219 100755 --- a/apps/crc_proposal_end.py +++ b/apps/crc_proposal_end.py @@ -37,18 +37,17 @@ def app_logic(self, args: Namespace) -> None: keystone_session.login(username=os.environ["USER"], password=getpass("Please enter your CRCD login password:\n")) - team_id = get_team_id(keystone_session, args.account) - alloc_requests = get_active_requests(keystone_session, team_id) + alloc_requests = get_active_requests(keystone_session, args.account) if not alloc_requests: print(f"\033[91m\033[1mNo active allocation information found in accounting system for '{args.account}'!\n") print("Showing end date for most recently expired Resource Allocation Request:\033[0m \n") try: - alloc_requests = [get_most_recent_expired_request(keystone_session, team_id)] + alloc_requests = [get_most_recent_expired_request(keystone_session, args.account)] except IndexError: print("\033[91m\033[1mNo allocation information found. Either the group does not have any allocations, " - "or you do not have permissions to view them. If you believe this to be a mistake, please submit a " - "help ticket to the CRCD team. \033[0m \n") + "or you do not have permissions to view them. If you believe this to be a mistake, please submit " + "a help ticket to the CRCD team. \033[0m \n") exit() for request in alloc_requests: diff --git a/apps/crc_sus.py b/apps/crc_sus.py index 5cf6d73..d5e6f74 100755 --- a/apps/crc_sus.py +++ b/apps/crc_sus.py @@ -62,15 +62,14 @@ def app_logic(self, args: Namespace) -> None: keystone_session.login(username=os.environ["USER"], password=getpass("Please enter your CRCD login password:\n")) - group_id = get_team_id(keystone_session, args.account) - alloc_requests = get_active_requests(keystone_session, group_id) + alloc_requests = get_active_requests(keystone_session, args.account) if not alloc_requests: print(f"\033[91m\033[1mNo active allocation information found in accounting system for '{args.account}'!\n") print("Showing remaining service unit amounts for most recently expired Resource Allocation Request:" "\033[0m \n") try: - alloc_requests = [get_most_recent_expired_request(keystone_session, group_id)] + alloc_requests = [get_most_recent_expired_request(keystone_session, args.account)] except IndexError: print("\033[91m\033[1mNo allocation information found. Either the group does not have any allocations, " "or you do not have permissions to view them. If you believe this to be a mistake, please submit " diff --git a/apps/crc_usage.py b/apps/crc_usage.py index 1996c5f..6d1daa3 100755 --- a/apps/crc_usage.py +++ b/apps/crc_usage.py @@ -100,14 +100,13 @@ def app_logic(self, args: Namespace) -> None: password=getpass("Please enter your CRCD login password:\n")) # Gather AllocationRequests from Keystone - group_id = get_team_id(keystone_session, args.account) - alloc_requests = get_active_requests(keystone_session, group_id) + alloc_requests = get_active_requests(keystone_session, args.account) if not alloc_requests: print(f"\033[91m\033[1mNo active allocation information found in accounting system for '{args.account}'!\n") print("Attempting to show the most recently expired Resource Allocation Request info: \033[0m \n") try: - alloc_requests = [get_most_recent_expired_request(keystone_session, group_id)] + alloc_requests = [get_most_recent_expired_request(keystone_session, args.account)] except IndexError: print("\033[91m\033[1mNo allocation information found. Either the group does not have any allocations, " "or you do not have permissions to view them. If you believe this to be a mistake, please submit "