From f9ebd686549001fecbecd4af089137726eb66c98 Mon Sep 17 00:00:00 2001 From: Jose Gracia Date: Mon, 20 Nov 2023 16:50:22 +0100 Subject: [PATCH] Refactor database class for multiple querries --- monitoring/db/scripts/get_detailed_power.py | 28 +++++++++++++-------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/monitoring/db/scripts/get_detailed_power.py b/monitoring/db/scripts/get_detailed_power.py index d235492..5128539 100644 --- a/monitoring/db/scripts/get_detailed_power.py +++ b/monitoring/db/scripts/get_detailed_power.py @@ -54,6 +54,10 @@ class Power: return cls +# @classmethod +# def from_db(cls, db): + + def to_file(self, jobid): """Dumps power data to file. Returns filename is succesfull and None if unsucessfull.""" fname = self.filename(jobid) @@ -122,9 +126,8 @@ class Power: return fname class MonitoringDB: - def __init__(self, config): - self.connection = self.init_db(config.verbose) - self.query = self.init_query(config.jobid, config.interval) + def __init__(self, verbose): + self.connection = self.init_db(verbose) @staticmethod def init_db(verbose): @@ -134,8 +137,11 @@ class MonitoringDB: connection = engine.connect() return connection + def close_db(self): + return self.connection.close() + @staticmethod - def init_query(jobid, interval): + def build_query(jobid, interval): import sqlalchemy as db query_string = """with job as ( select job_id, starttime, endtime, nodes from jobs where job_id='{jobid}.hawk-pbs5' @@ -166,19 +172,21 @@ class MonitoringDB: """ return db.text(query_string.format(jobid=jobid, interval=interval)) - def db_to_list(self): - return self.connection.execute(self.query).fetchall() + def db_to_list(self, jobid, interval): + query = self.build_query(jobid, interval) + return self.connection.execute(query).fetchall() - def db_to_pf(self): - return pd.read_sql(self.query, con=self.connection) + def db_to_pf(self, jobid, inerval): + query = self.build_query(jobid, interval) + return pd.read_sql(query, con=self.connection) if __name__ == "__main__": config = parse_arguments() config.interval = 5 - DB = MonitoringDB(config) - all_list = DB.db_to_list() + DB = MonitoringDB(config.verbose) + all_list = DB.db_to_list(config.jobid, config.interval) power = Power.from_list(all_list) #print("#epochs", len(power.epochs))