Skip to content

Commit 4ddebc7

Browse files
committed
slurm2sql: sacct2, seff2: Generalize --user and --partition options
- Make them work for either --db or new data - Generalize their whole framework
1 parent e5ee302 commit 4ddebc7

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

slurm2sql.py

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,7 @@ def infer_type(cd):
900900
db.execute('CREATE VIEW IF NOT EXISTS eff AS select '
901901
'JobIDnostep AS JobID, '
902902
'max(User) AS User, '
903-
'max(Partition), '
903+
'max(Partition) AS Partition, '
904904
'Account, '
905905
'State, '
906906
'Time, '
@@ -1000,6 +1000,9 @@ def args_to_sacct_filter(args, sacct_filter):
10001000
# it shouldn't be re-handled in the future SQL code (future
10011001
# SQL woludn't handle multiple users, for example).
10021002
args.user = None
1003+
if getattr(args, 'partition', None):
1004+
sacct_filter[:0] = [f'--partition={args.partition}']
1005+
args.partition = None
10031006
if getattr(args, 'running_at_time', None):
10041007
sacct_filter[:0] = [f'--start={args.running_at_time}', f'--end={args.running_at_time}', '--state=RUNNING' ]
10051008
args.running_at_time = None
@@ -1009,6 +1012,8 @@ def args_to_sql_where(args):
10091012
where = [ ]
10101013
if getattr(args, 'user', None):
10111014
where.append('and user=:user')
1015+
if getattr(args, 'partition', None):
1016+
where.append("and Partition like '%'||:partition||'%'")
10121017
return ' '.join(where)
10131018

10141019

@@ -1107,10 +1112,6 @@ def sacct_cli(argv=sys.argv[1:], csv_input=None):
11071112
help="Output format (see tabulate formats: https://pypi.org/project/tabulate/ (default simple)")
11081113
parser.add_argument('--order',
11091114
help="SQL order by (arbitrary SQL expression using column names). NOT safe from SQL injection.")
1110-
parser.add_argument('--completed', '-c', action='store_true',
1111-
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'. Not compatible with --db.")
1112-
parser.add_argument('--user', '-u', help="Limit to this or these users. Compatible with --db.")
1113-
parser.add_argument('--running-at-time', metavar='TIME', help="Only jobs running at this time. Expanded to --start=TIME --end=TIME --state=R. Not compatible with --db.")
11141115
parser.add_argument('--csv-input',
11151116
help="Don't parse sacct but import this CSV file. It's read with "
11161117
"Python's default csv reader (excel format). Beware badly "
@@ -1119,6 +1120,16 @@ def sacct_cli(argv=sys.argv[1:], csv_input=None):
11191120
help="Don't output anything unless errors")
11201121
parser.add_argument('--verbose', '-v', action='store_true',
11211122
help="Output more logging info")
1123+
# No --db compatibility
1124+
group = parser.add_argument_group(description="Selectors that only works when getting new data (not with --db):")
1125+
group.add_argument('--completed', '-c', action='store_true',
1126+
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'. Not compatible with --db.")
1127+
group.add_argument('--running-at-time', metavar='TIME', help="Only jobs running at this time. Not compatible with --db. Expanded to --start=TIME --end=TIME --state=R.")
1128+
# --db compatibility
1129+
group = parser.add_argument_group(description="Selectors that also work with --db:")
1130+
group.add_argument('--user', '-u', help="Limit to this or these users. Compatible with --db.")
1131+
group.add_argument('--partition', '-r', help="Jobs in this partition. Works with --db. Getting fresh data, an exact match and can be a comma separated list. With --db, a raw glob match.")
1132+
11221133
args, sacct_filter = parser.parse_known_args(argv)
11231134

11241135
if args.verbose:
@@ -1135,7 +1146,8 @@ def sacct_cli(argv=sys.argv[1:], csv_input=None):
11351146
where = args_to_sql_where(args)
11361147

11371148
from tabulate import tabulate
1138-
cur = db.execute(f'select {args.output} from slurm WHERE true {where}', {'user':args.user})
1149+
cur = db.execute(f'select {args.output} from slurm WHERE true {where}',
1150+
{'user':args.user, 'partition': args.partition})
11391151
headers = [ x[0] for x in cur.description ]
11401152
print(tabulate(cur, headers=headers, tablefmt=args.format))
11411153

@@ -1169,9 +1181,6 @@ def seff_cli(argv=sys.argv[1:], csv_input=None):
11691181
help="Aggregate data by user.")
11701182
parser.add_argument('--order',
11711183
help="SQL order by (arbitrary SQL expression using column names). NOT safe from SQL injection.")
1172-
parser.add_argument('--completed', '-c', action='store_true',
1173-
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'.")
1174-
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.")
11751184
parser.add_argument('--csv-input',
11761185
help="Don't parse sacct but import this CSV file. It's read with "
11771186
"Python's default csv reader (excel format). Beware badly "
@@ -1180,6 +1189,16 @@ def seff_cli(argv=sys.argv[1:], csv_input=None):
11801189
help="Don't output anything unless errors")
11811190
parser.add_argument('--verbose', '-v', action='store_true',
11821191
help="Output more logging info")
1192+
# No --db compatibility
1193+
group = parser.add_argument_group(description="Selectors that only works when getting new data (not with --db):")
1194+
group.add_argument('--completed', '-c', action='store_true',
1195+
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'. Not compatible with --db.")
1196+
group.add_argument('--running-at-time', metavar='TIME', help="Only jobs running at this time. Not compatible with --db. Expanded to --start=TIME --end=TIME --state=R.")
1197+
# --db compatibility
1198+
group = parser.add_argument_group(description="Selectors that also work with --db:")
1199+
group.add_argument('--user', '-u', help="Limit to this or these users. Compatible with --db.")
1200+
group.add_argument('--partition', '-r', help="Jobs in this partition. Works with --db. Getting fresh data, an exact match and can be a comma separated list. With --db, a raw glob match.")
1201+
11831202
args, sacct_filter = parser.parse_known_args(argv)
11841203

11851204
if args.verbose:
@@ -1218,9 +1237,9 @@ def seff_cli(argv=sys.argv[1:], csv_input=None):
12181237
round(sum(TotDiskWrite/1048576)/sum(Elapsed),2) AS write_MiBps
12191238
12201239
FROM eff
1221-
WHERE End IS NOT NULL WHERE true {where}
1240+
WHERE End IS NOT NULL {where}
12221241
GROUP BY user ) {order_by}
1223-
""", {'user': args.user})
1242+
""", {'user': args.user, 'partition': args.partition})
12241243
headers = [ x[0] for x in cur.description ]
12251244
data = cur.fetchall()
12261245
if len(data) == 0:
@@ -1249,7 +1268,7 @@ def seff_cli(argv=sys.argv[1:], csv_input=None):
12491268
round(TotDiskWrite/Elapsed/1048576,2) AS write_MiBps
12501269
12511270
FROM eff
1252-
WHERE End IS NOT NULL {where} ) {order_by}""", {'user': args.user})
1271+
WHERE End IS NOT NULL {where} ) {order_by}""", {'user': args.user, 'partition': args.partition})
12531272
headers = [ x[0] for x in cur.description ]
12541273
data = cur.fetchall()
12551274
if len(data) == 0:

0 commit comments

Comments
 (0)