@@ -452,6 +452,38 @@ def aggregate_usage(self, start_time, end_time):
452452 job_entry ['core_hours' ] += cpus * dur_hours
453453 return agg , totals
454454
455+ def fetch_all_accounts (self ):
456+ """Return a sorted list of all accounts in the cluster."""
457+ self .connect ()
458+ accounts = set ()
459+ with self ._conn .cursor () as cur :
460+ try :
461+ cur .execute ("SHOW TABLES LIKE 'acct_table'" )
462+ if cur .fetchone ():
463+ cur .execute ("SELECT name FROM acct_table WHERE deleted = 0" )
464+ for row in cur .fetchall ():
465+ name = row .get ('name' )
466+ if name :
467+ accounts .add (name )
468+ return sorted (accounts )
469+ except pymysql .err .ProgrammingError :
470+ pass
471+
472+ assoc_table = (
473+ f"{ self .cluster } _assoc_table" if self .cluster else "assoc_table"
474+ )
475+ try :
476+ cur .execute (
477+ f"SELECT DISTINCT acct FROM { assoc_table } WHERE deleted = 0"
478+ )
479+ for row in cur .fetchall ():
480+ acct = row .get ('acct' )
481+ if acct :
482+ accounts .add (acct )
483+ except pymysql .err .ProgrammingError :
484+ pass
485+ return sorted (accounts )
486+
455487 def fetch_invoices (self , start_date = None , end_date = None ):
456488 """Fetch invoice metadata from the database if present."""
457489 if start_date :
@@ -651,6 +683,7 @@ def export_summary(self, start_time, end_time):
651683 dest = "slurm_conf" ,
652684 help = "path to slurm.conf for auto cluster detection" ,
653685 )
686+ parser .add_argument ("--accounts" , action = "store_true" , help = "list all accounts and exit" )
654687
655688 args = parser .parse_args ()
656689
@@ -660,6 +693,17 @@ def export_summary(self, start_time, end_time):
660693 slurm_conf = args .slurm_conf ,
661694 )
662695
696+ if args .accounts :
697+ data = {"accounts" : db .fetch_all_accounts ()}
698+ if args .output in ("-" , "/dev/stdout" ):
699+ json .dump (data , sys .stdout , indent = 2 , default = str )
700+ sys .stdout .write ("\n " )
701+ else :
702+ with open (args .output , "w" ) as fh :
703+ json .dump (data , fh , indent = 2 , default = str )
704+ print (f"Wrote { args .output } " )
705+ sys .exit (0 )
706+
663707 def _export_day (day ):
664708 day_str = day .isoformat ()
665709 data = db .export_summary (day_str , day_str )
0 commit comments