Refactor database class for multiple querries

This commit is contained in:
Jose Gracia 2023-11-20 16:50:22 +01:00
parent 9294b04291
commit f9ebd68654

View file

@ -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))