@@ -703,7 +703,7 @@ def main(argv=sys.argv[1:], db=None, raw_sacct=None, csv_input=None):
703
703
logging .lastResort .setLevel (logging .WARN )
704
704
LOG .debug (args )
705
705
706
- sacct_filter = process_sacct_filter (args , sacct_filter )
706
+ sacct_filter = args_to_sacct_filter (args , sacct_filter )
707
707
708
708
# db is only given as an argument in tests (normally)
709
709
if db is None :
@@ -982,7 +982,7 @@ def rows():
982
982
return errors [0 ]
983
983
984
984
985
- def process_sacct_filter (args , sacct_filter ):
985
+ def args_to_sacct_filter (args , sacct_filter ):
986
986
"""Generate sacct filter args in a standard way
987
987
988
988
For example adding a --completed argument that translates into
@@ -1005,6 +1005,13 @@ def process_sacct_filter(args, sacct_filter):
1005
1005
args .running_at_time = None
1006
1006
return sacct_filter
1007
1007
1008
+ def args_to_sql_where (args ):
1009
+ where = [ ]
1010
+ if getattr (args , 'user' , None ):
1011
+ where .append ('and user=:user' )
1012
+ return ' ' .join (where )
1013
+
1014
+
1008
1015
def import_or_open_db (args , sacct_filter , csv_input = None ):
1009
1016
"""Helper function to either open a DB or generate a new in-mem one from sacct
1010
1017
@@ -1021,7 +1028,7 @@ def import_or_open_db(args, sacct_filter, csv_input=None):
1021
1028
LOG .warn ("Warning: reading from database. Any sacct filters are ignored." )
1022
1029
else :
1023
1030
# Import fresh
1024
- sacct_filter = process_sacct_filter (args , sacct_filter )
1031
+ sacct_filter = args_to_sacct_filter (args , sacct_filter )
1025
1032
LOG .debug (f'sacct args: { sacct_filter } ' )
1026
1033
db = sqlite3 .connect (':memory:' )
1027
1034
errors = slurm2sql (db , sacct_filter = sacct_filter ,
@@ -1125,13 +1132,10 @@ def sacct_cli(argv=sys.argv[1:], csv_input=None):
1125
1132
db = import_or_open_db (args , sacct_filter , csv_input = csv_input )
1126
1133
1127
1134
# If we run sacct, then args.user is set to None so we don't do double filtering here
1128
- if args .user :
1129
- where_user = "WHERE user=:user"
1130
- else :
1131
- where_user = ''
1135
+ where = args_to_sql_where (args )
1132
1136
1133
1137
from tabulate import tabulate
1134
- cur = db .execute (f'select { args .output } from slurm { where_user } ' , {'user' :args .user })
1138
+ cur = db .execute (f'select { args .output } from slurm WHERE true { where } ' , {'user' :args .user })
1135
1139
headers = [ x [0 ] for x in cur .description ]
1136
1140
print (tabulate (cur , headers = headers , tablefmt = args .format ))
1137
1141
@@ -1189,15 +1193,11 @@ def seff_cli(argv=sys.argv[1:], csv_input=None):
1189
1193
else :
1190
1194
order_by = ''
1191
1195
1192
- # If we run sacct, then args.user is set to None so we don't do double filtering here
1193
- if args .user :
1194
- where_user = "and user=:user"
1195
- else :
1196
- where_user = ''
1197
-
1198
-
1199
1196
db = import_or_open_db (args , sacct_filter , csv_input = csv_input )
1200
1197
1198
+ # If we run sacct, then args.user is set to None so we don't do double filtering here
1199
+ where = args_to_sql_where (args )
1200
+
1201
1201
from tabulate import tabulate
1202
1202
1203
1203
if args .aggregate_user :
@@ -1218,7 +1218,7 @@ def seff_cli(argv=sys.argv[1:], csv_input=None):
1218
1218
round(sum(TotDiskWrite/1048576)/sum(Elapsed),2) AS write_MiBps
1219
1219
1220
1220
FROM eff
1221
- WHERE End IS NOT NULL { where_user }
1221
+ WHERE End IS NOT NULL WHERE true { where }
1222
1222
GROUP BY user ) { order_by }
1223
1223
""" , {'user' : args .user })
1224
1224
headers = [ x [0 ] for x in cur .description ]
@@ -1249,7 +1249,7 @@ def seff_cli(argv=sys.argv[1:], csv_input=None):
1249
1249
round(TotDiskWrite/Elapsed/1048576,2) AS write_MiBps
1250
1250
1251
1251
FROM eff
1252
- WHERE End IS NOT NULL { where_user } ) { order_by } """ , {'user' : args .user })
1252
+ WHERE End IS NOT NULL { where } ) { order_by } """ , {'user' : args .user })
1253
1253
headers = [ x [0 ] for x in cur .description ]
1254
1254
data = cur .fetchall ()
1255
1255
if len (data ) == 0 :
0 commit comments