Mentions légales du service

Skip to content
Snippets Groups Projects
Commit dc561353 authored by Simon Delamare's avatar Simon Delamare
Browse files

db: Move job and time related processing in api.get_metrics function

parent 7da9179f
No related branches found
No related tags found
No related merge requests found
...@@ -4,6 +4,7 @@ import requests ...@@ -4,6 +4,7 @@ import requests
import pytest import pytest
import time import time
import json import json
import datetime
def metrics(params): def metrics(params):
...@@ -22,13 +23,36 @@ def metrics_available(params={}): ...@@ -22,13 +23,36 @@ def metrics_available(params={}):
return r.json() return r.json()
def test_request(): def test_metrics():
assert ( assert len(metrics({"devices": "local"})) > 0
len(metrics({"devices": "device-1", "start_time": "2020-12-31T22:59:59"})) > 0
def test_metrics_start():
r = metrics({"devices": "device-1", "start_time": "2020-12-31T23:00:00"})
assert len(r) > 0
assert all("2020-12-31T23:0" in m["timestamp"] for m in r)
assert all("device-1" == m["device_id"] for m in r)
def test_metrics_end():
r = metrics({"devices": "device-1", "end_time": "2020-12-31T23:19:59"})
assert len(r) > 0
assert all("2020-12-31T23:1" in m["timestamp"] for m in r)
def test_metrics_startend():
r = metrics(
{
"devices": "device-1",
"start_time": "2020-12-31T23:00:00",
"end_time": "2020-12-31T23:00:59",
}
) )
assert len(r) > 0
assert all("2020-12-31T23:00" in m["timestamp"] for m in r)
def test_devices(): def test_metrics_devices():
assert 2 * len( assert 2 * len(
metrics({"devices": "device-1", "start_time": "2020-12-31T22:59:59"}) metrics({"devices": "device-1", "start_time": "2020-12-31T22:59:59"})
) == pytest.approx( ) == pytest.approx(
...@@ -40,29 +64,7 @@ def test_devices(): ...@@ -40,29 +64,7 @@ def test_devices():
) )
def test_starttime(): def test_metrics_metric():
assert (
metrics({"devices": "device-1", "start_time": "2020-12-31T22:59:59.999"})[0][
"timestamp"
]
== "2020-12-31T23:00:00+00:00"
)
def test_endtime():
assert (
metrics(
{
"devices": "device-1",
"start_time": "2020-12-31T22:59:59",
"end_time": "2020-12-31T23:30:00.0001",
}
)[-1]["timestamp"]
== "2020-12-31T23:30:00+00:00"
)
def test_metric():
assert all( assert all(
r["metric_id"] == "metric-1" r["metric_id"] == "metric-1"
for r in metrics( for r in metrics(
...@@ -75,29 +77,40 @@ def test_metric(): ...@@ -75,29 +77,40 @@ def test_metric():
) )
def test_job(): def test_metrics_job():
assert all( r = metrics({"job_id": 1})
r["device_id"] in ("device-1", "device-2") for r in metrics({"job_id": 1}) assert all(m["device_id"] in ("device-1", "device-2") for m in r)
) assert r[0]["timestamp"] == "2020-12-31T22:00:00+00:00"
assert metrics({"job_id": 1})[0]["timestamp"] == "2020-12-31T22:00:05+00:00" assert r[-1]["timestamp"] == "2020-12-31T23:00:00+00:00"
assert metrics({"job_id": 1})[-1]["timestamp"] == "2020-12-31T22:59:55+00:00"
def test_job_devices(): def test_metrics_job_devices():
assert all( assert all(
r["device_id"] == "device-1" r["device_id"] == "device-1"
for r in metrics({"job_id": 1, "devices": "device-1"}) for r in metrics({"job_id": 1, "devices": "device-1"})
) )
def test_job_starttime(): def test_job_metric():
assert all(
r["metric_id"] == "metric-1"
for r in metrics(
{
"job_id": 1,
"metrics": "metric-1",
}
)
)
def test_metrics_job_start():
assert ( assert (
metrics({"job_id": 1, "start_time": "2020-12-31T22:29:59.999"})[0]["timestamp"] metrics({"job_id": 1, "start_time": "2020-12-31T22:29:59.999"})[0]["timestamp"]
== "2020-12-31T22:30:00+00:00" == "2020-12-31T22:30:00+00:00"
) )
def test_job_endtime(): def test_metrics_job_end():
assert ( assert (
metrics( metrics(
{ {
...@@ -120,31 +133,11 @@ def test_job_endtime(): ...@@ -120,31 +133,11 @@ def test_job_endtime():
) )
def test_job_notfinished(): def test_metrics_job_notfinished():
assert ( r = metrics({"job_id": 3})
metrics({"job_id": 2, "devices": "device-5",})[ assert any(str(datetime.date.today()) in m["timestamp"] for m in r)
-1 r = metrics({"job_id": 3, "end_time": "2020-12-31T23:59:59"})
]["timestamp"] assert not any(str(datetime.date.today()) in m["timestamp"] for m in r)
== "2021-01-01T00:00:00+00:00"
)
assert (
metrics({"job_id": 2, "end_time": "2020-12-31T22:40:00.0001",})[
-1
]["timestamp"]
== "2020-12-31T22:40:00+00:00"
)
def test_job_metric():
assert all(
r["metric_id"] == "metric-1"
for r in metrics(
{
"job_id": 1,
"metrics": "metric-1",
}
)
)
def test_available_metrics(): def test_available_metrics():
......
...@@ -326,40 +326,47 @@ def time_to_sql(time): ...@@ -326,40 +326,47 @@ def time_to_sql(time):
except ValueError: except ValueError:
return "'%s'::timestamp with time zone" % time return "'%s'::timestamp with time zone" % time
global start_time, end_time
_devices = ",".join([arg for arg in [nodes,devices] if arg]) _devices = ",".join([arg for arg in [nodes,devices] if arg])
if not job_id and not _devices: if not job_id and not _devices:
plpy.error("Missing 'devices', 'nodes' or 'job_id' argument") plpy.error("Missing 'devices', 'nodes' or 'job_id' argument")
if job_id:
nodetime = plpy.execute("SELECT MIN(start_time) AS start_time, COALESCE(MAX(stop_time), NOW()) AS stop_time, STRING_AGG(node, ',') AS nodes FROM nodetime_by_job WHERE job_id = %s GROUP BY job_id" % job_id)[0]
if not start_time:
start_time = nodetime["start_time"]
# TODO Fix for running job
if not end_time:
end_time = nodetime["stop_time"]
if not _devices:
_devices = nodetime["nodes"]
suffix = "" suffix = ""
if summary: if summary:
suffix = "_summary" suffix = "_summary"
cond = [] cond = []
if job_id: req = "SELECT * FROM metrics_by_device%s WHERE " % suffix
req = "SELECT timestamp, device_id, metric_id, value, labels FROM metrics_by_job%s WHERE " % suffix
cond.append("job_id = %d" % job_id)
else:
req = "SELECT * FROM metrics_by_device%s WHERE " % suffix
if _devices: if _devices:
_devices = [device.split('.')[0] for device in _devices.split(",")] _devices = [device.split('.')[0] for device in _devices.split(",")]
cond.append("device_id in ('%s')" % "','".join(_devices)) cond.append("device_id in ('%s')" % "','".join(_devices))
end_time_sql = time_to_sql(end_time) if start_time and end_time:
if job_id and not end_time: start_time_sql = time_to_sql(start_time)
pass end_time_sql = time_to_sql(end_time)
else: elif start_time and not end_time:
cond.append("timestamp <= %s" % end_time_sql) start_time_sql = time_to_sql(start_time)
end_time_sql = time_to_sql(plpy.execute("SELECT %s + INTERVAL '5min' AS t" % start_time_sql)[0]["t"])
if job_id and not start_time: elif not start_time and end_time:
pass end_time_sql = time_to_sql(end_time)
start_time_sql = time_to_sql(plpy.execute("SELECT %s - INTERVAL '5min' AS t" % end_time_sql)[0]["t"])
else: else:
if start_time: end_time_sql = time_to_sql(plpy.execute("SELECT NOW() AS t")[0]["t"])
start_time_sql = time_to_sql(start_time) start_time_sql = time_to_sql(plpy.execute("SELECT %s - INTERVAL '5min' AS t" % end_time_sql)[0]["t"])
else: cond.append("timestamp >= %s AND timestamp <= %s" % (start_time_sql, end_time_sql))
start_time_sql = end_time_sql + " - interval '5 min'"
cond.append("timestamp >= %s" % start_time_sql)
if metrics: if metrics:
metrics_id = metrics.split(",") metrics_id = metrics.split(",")
...@@ -406,7 +413,7 @@ BEGIN ...@@ -406,7 +413,7 @@ BEGIN
IF params->>'job_id' IS NOT NULL THEN IF params->>'job_id' IS NOT NULL THEN
SELECT nullif(substring(params->>'job_id' FROM '^\d+$'), '')::int INTO from_job_id; SELECT nullif(substring(params->>'job_id' FROM '^\d+$'), '')::int INTO from_job_id;
IF from_job_id IS NOT NULL THEN IF from_job_id IS NOT NULL THEN
SELECT MAX(stop_time) FROM nodetime_by_job WHERE job_id = from_job_id INTO from_job_id_end; SELECT COALESCE(MAX(stop_time), NOW()) FROM nodetime_by_job WHERE job_id = from_job_id INTO from_job_id_end;
SELECT ARRAY(SELECT DISTINCT node FROM nodetime_by_job WHERE nodetime_by_job.job_id = from_job_id) INTO from_devices; SELECT ARRAY(SELECT DISTINCT node FROM nodetime_by_job WHERE nodetime_by_job.job_id = from_job_id) INTO from_devices;
RETURN QUERY SELECT * FROM api.available_metrics(at => LEAST(from_job_id_end, at), params => jsonb_build_object('devices', array_to_json(from_devices))); RETURN QUERY SELECT * FROM api.available_metrics(at => LEAST(from_job_id_end, at), params => jsonb_build_object('devices', array_to_json(from_devices)));
RETURN; RETURN;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment