Outline query string as class constant

This commit is contained in:
Jose Gracia 2024-02-06 09:04:52 +01:00
parent 41dab7a647
commit ae00b04bbe

View file

@ -139,25 +139,8 @@ class Power:
class MonitoringDB:
def __init__(self, verbose):
self.connection = self.init_db(verbose)
@staticmethod
def init_db(verbose):
import sqlalchemy as db
_verbose = False #True
engine = db.create_engine('postgresql://hpc@hawk-monitor4:5432/coe_mon', echo=verbose)
connection = engine.connect()
return connection
def close_db(self):
return self.connection.close()
@staticmethod
def build_query(jobid, interval):
"""Query taken from https://kb.hlrs.de/monitoring/index.php/TimescaleDB_-_Query_Guidelines#Power_by_Job_Query"""
import sqlalchemy as db
query_string = """with _job as (
QUERY_STRING_HAWK = """
with _job as (
select job_id, starttime, endtime, nodes from job where job_id='{jobid}.hawk-pbs5'
),
node_series as(
@ -184,6 +167,26 @@ select a.time, a.value, ns.name from (
group by time, cmcavg.series_id order by time desc) a
inner join node_series ns on a.series_id::int = ns.series_id;
"""
def __init__(self, verbose):
self.connection = self.init_db(verbose)
@staticmethod
def init_db(verbose):
import sqlalchemy as db
_verbose = False #True
engine = db.create_engine('postgresql://hpc@hawk-monitor4:5432/coe_mon', echo=verbose)
connection = engine.connect()
return connection
def close_db(self):
return self.connection.close()
@classmethod
def build_query(cls, jobid, interval):
"""Query taken from https://kb.hlrs.de/monitoring/index.php/TimescaleDB_-_Query_Guidelines#Power_by_Job_Query"""
import sqlalchemy as db
query_string = cls.QUERY_STRING_HAWK
return db.text(query_string.format(jobid=jobid, interval=interval))
def db_to_list(self, jobid, interval):