v1.0.92: Steps 5-9 — dynamic services, PostgreSQL history, /api/history

This commit is contained in:
2026-07-27 22:59:52 +04:00
parent 4952f8b096
commit d95afc60e3
7 changed files with 219 additions and 9 deletions
+43
View File
@@ -341,6 +341,18 @@ def _finish_op(client, op_uid, instance_uid, svc_id, display_name, op_name, svc_
"duration": round(time.time() - t0, 1),
"_ts": time.time(),
}
# Сохранить в БД историю
try:
from db.save_run import save_run
save_run(client_id, stand, svc_id, op.get("svc", ""), op_name, svc_op_id,
instance_uid, display_name,
"OK" if is_ok else "FAIL",
round(time.time() - t0, 1),
str(err) if err else "",
{}, # params недоступны в _finish_op — будут при следующем улучшении
op.get("stages", []))
except Exception:
pass # не ронять фоновый поток из-за БД
return
time.sleep(5)
except Exception:
@@ -391,6 +403,37 @@ def api_log():
return jsonify([])
@bp.route("/api/history")
def api_history():
"""Последние 50 записей истории тестов (фильтр по client_id + stand)."""
try:
from db.pool import get_conn, put_conn
conn = get_conn()
if not conn:
return jsonify([])
cur = conn.cursor()
cur.execute("""
SELECT id, created_at, client_id, stand, svc_id, svc_name,
op_name, instance_uid, display_name, status, duration_sec, error_log
FROM runs
WHERE client_id = %s AND stand = %s
ORDER BY created_at DESC
LIMIT 50
""", (get_client_id(), get_stand()))
rows = cur.fetchall()
cols = [d[0] for d in cur.description]
result = [dict(zip(cols, r)) for r in rows]
# Конвертировать datetime в строку
for r in result:
if r["created_at"]:
r["created_at"] = r["created_at"].isoformat()
cur.close()
put_conn(conn)
return jsonify(result)
except Exception as e:
return jsonify({"error": str(e)}), 500
def _find_uid(resp):
"""Извлечь instanceUid или instanceOperationUid из ответа API."""
if isinstance(resp, dict):