feat: POST /api/v1/test/next + авто-подбор

This commit is contained in:
Repinoid
2026-06-14 14:05:00 +04:00
parent f9c2152c9d
commit de54c1ff40
+36
View File
@@ -335,6 +335,42 @@ def register(app):
return jsonify({"ok": True})
@app.route("/api/v1/test/next", methods=["POST"])
def test_next():
"""Авто-подбор параметров теста. Принимает результаты, возвращает следующий скрипт или done."""
data = request.get_json(silent=True) or {}
results = data.get("results", [])
run = data.get("run", 0)
# Анализ результатов
total = len(results)
if total == 0:
return jsonify({"done": True, "message": "Нет данных", "script": None})
ok_count = sum(1 for r in results if r.get("raw", "").startswith("41"))
err_pct = (total - ok_count) * 100 // total if total > 0 else 100
wait_ms = int(request.args.get("wait", data.get("wait_ms", 1500)))
max_runs = 5
if err_pct < 20 or run >= max_runs:
return jsonify({
"done": True,
"message": f"✅ Стабильно: {ok_count}/{total} ({err_pct}% ошибок) на wait={wait_ms}ms",
"script": None,
"final_wait_ms": wait_ms,
})
# Увеличиваем паузу
new_wait = wait_ms + 300
return jsonify({
"done": False,
"message": f"⚠️ {err_pct}% ошибок — увеличиваю паузу до {new_wait}ms",
"run": run + 1,
"wait_ms": new_wait,
"script": build_test_script(wait_ms=new_wait, pids=["010C", "0106"], repeat=6),
})
def _save_profile(mac: str, profile: dict):
"""Сохраняет профиль в БД (best-effort)."""
try: