Skip to content

Commit 5878c3c

Browse files
committed
slurm2sql: Make sacct2 and seff2 specially handle --user in conjunction with --db
1 parent e4cbc04 commit 5878c3c

File tree

1 file changed

+31
-11
lines changed

1 file changed

+31
-11
lines changed

slurm2sql.py

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -994,6 +994,12 @@ def process_sacct_filter(args, sacct_filter):
994994
# Set for completed jobs.
995995
if getattr(args, 'completed', None):
996996
sacct_filter[:0] = ['--endtime=now', f'--state={COMPLETED_STATES}']
997+
if getattr(args, 'user', None):
998+
sacct_filter[:0] = [f'--user={args.user}']
999+
# Set args.user to None. We have already handled it here and
1000+
# it shouldn't be re-handled in the future SQL code (future
1001+
# SQL woludn't handle multiple users, for example).
1002+
args.user = None
9971003
return sacct_filter
9981004

9991005
def import_or_open_db(args, sacct_filter, csv_input=None):
@@ -1006,17 +1012,17 @@ def import_or_open_db(args, sacct_filter, csv_input=None):
10061012
db: filename of a database to open
10071013
10081014
"""
1009-
sacct_filter = process_sacct_filter(args, sacct_filter)
1010-
1011-
LOG.debug(f'sacct args: {sacct_filter}')
10121015
if args.db:
10131016
db = sqlite3.connect(args.db)
10141017
if sacct_filter:
10151018
LOG.warn("Warning: reading from database. Any sacct filters are ignored.")
10161019
else:
1017-
db = sqlite3.connect(':memory:')
1018-
errors = slurm2sql(db, sacct_filter=sacct_filter,
1019-
csv_input=getattr(args, 'csv_input', False) or csv_input)
1020+
# Import fresh
1021+
sacct_filter = process_sacct_filter(args, sacct_filter)
1022+
LOG.debug(f'sacct args: {sacct_filter}')
1023+
db = sqlite3.connect(':memory:')
1024+
errors = slurm2sql(db, sacct_filter=sacct_filter,
1025+
csv_input=getattr(args, 'csv_input', False) or csv_input)
10201026
return db
10211027

10221028

@@ -1093,6 +1099,7 @@ def sacct_cli(argv=sys.argv[1:], csv_input=None):
10931099
help="SQL order by (arbitrary SQL expression using column names). NOT safe from SQL injection.")
10941100
parser.add_argument('--completed', '-c', action='store_true',
10951101
help=f"Select for completed job states ({COMPLETED_STATES}) You need to specify --starttime (-S) at some point in the past, due to how saccont default works (for example '-S now-1week'). This option automatically sets '-E now'")
1102+
parser.add_argument('--user', '-u', help="Limit to this or these users. This specific argument will work with --db, if it's a single user.")
10961103
parser.add_argument('--csv-input',
10971104
help="Don't parse sacct but import this CSV file. It's read with "
10981105
"Python's default csv reader (excel format). Beware badly "
@@ -1113,9 +1120,14 @@ def sacct_cli(argv=sys.argv[1:], csv_input=None):
11131120

11141121
db = import_or_open_db(args, sacct_filter, csv_input=csv_input)
11151122

1116-
from tabulate import tabulate
1123+
# If we run sacct, then args.user is set to None so we don't do double filtering here
1124+
if args.user:
1125+
where_user = "WHERE user=:user"
1126+
else:
1127+
where_user = ''
11171128

1118-
cur = db.execute(f'select {args.output} from slurm')
1129+
from tabulate import tabulate
1130+
cur = db.execute(f'select {args.output} from slurm {where_user}', {'user':args.user})
11191131
headers = [ x[0] for x in cur.description ]
11201132
print(tabulate(cur, headers=headers, tablefmt=args.format))
11211133

@@ -1151,6 +1163,7 @@ def seff_cli(argv=sys.argv[1:], csv_input=None):
11511163
help="SQL order by (arbitrary SQL expression using column names). NOT safe from SQL injection.")
11521164
parser.add_argument('--completed', '-c', action='store_true',
11531165
help=f"Select for completed job states ({COMPLETED_STATES}) You need to specify --starttime (-S) at some point in the past, due to how saccont default works (for example '-S now-1week'). This option automatically sets '-E now'.")
1166+
parser.add_argument('--user', '-u', help="Limit to this or these users. This specific argument will work with --db, if it's a single user.")
11541167
parser.add_argument('--csv-input',
11551168
help="Don't parse sacct but import this CSV file. It's read with "
11561169
"Python's default csv reader (excel format). Beware badly "
@@ -1172,6 +1185,13 @@ def seff_cli(argv=sys.argv[1:], csv_input=None):
11721185
else:
11731186
order_by = ''
11741187

1188+
# If we run sacct, then args.user is set to None so we don't do double filtering here
1189+
if args.user:
1190+
where_user = "and user=:user"
1191+
else:
1192+
where_user = ''
1193+
1194+
11751195
db = import_or_open_db(args, sacct_filter, csv_input=csv_input)
11761196

11771197
from tabulate import tabulate
@@ -1194,9 +1214,9 @@ def seff_cli(argv=sys.argv[1:], csv_input=None):
11941214
round(sum(TotDiskWrite/1048576)/sum(Elapsed),2) AS write_MiBps
11951215
11961216
FROM eff
1197-
WHERE End IS NOT NULL
1217+
WHERE End IS NOT NULL {where_user}
11981218
GROUP BY user ) {order_by}
1199-
""")
1219+
""", {'user': args.user})
12001220
headers = [ x[0] for x in cur.description ]
12011221
data = cur.fetchall()
12021222
if len(data) == 0:
@@ -1225,7 +1245,7 @@ def seff_cli(argv=sys.argv[1:], csv_input=None):
12251245
round(TotDiskWrite/Elapsed/1048576,2) AS write_MiBps
12261246
12271247
FROM eff
1228-
WHERE End IS NOT NULL ) {order_by}""")
1248+
WHERE End IS NOT NULL {where_user} ) {order_by}""", {'user': args.user})
12291249
headers = [ x[0] for x in cur.description ]
12301250
data = cur.fetchall()
12311251
if len(data) == 0:

0 commit comments

Comments
 (0)