-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update crc-usage, crc-sus, and crc_proposal_end to gather information…
… from keystone (#233) * Start on building a usage table with info from keystone instead of crc-bank * utility functions for interacting with keystone * pull function for getting allocations by primary key * add requests * verify provided slurm account against keystone as well as slurm * properly handle dates and pretty table rows * split output into summary and usage tables, keep track of per cluster usage * PEP fixes and comments * Update crc_sus.py Output remaining SUs on each clsuter using keystone requests and sreport * adjustments from meeting * filter on active and expiration date in request for allocations * add and move slurm functions for gathering usage data into utils.system_info * move slurm functions out of utils.keystone * add usage table setup * small fix with field_names * fix imports, use account_name in checking for whether slurm account exists * fix adding total to out_data * clean up usage output formatting * small formatting changes to crc_sus * formatting adjustment * add request primary key as query param for requests * use cluster totals instead of allocation totals, other review items * use request pk when gathering allocations * use check_slurm_account_exists * working state for crc-sus and crc-usage * fix crc_usage test that wa failing * pull duplicate functionality for running subprocesses * small typo in crc_usage test string * crc-proposal-end (#232) * Update crc_proposal_end.py Added authentication procedure * Update crc_proposal_end.py * Update crc_proposal_end.py * Update crc_proposal_end.py Updated requests with group_id * Update crc_proposal_end.py Update requests * working version of crc_proposal_end * remove unused imports --------- Co-authored-by: Nickolas Comeau <[email protected]> * fix expected output string * codacy / PEP8 items * pull crc-bank from dependencies * add prettytable to deps * needs to be an f string * fix conversion to percent that was broken * more broken division * Rework utility functions to be more specific, adjust wrappers to utilize them * use reworked utility functions in crc_usage and build/print summary and usage tables in separate functions * codacy fixes * fix bug where all allocations appear under each request * researchgroup -> researchgroup ID, per_request option for per cluster totals * user per_cluster_totals * fix bare except * small codacy fixes --------- Co-authored-by: chnixi <[email protected]>
- Loading branch information
Showing
8 changed files
with
271 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
"""Utility functions used across various wrappers for interacting with keystone""" | ||
|
||
from datetime import date | ||
|
||
import requests | ||
|
||
KEYSTONE_URL = "https://keystone.crc.pitt.edu" | ||
CLUSTERS = {1: 'MPI', 2: 'SMP', 3: 'HTC', 4: 'GPU'} | ||
|
||
|
||
def get_auth_header(keystone_url: str, auth_header: dict) -> dict: | ||
""" Generate an authorization header to be used for accessing information from keystone""" | ||
|
||
response = requests.post(f"{keystone_url}/authentication/new/", json=auth_header) | ||
response.raise_for_status() | ||
tokens = response.json() | ||
return {"Authorization": f"Bearer {tokens['access']}"} | ||
|
||
|
||
def get_request_allocations(keystone_url: str, request_pk: int, auth_header: dict) -> dict: | ||
"""Get All Allocation information from keystone for a given request""" | ||
|
||
response = requests.get(f"{keystone_url}/allocations/allocations/?request={request_pk}", headers=auth_header) | ||
response.raise_for_status() | ||
return response.json() | ||
|
||
|
||
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) | ||
response.raise_for_status() | ||
return [request for request in response.json() | ||
if date.fromisoformat(request['active']) <= date.today() < date.fromisoformat(request['expire'])] | ||
|
||
|
||
def get_researchgroup_id(keystone_url: str, account_name: str, auth_header: dict) -> int: | ||
"""Get the Researchgroup ID from keystone for the specified Slurm account""" | ||
|
||
response = requests.get(f"{keystone_url}/users/researchgroups/?name={account_name}", headers=auth_header) | ||
response.raise_for_status() | ||
|
||
try: | ||
group_id = int(response.json()[0]['id']) | ||
except IndexError: | ||
group_id = None | ||
|
||
return group_id | ||
|
||
|
||
def get_earliest_startdate(alloc_requests: [dict]) -> date: | ||
"""Given a number of requests, determine the earliest start date across them""" | ||
|
||
earliest_date = date.today() | ||
for request in alloc_requests: | ||
start = date.fromisoformat(request['active']) | ||
if start < earliest_date: | ||
earliest_date = start | ||
|
||
return earliest_date | ||
|
||
|
||
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""" | ||
|
||
per_cluster_totals = {} | ||
for request in alloc_requests: | ||
if per_request: | ||
per_cluster_totals[request['id']] = {} | ||
for allocation in get_request_allocations(KEYSTONE_URL, request['id'], auth_header): | ||
cluster = CLUSTERS[allocation['cluster']] | ||
if per_request: | ||
per_cluster_totals[request['id']].setdefault(cluster, 0) | ||
per_cluster_totals[request['id']][cluster] += allocation['awarded'] | ||
else: | ||
per_cluster_totals.setdefault(cluster, 0) | ||
per_cluster_totals[cluster] += allocation['awarded'] | ||
|
||
return per_cluster_totals |
Oops, something went wrong.