Replace explicit query string with function in TimescaleDB

This commit is contained in:
Jose Gracia 2024-02-07 14:28:57 +01:00
parent 8543446b91
commit a484711971

View file

@ -141,34 +141,17 @@ class Power:
class MonitoringDB: class MonitoringDB:
QUERY_STRING_HAWK = """ QUERY_STRING_HAWK = """
with _job as ( -- For description of get_job_data(), see https://kb.hlrs.de/monitoring/index.php/TimescaleDB_-_Query_Guidelines#Function:_get_job_data_and_get_ai_job_data
select job_id, starttime, endtime, nodes from job where job_id='{jobid}.hawk-pbs5' select * from get_job_data(
), '{jobid}.hawk-pbs5',
node_series as( 'cmc_power_racktraynodepoweravg', -- power data source
select n.name, scmcavg.id as series_id from node n '{interval} seconds',
inner join (select * from label where key='node') l on n.id = l.value::int array['avg'], -- aggregation: average across samples in bucket
inner join series_cmc_power_racktraynodepoweravg scmcavg on l.id = scmcavg.labels[( array['time','node'] -- sort by time first than node (ascending)
select pos from label_key_position
where metric_category= 'cmc_power'
and metric_name = 'RackTrayNodePowerAvg'
and key = 'node'
)]
where n.id = any((select nodes from _job)::int[])
) )
select a.time, ns.name, a.value from ( as t(time bigint, name varchar, avg double precision);
select """
time_bucket(extract ('epoch' from '{interval} seconds'::interval)::int*1000, cmcavg.ts) as time,
cmcavg.series_id::varchar,
avg(cmcavg.val) AS value
from cmc_power_racktraynodepoweravg cmcavg
where
ts <= (select endtime from _job)
and ts >= (select starttime from _job)
and series_id = Any(select series_id from node_series)
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): def __init__(self, verbose):
self.connection = self.init_db(verbose) self.connection = self.init_db(verbose)
@ -185,7 +168,6 @@ inner join node_series ns on a.series_id::int = ns.series_id;
@classmethod @classmethod
def build_query(cls, jobid, interval): 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 import sqlalchemy as db
query_string = cls.QUERY_STRING_HAWK query_string = cls.QUERY_STRING_HAWK
return db.text(query_string.format(jobid=jobid, interval=interval)) return db.text(query_string.format(jobid=jobid, interval=interval))