Skip to content

Commit

Permalink
get_job_records(): improve function logic
Browse files Browse the repository at this point in the history
Problem: There are a few improvements that can be made to enhance
readability, efficiency, and maintainability of this function.

Simplify the construction of the params dictionary.

Use f-strings for string formatting of the SELECT statement and WHERE
clauses.

Remove the unnecessary check for len(result) == 0 since we can just
return an empty list if no records are found.
  • Loading branch information
cmoussa1 committed Jun 28, 2024
1 parent ab3f9e6 commit f140aa2
Showing 1 changed file with 18 additions and 33 deletions.
51 changes: 18 additions & 33 deletions src/bindings/python/fluxacct/accounting/job_archive_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,69 +180,54 @@ def def_bank_jobs(job_records, default_bank):


def get_job_records(conn, bank, default_bank, **kwargs):
job_records = []

# find out which args were passed and place them in a dict
valid_params = ("user", "after_start_time", "before_end_time", "jobid")
params = {}
params_list = []

valid_params = {"user", "after_start_time", "before_end_time", "jobid"}
params = {
key: val
for (key, val) in kwargs.items()
for key, val in kwargs.items()
if val is not None and key in valid_params
}

select_stmt = (
"SELECT userid,id,t_submit,t_run,t_inactive,ranks,R,jobspec FROM jobs "
)
where_stmt = ""

def append_to_where(where_stmt, conditional):
if where_stmt != "":
return "{} AND {} ".format(where_stmt, conditional)

return "WHERE {}".format(conditional)
select_stmt = "SELECT userid,id,t_submit,t_run,t_inactive,ranks,R,jobspec FROM jobs"
where_clauses = []
params_list = []

# generate the SELECT statement based on the parameters passed in
if "user" in params:
params["user"] = get_uid(params["user"])
where_clauses.append("userid = ?")
params_list.append(params["user"])
where_stmt = append_to_where(where_stmt, "userid=? ")
if "after_start_time" in params:
where_clauses.append("t_run > ?")
params_list.append(params["after_start_time"])
where_stmt = append_to_where(where_stmt, "t_run > ? ")
if "before_end_time" in params:
where_clauses.append("t_inactive < ?")
params_list.append(params["before_end_time"])
where_stmt = append_to_where(where_stmt, "t_inactive < ? ")
if "jobid" in params:
where_clauses.append("id = ?")
params_list.append(params["jobid"])
where_stmt = append_to_where(where_stmt, "id=? ")

select_stmt += where_stmt
if where_clauses:
select_stmt += " WHERE " + " AND ".join(where_clauses)

cur = conn.cursor()
cur.execute(select_stmt, (*tuple(params_list),))
cur.execute(select_stmt, tuple(params_list))
result = cur.fetchall()
# if the length of dataframe is 0, that means no job records were found
# in the jobs table, so just return an empty list
if len(result) == 0:
return job_records

if not result:
return []

if bank is None and default_bank is None:
# special case for unit tests in test_job_archive_interface.py
job_records = add_job_records(result)

return job_records

return add_job_records(result)

if bank != default_bank:
jobs = sec_bank_jobs(result, bank)
else:
jobs = def_bank_jobs(result, default_bank)

job_records = add_job_records(jobs)

return job_records
return add_job_records(result)


def output_job_records(conn, output_file, **kwargs):
Expand Down

0 comments on commit f140aa2

Please sign in to comment.