Skip to content

Commit

Permalink
Show most recently expired allocation info when there are no active a…
Browse files Browse the repository at this point in the history
…llocations (#235)

* Show info from most recently expired allocation when no active allocations are found

* make the message about not finding active allocations stand out more

* fix typo

* use bold and red color

* Codacy complaining about line lengths
  • Loading branch information
Comeani authored May 16, 2024
1 parent 9d8a752 commit b69254f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 18 deletions.
9 changes: 5 additions & 4 deletions apps/crc_proposal_end.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ def app_logic(self, args: Namespace) -> None:
keystone_group_id = get_researchgroup_id(KEYSTONE_URL, args.account, auth_header)
alloc_requests = get_active_requests(KEYSTONE_URL, keystone_group_id, auth_header)

if not (keystone_group_id and requests):
print(f"No active allocation information found in accounting system for '{args.account}'")
exit()
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")
alloc_requests = get_most_recent_expired_request(KEYSTONE_URL, keystone_group_id, auth_header)

for request in alloc_requests:
print(f"Resource Allocation Request: '{request['title']}' ends on {request['expire']} ")
print(f"'{request['title']}' ends on {request['expire']} ")
7 changes: 4 additions & 3 deletions apps/crc_sus.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,10 @@ def app_logic(self, args: Namespace) -> None:
# Determine if provided or default account is in Keystone
keystone_group_id = get_researchgroup_id(KEYSTONE_URL, args.account, auth_header)
alloc_requests = get_active_requests(KEYSTONE_URL, keystone_group_id, auth_header)
if not (keystone_group_id and alloc_requests):
print(f"No active allocation information found in accounting system for '{args.account}'")
exit()
if not alloc_requests:
print(f"\033[91m\033[1mNo active allocation information found in accounting system for '{args.account}'!\n")
print("Showing SUs for most recently expired Resource Allocation Request:\033[0m")
alloc_requests = get_most_recent_expired_request(KEYSTONE_URL, keystone_group_id, auth_header)

per_cluster_totals = get_per_cluster_totals(alloc_requests, auth_header)
earliest_date = get_earliest_startdate(alloc_requests)
Expand Down
11 changes: 4 additions & 7 deletions apps/crc_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,12 @@ def app_logic(self, args: Namespace) -> None:

# Gather AllocationRequests from Keystone
keystone_group_id = get_researchgroup_id(KEYSTONE_URL, args.account, auth_header)
if not keystone_group_id:
print(f"No Slurm Account found in the accounting system for '{args.account}'. \n"
f"Please submit a ticket to the CRC team to ensure your allocation was properly configured")
exit()

alloc_requests = get_active_requests(KEYSTONE_URL, keystone_group_id, auth_header)
if not (keystone_group_id and alloc_requests):
print(f"No active allocation data found in the accounting system for '{args.account}'")
exit()
if not alloc_requests:
print(f"\033[91m\033[1mNo active allocation information found in accounting system for '{args.account}'!\n")
print("Showing usage information for most recently expired Resource Allocation Request: \033[0m")
alloc_requests = get_most_recent_expired_request(KEYSTONE_URL, keystone_group_id, auth_header)

self.print_summary_table(alloc_requests,
args.account,
Expand Down
24 changes: 20 additions & 4 deletions apps/utils/keystone.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ def get_request_allocations(keystone_url: str, request_pk: int, auth_header: dic
def get_active_requests(keystone_url: str, group_pk: int, auth_header: dict) -> [dict]:
"""Get all active AllocationRequest information from keystone for a given group"""

response = requests.get(f"{keystone_url}/allocations/requests/?group={group_pk}&status=AP", headers=auth_header)
today = date.today().isoformat()
response = requests.get(
f"{keystone_url}/allocations/requests/?group={group_pk}&status=AP&active__lte={today}&expire__gt={today}",
headers=auth_header)
response.raise_for_status()
return [request for request in response.json()
if date.fromisoformat(request['active']) <= date.today() < date.fromisoformat(request['expire'])]
return [request for request in response.json()]


def get_researchgroup_id(keystone_url: str, account_name: str, auth_header: dict) -> int:
Expand All @@ -43,7 +45,9 @@ def get_researchgroup_id(keystone_url: str, account_name: str, auth_header: dict
try:
group_id = int(response.json()[0]['id'])
except IndexError:
group_id = None
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 group_id

Expand All @@ -60,6 +64,18 @@ def get_earliest_startdate(alloc_requests: [dict]) -> date:
return earliest_date


def get_most_recent_expired_request(keystone_url: str, group_pk: int, auth_header: dict) -> [dict]:
"""Get the single most recently expired AllocationRequest information from keystone for a given group"""

today = date.today().isoformat()
response = requests.get(
f"{keystone_url}/allocations/requests/?ordering=-expire&group={group_pk}&status=AP&expire__lte={today}",
headers=auth_header)
response.raise_for_status()

return [response.json()[0]]


def get_per_cluster_totals(alloc_requests: [dict], auth_header: dict, per_request: bool = False) -> dict:
"""Gather the awarded totals across the given requests on each cluster into a dictionary"""

Expand Down

0 comments on commit b69254f

Please sign in to comment.