Skip to content

Commit

Permalink
Allow queries for all users when since is set
Browse files Browse the repository at this point in the history
  • Loading branch information
troycomi committed Mar 17, 2022
1 parent e648484 commit 481ac08
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "reportseff"
version = "2.0.1"
version = "2.1.0"
description= "Tablular seff output"
authors = ["Troy Comi <[email protected]>"]
license = "MIT"
Expand Down
8 changes: 7 additions & 1 deletion src/reportseff/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,15 @@ def get_jobs(

inquirer.set_since(since)

add_jobs = False

try:
if user:
inquirer.set_user(user)
add_jobs = True
elif inquirer.has_since() and not jobs: # since is set
inquirer.all_users()
add_jobs = True
else:
job_collection.set_jobs(jobs)

Expand All @@ -161,7 +167,7 @@ def get_jobs(
db_output = get_db_output(inquirer, renderer, job_collection, debug)
for entry in db_output:
try:
job_collection.process_entry(entry, user_provided=(user != ""))
job_collection.process_entry(entry, add_job=add_jobs)
except Exception as error:
click.echo(f"Error processing entry: {entry}", err=True)
raise error
Expand Down
27 changes: 27 additions & 0 deletions src/reportseff/db_inquirer.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ def set_user(self, user: str) -> None:
user: user name
"""

@abstractmethod
def all_users(self) -> None:
"""Ignore provided jobs, query for all users."""

@abstractmethod
def set_state(self, state: str) -> None:
"""Set the state to include output jobs.
Expand All @@ -74,6 +78,14 @@ def set_since(self, since: str) -> None:
will subract that amount from the current time
"""

@abstractmethod
def has_since(self) -> bool:
"""Tests if `since` has been set.
Returns:
True if set_since has been called on this inquirer
"""


class SacctInquirer(BaseInquirer):
"""Implementation of BaseInquirer for the sacct slurm function."""
Expand All @@ -85,6 +97,7 @@ def __init__(self) -> None:
self.state: Optional[Set] = None
self.not_state: Optional[Set] = None
self.since: Optional[str] = None
self.query_all_users: bool = False

def get_valid_formats(self) -> List[str]:
"""Get the valid formatting options supported by the inquirer.
Expand Down Expand Up @@ -137,6 +150,8 @@ def get_db_output(
start_date = datetime.date.today() - datetime.timedelta(days=7)
self.since = start_date.strftime("%m%d%y") # MMDDYY
args += [f"--user={self.user}", f"--starttime={self.since}"]
elif self.query_all_users:
args += ["--allusers", f"--starttime={self.since}"]
else:
args += ["--jobs=" + ",".join(jobs)]
if self.since:
Expand Down Expand Up @@ -180,6 +195,10 @@ def set_user(self, user: str) -> None:
"""
self.user = user

def all_users(self) -> None:
"""Query for all users if `since` is set."""
self.query_all_users = True

def set_state(self, state: str) -> None:
"""Set the state to include output jobs.
Expand Down Expand Up @@ -260,6 +279,14 @@ def set_since(self, since: str) -> None:
else:
self.since = since

def has_since(self) -> bool:
"""Check if since has been set.
Returns:
True if since has been set properly
"""
return bool(self.since)


def get_states_as_set(state_list: str) -> Set:
"""Helper method to parse the state string.
Expand Down
8 changes: 3 additions & 5 deletions src/reportseff/job_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,21 +140,19 @@ def add_job(self, job: str, jobid: str, filename: Optional[str] = None) -> None:
"""
self.jobs[jobid] = Job(job, jobid, filename)

def process_entry(self, entry: Dict, user_provided: bool = False) -> None:
def process_entry(self, entry: Dict, add_job: bool = False) -> None:
"""Update the jobs collection with information from the provided entry.
Args:
entry: the accout entry from a db inquirer
user_provided: switch to indicate the jobs are from a user query
if true, will short logic to ensure the entry matches a requested
job
add_job: if true, will add the job to the collection if it doesn't exist
"""
job_id = entry["JobID"].split(".")[0]
job_id_raw = entry["JobIDRaw"].split(".")[0]
if job_id not in self.jobs:
match = self.job_regex.match(job_id)
# job is in jobs
if match and (match.group("job") in self.jobs or user_provided):
if match and (match.group("job") in self.jobs or add_job):
self.add_job(match.group("job"), job_id)
# check if the job_id is an array job
elif job_id_raw in self.jobs:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_job_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ def test_process_entry_array_user(jobs):
"State": "PENDING",
"TotalCPU": "00:00:00",
},
user_provided=True,
add_job=True,
)
expected_job = Job("14729857", "14729857_[737-999]", None)
expected_job.state = "PENDING"
Expand Down

0 comments on commit 481ac08

Please sign in to comment.