TBC/backend/app.py

857 lines
33 KiB
Python

"""
AutoFlow Platform Backend - Flask Application
Unified automation testing platform combining:
- D-Cloud E2E testing (data-loop-automation)
- Robogo full-chain inspection (test project)
Start: python -m backend.app
Access: http://127.0.0.1:5001
"""
import os
import sys
import json
import uuid
import time
import hashlib
import queue
import threading
import subprocess
import signal
from datetime import datetime, timedelta
from pathlib import Path
from flask import Flask, request, jsonify, Response, send_from_directory
from flask_cors import CORS
# Ensure project root is on path
PROJECT_ROOT = Path(__file__).parent.parent
sys.path.insert(0, str(PROJECT_ROOT))
app = Flask(
__name__,
static_folder=str(PROJECT_ROOT / "frontend" / "dist"),
static_url_path="/static"
)
CORS(app)
# ── Constants ──────────────────────────────────────────────────────────────────
DB_FILE = str(PROJECT_ROOT / "platform_db.json")
REPORTS_DIR = str(PROJECT_ROOT / "platform_reports")
SCREENSHOTS_DIR = str(PROJECT_ROOT / "platform_artifacts" / "screenshots")
PYTHON_BIN = sys.executable
os.makedirs(REPORTS_DIR, exist_ok=True)
os.makedirs(SCREENSHOTS_DIR, exist_ok=True)
# ── Products & Test Modules Config ─────────────────────────────────────────────
PRODUCTS = {
"robogo": {
"name": "Robogo",
"desc": "Robogo 全链路 UI 巡检",
"icon": "🤖",
"entry": str(PROJECT_ROOT / "framework" / "scripts" / "run_all.py"),
"type": "scenario"
},
"data_loop": {
"name": "数据闭环",
"desc": "数据闭环平台端到端业务流水线验证",
"icon": "🔄",
"entry": str(PROJECT_ROOT / "framework" / "scripts" / "run_data_loop.py"),
"type": "scenario"
}
}
DCLOUD_MODULES = {
"e2e": {"name": "E2E 全量", "icon": "Zap", "marker": "e2e"},
"workbench": {"name": "工作台", "icon": "Box", "marker": "workbench"},
"data_space": {"name": "数据空间", "icon": "Database", "marker": "data_space or dataspace"},
"data_set": {"name": "数据集", "icon": "Layers", "marker": "dataset"},
"data_generation": {"name": "数据生成", "icon": "Wand2", "marker": "data_generation"},
"model_workshop": {"name": "模型工作台", "icon": "Cpu", "marker": "model_workshop"},
"model_square": {"name": "模型广场", "icon": "ShoppingBag", "marker": "model_square"},
"monitor": {"name": "监控看板", "icon": "BarChart3", "marker": "monitor"},
"image_manage": {"name": "图像管理", "icon": "Image", "marker": "image_manage"},
"mirror_center": {"name": "镜像中心", "icon": "HardDrive", "marker": "mirror_center"},
"cloud_desktop": {"name": "云端桌面", "icon": "Monitor", "marker": "cloud_desktop"},
"dev_machine": {"name": "开发机", "icon": "Code2", "marker": "dev_machine"},
"device_manage": {"name": "设备管理", "icon": "Smartphone", "marker": "device_manage"},
"material_library": {"name": "素材库", "icon": "Library", "marker": "material_library"},
"prompt_library": {"name": "提示词库", "icon": "MessageSquare", "marker": "prompt_library"},
"lora_finetune": {"name": "LoRA微调", "icon": "GitBranch", "marker": "lora_finetune"},
}
# ── In-memory State ────────────────────────────────────────────────────────────
tasks_db: dict = {}
reports_db: dict = {}
log_queues: dict = {}
process_pids: dict = {}
db_lock = threading.Lock()
# ── Database Persistence ───────────────────────────────────────────────────────
def _load_db():
global tasks_db, reports_db
if os.path.exists(DB_FILE):
try:
with open(DB_FILE, 'r', encoding='utf-8') as f:
data = json.load(f)
tasks_db = data.get("tasks", {})
reports_db = data.get("reports", {})
except Exception as e:
print(f"⚠️ Database load failed: {e}")
def _save_db():
with db_lock:
try:
active_once_tasks = [
t for t in tasks_db.values()
if t.get("schedule_type", "once") == "once" and not t.get("is_deleted")
]
if len(active_once_tasks) > 100:
sorted_tasks = sorted(active_once_tasks, key=lambda t: t.get("created_at", ""))
for t in sorted_tasks[:50]:
tid = t.get("id")
if tid:
_cleanup_assets(tid)
t["is_deleted"] = True
safe_data = json.loads(json.dumps({"tasks": tasks_db, "reports": reports_db}))
for t in safe_data.get("tasks", {}).values():
pw = t.get("password")
if pw and not pw.startswith("sha256:"):
t["password"] = "sha256:" + hashlib.sha256(pw.encode()).hexdigest()
with open(DB_FILE, 'w', encoding='utf-8') as f:
json.dump(safe_data, f, ensure_ascii=False)
except Exception:
pass
def _cleanup_assets(tid):
try:
if os.path.exists(SCREENSHOTS_DIR):
for s in os.listdir(SCREENSHOTS_DIR):
if s.startswith(tid):
try:
os.remove(os.path.join(SCREENSHOTS_DIR, s))
except Exception:
pass
except Exception as e:
print(f"⚠️ Cleanup failed: {e}")
_load_db()
# ── Feishu Alerting ────────────────────────────────────────────────────────────
def send_alerts(report):
task_id = report.get("task_id")
task = tasks_db.get(task_id, {})
channels = task.get("alert_channels", [])
webhook_url = os.getenv("LARK_WEBHOOK_URL", "")
if "lark" in channels and webhook_url:
import requests as req
rule = task.get("alert_rule", "always")
if rule == "only_on_fail" and report["result"] == "PASS":
return
try:
status_color = "green" if report["result"] == "PASS" else "red"
status_text = "成功" if report["result"] == "PASS" else "失败"
card = {
"config": {"wide_screen_mode": True},
"header": {
"title": {"tag": "plain_text", "content": f"🔔 AutoFlow 报告: {report['task_name']}"},
"template": status_color
},
"elements": [
{
"tag": "div",
"fields": [
{"is_short": True, "text": {"tag": "lark_md", "content": f"**状态:** {status_text}"}},
{"is_short": True, "text": {"tag": "lark_md", "content": f"**产品:** {report['product']}"}},
{"is_short": True, "text": {"tag": "lark_md", "content": f"**环境:** {task.get('env', 'PROD')}"}},
{"is_short": True, "text": {"tag": "lark_md", "content": f"**通过/总计:** {report['pass']}/{report['total_runs']}"}}
]
}
]
}
req.post(webhook_url, json={"msg_type": "interactive", "card": card}, timeout=5)
except Exception as e:
print(f"❌ Feishu alert failed: {e}")
# ── Task Execution ─────────────────────────────────────────────────────────────
def run_task_process(task):
task_id = task["id"]
task["status"] = "running"
task["started_at"] = datetime.now().isoformat()
_save_db()
q = log_queues.get(task_id)
logs_all = []
def push(msg, level="INFO"):
entry = {"ts": datetime.now().strftime("%H:%M:%S"), "level": level, "msg": msg}
logs_all.append(entry)
if q:
q.put(json.dumps(entry))
run_limit = int(task.get("run_count", 1))
retry_count = int(task.get("retry_count", 1)) if task.get("retry_on_fail") else 0
retry_delay = int(task.get("retry_delay", 5))
env = os.environ.copy()
env["ROBOGO_SCREENSHOTS_DIR"] = os.path.abspath(SCREENSHOTS_DIR)
env["ROBOGO_REPORTS_DIR"] = os.path.abspath(REPORTS_DIR)
env["ROBOGO_TASK_ID"] = task_id
env["ROBOGO_ENV"] = task.get("env", "PROD")
env["ROBOGO_SCOPE"] = task.get("scope", "all")
env["AUTH_ACCOUNT"] = task.get("account") or ""
env["AUTH_PASSWORD"] = task.get("password") or ""
env["ROBOGO_USER"] = task.get("account") or ""
env["ROBOGO_PWD"] = task.get("password") or ""
env["PYTEST_RUNNING"] = "1"
env["HEADLESS"] = task.get("headless", os.environ.get("HEADLESS", "true"))
task_type = task.get("task_type", "scenario")
dcloud_module = task.get("dcloud_module", "")
total_pass, total_fail = 0, 0
current_run = 0
planned_runs = run_limit
last_run_success = None
is_retry_run = False
push(f"🎬 Task started — env: {env.get('ROBOGO_ENV', 'N/A')} | scope: {env.get('ROBOGO_SCOPE', 'N/A')}", "INFO")
# Build command based on task type
junit_report = str(PROJECT_ROOT / "reports" / f"{task_id}.xml")
if task_type == "dcloud_module" and dcloud_module:
test_path = task.get("test", "")
if dcloud_module == "e2e":
if test_path:
cmd = [PYTHON_BIN, "-m", "pytest", test_path, "-v", "--tb=short", "--color=no", "-s", f"--junitxml={junit_report}"]
push(f"🧪 Running E2E test: {test_path}", "INFO")
else:
cmd = [PYTHON_BIN, "-m", "pytest", "tests/", "-m", "e2e", "-v", "--tb=short", "--color=no", "-s", f"--junitxml={junit_report}"]
push("🧪 Running ALL E2E tests across all modules", "INFO")
elif test_path:
cmd = [PYTHON_BIN, "-m", "pytest", test_path, "-v", "--tb=short", "--color=no", "-s", f"--junitxml={junit_report}"]
push(f"🧪 Running test: {test_path}", "INFO")
else:
cmd = [PYTHON_BIN, "-m", "pytest", f"tests/{dcloud_module}/", "-v", "--tb=short", "--color=no", "-s", f"--junitxml={junit_report}"]
push(f"🧪 Running D-Cloud module tests: {dcloud_module}", "INFO")
elif task_type == "dcloud_e2e":
cmd = [PYTHON_BIN, "-m", "pytest", "tests/", "-m", "e2e", "-v", "--tb=short", "--color=no", "-s", f"--junitxml={junit_report}"]
push("🧪 Running ALL E2E tests across all modules", "INFO")
elif task_type == "dcloud_all":
cmd = [PYTHON_BIN, "-m", "pytest", "tests/", "-v", "--tb=short", "--color=no", "-s", f"--junitxml={junit_report}"]
push("🧪 Running ALL D-Cloud tests", "INFO")
elif task_type == "scenario":
cmd = [PYTHON_BIN, task["entry"]]
push(f"🚀 Running scenario: {task.get('product', 'unknown')}", "INFO")
else:
cmd = [PYTHON_BIN, task["entry"]]
push(f"🚀 Running: {task.get('product', 'unknown')}", "INFO")
while current_run < run_limit:
current_run += 1
push(f"▶️ Run {current_run}/{planned_runs}", "INFO")
try:
proc = subprocess.Popen(
cmd,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
text=True, bufsize=1, env=env,
preexec_fn=os.setsid if os.name != 'nt' else None,
cwd=str(PROJECT_ROOT)
)
process_pids[task_id] = proc.pid
for line in proc.stdout:
push(line.rstrip(), "INFO")
proc.wait()
process_pids.pop(task_id, None)
if proc.returncode == 0:
last_run_success = True
if is_retry_run:
total_fail = max(0, total_fail - 1)
total_pass += 1
push(f"✅ Retry #{current_run} succeeded (overrode previous failure)", "SUCCESS")
else:
total_pass += 1
push(f"✅ Run #{current_run} passed", "SUCCESS")
is_retry_run = False
else:
last_run_success = False
total_fail += 1
push(f"❌ Run #{current_run} failed", "ERROR")
if retry_count > 0:
push(f"🔁 Retrying ({retry_count} left), waiting {retry_delay}s...", "WARN")
time.sleep(retry_delay)
retry_count -= 1
run_limit += 1
is_retry_run = True
else:
is_retry_run = False
except Exception as e:
push(f"💥 System error: {e}", "ERROR")
last_run_success = False
total_fail += 1
is_retry_run = False
finished_at = datetime.now().isoformat()
final_result = "PASS" if last_run_success else "FAIL"
report = {
"task_id": task_id,
"task_name": task["name"],
"product": task.get("product", "dcloud"),
"module": task.get("dcloud_module", "") or task.get("product", ""),
"total_runs": planned_runs,
"pass": total_pass,
"fail": total_fail,
"started_at": task["started_at"],
"finished_at": finished_at,
"result": final_result
}
log_file = os.path.join(REPORTS_DIR, f"{task_id}.json")
try:
with open(log_file, 'w', encoding='utf-8') as f:
json.dump({"logs": logs_all}, f, ensure_ascii=False)
except Exception:
pass
# Generate HTML dashboard report for D-Cloud tasks
if task_type.startswith("dcloud"):
try:
html_report = os.path.join(REPORTS_DIR, f"{task_id}.html")
subprocess.run(
[PYTHON_BIN, str(PROJECT_ROOT / "scripts" / "generate_report.py"),
"--output", html_report, "--no-history"],
capture_output=True, timeout=30, cwd=str(PROJECT_ROOT)
)
report["html_report"] = html_report
except Exception:
pass
reports_db[task_id] = report
task["status"] = "pass" if final_result == "PASS" else "fail"
task["finished_at"] = finished_at
task["report_id"] = task_id
_save_db()
send_alerts(report)
push("__DONE__", "DONE")
# ── API Routes ─────────────────────────────────────────────────────────────────
@app.route("/api/products")
def get_products():
return jsonify(PRODUCTS)
@app.route("/api/dcloud-modules")
def get_dcloud_modules():
return jsonify(DCLOUD_MODULES)
@app.route("/api/dcloud-modules/<module>/tests")
def get_module_tests(module):
if module not in DCLOUD_MODULES:
return jsonify({"error": "Unknown module"}), 404
test_dir = PROJECT_ROOT / "tests" / module / "tests"
if not test_dir.exists():
return jsonify([])
tests = []
for f in sorted(test_dir.glob("test_*.py")):
test_type = ""
if "_e2e_" in f.name:
test_type = "e2e"
elif "_api_boundary" in f.name or "_api_comprehensive" in f.name:
test_type = "api"
elif "_api" in f.name:
test_type = "api"
elif "_ui_exceptions" in f.name:
test_type = "exception"
elif "_ui" in f.name:
test_type = "ui"
elif "_flow" in f.name:
test_type = "flow"
elif "_e2e" in f.name:
test_type = "e2e"
elif "full_pipeline" in f.name:
test_type = "e2e"
# Extract test functions from file
funcs = []
try:
content = f.read_text()
for line in content.split("\n"):
line = line.strip()
if line.startswith("def test_"):
func_name = line.split("(")[0].replace("def ", "")
funcs.append(func_name)
except Exception:
pass
tests.append({
"file": f.name,
"path": f"tests/{module}/tests/{f.name}",
"type": test_type,
"functions": funcs,
"count": len(funcs),
})
return jsonify(tests)
@app.route("/api/tasks", methods=["GET"])
def list_tasks():
product = request.args.get("product", "")
t_list = [t for t in tasks_db.values() if not t.get("is_deleted")]
if product:
t_list = [t for t in t_list if t.get("product") == product]
return jsonify(t_list)
@app.route("/api/tasks", methods=["POST"])
def create_task():
body = request.json
tid = str(uuid.uuid4())[:8]
task_type = body.get("task_type", "scenario")
p_key = body.get("product", "robogo")
dcloud_module = body.get("dcloud_module", "")
if task_type in ("dcloud_module", "dcloud_all"):
entry = ""
display_product = "dcloud"
else:
p = PRODUCTS.get(p_key, {})
entry = p.get("entry", "")
display_product = p_key
task = {
"id": tid,
"name": body.get("name", f"Task_{tid}"),
"product": display_product,
"status": "pending",
"created_at": datetime.now().isoformat(),
"account": body.get("account"),
"password": body.get("password"),
"run_count": int(body.get("run_count", 1)),
"retry_on_fail": body.get("retry_on_fail", False),
"retry_count": int(body.get("retry_count", 1)),
"retry_delay": int(body.get("retry_delay", 5)),
"env": body.get("env", "PROD"),
"scope": body.get("scope", "all"),
"scheduled_at": body.get("scheduled_at"),
"schedule_type": body.get("schedule_type", "once"),
"schedule_window": body.get("schedule_window", "00:00-23:59"),
"alert_channels": body.get("alert_channels", ["lark"]),
"alert_rule": body.get("alert_rule", "always"),
"entry": entry,
"task_type": task_type,
"dcloud_module": dcloud_module,
"test": body.get("test", ""),
"headless": body.get("headless", "true"),
}
if task_type in ("dcloud_module", "dcloud_all"):
task["entry"] = "pytest"
tasks_db[tid] = task
log_queues[tid] = queue.Queue()
_save_db()
if not task["scheduled_at"] and task["schedule_type"] == "once":
threading.Thread(target=run_task_process, args=(task,), daemon=True).start()
return jsonify(task), 201
@app.route("/api/tasks/<tid>", methods=["DELETE"])
def delete_task(tid):
_cleanup_assets(tid)
tasks_db.pop(tid, None)
reports_db.pop(tid, None)
_save_db()
return jsonify({"success": True})
@app.route("/api/tasks/<tid>/stop", methods=["POST"])
def stop_task(tid):
pid = process_pids.get(tid)
task = tasks_db.get(tid)
if task:
if task.get("schedule_type") == "continuous":
task["schedule_type"] = "once"
task["status"] = "fail"
task["finished_at"] = datetime.now().isoformat()
if pid:
try:
os.killpg(os.getpgid(pid), signal.SIGKILL)
except Exception:
try:
os.kill(pid, signal.SIGKILL)
except Exception:
pass
process_pids.pop(tid, None)
_save_db()
return jsonify({"success": True})
@app.route("/api/tasks/<task_id>/logs")
def stream_logs(task_id):
q = log_queues.get(task_id)
if not q:
return jsonify({"error": "No stream"}), 404
def event_stream():
while True:
try:
msg = q.get(timeout=30)
yield f"data: {msg}\n\n"
if json.loads(msg).get("level") == "DONE":
break
except queue.Empty:
yield f"data: {json.dumps({'level': 'PING', 'msg': ''})}\n\n"
return Response(event_stream(), content_type="text/event-stream")
@app.route("/api/dashboard/stats")
def get_stats():
try:
product_filter = request.args.get("product", "")
reports = list(reports_db.values())
tasks = list(tasks_db.values())
if product_filter:
reports = [r for r in reports if r.get("product") == product_filter]
total = len(reports)
passed = sum(1 for r in reports if r.get('result') == 'PASS')
fail_count = total - passed
pass_rate = round(passed / total * 100, 1) if total > 0 else 0
trends = {}
today = datetime.now()
for i in range(6, -1, -1):
day = (today - timedelta(days=i)).strftime("%m-%d")
trends[day] = {"pass": 0, "fail": 0}
for r in reports:
f_at = r.get("finished_at")
if not f_at:
continue
try:
dt = datetime.fromisoformat(f_at).strftime("%m-%d")
if dt in trends:
if r.get("result") == "PASS":
trends[dt]["pass"] += 1
else:
trends[dt]["fail"] += 1
except Exception:
continue
health = {}
if product_filter == "dcloud" or not product_filter:
for mod_key, mod_val in DCLOUD_MODULES.items():
mod_reports = [r for r in reports if r.get("module") == mod_key]
passed_mod = sum(1 for r in mod_reports if r.get("result") == "PASS")
health[mod_val["name"]] = {
"rate": round(passed_mod / len(mod_reports) * 100) if mod_reports else 0,
"total": len(mod_reports),
}
if product_filter == "robogo" or not product_filter:
for p_key, p_val in PRODUCTS.items():
p_reports = [r for r in reports if r.get("product") == p_key]
total_p = sum(1 for r in p_reports if r.get("result") == "PASS")
health[p_val["name"]] = {
"rate": round(total_p / len(p_reports) * 100) if p_reports else 0,
"total": len(p_reports),
}
failure_map = {"元素定位/超时": 0, "业务逻辑报错": 0, "接口/网络异常": 0, "其他": 0}
failed_reports = [r for r in reports if r.get("result") == "FAIL"][-20:]
for r in failed_reports:
tid = r.get("task_id")
if not tid:
continue
log_file = os.path.join(REPORTS_DIR, f"{tid}.json")
if os.path.exists(log_file):
try:
with open(log_file, 'r') as f:
content = f.read()
if "Timeout" in content or "not found" in content:
failure_map["元素定位/超时"] += 1
elif "Exception" in content:
failure_map["业务逻辑报错"] += 1
else:
failure_map["其他"] += 1
except Exception:
pass
return jsonify({
"summary": {
"total_reports": total,
"pass_rate": pass_rate,
"fail_count": fail_count,
"closure_rate": round(passed / total * 100, 1) if total > 0 else 0
},
"trends": trends,
"health": health,
"failure_analysis": failure_map,
"ts": datetime.now().strftime("%H:%M:%S")
})
except Exception as e:
return jsonify({"error": f"Stats error: {str(e)}"}), 500
@app.route("/api/reports")
def list_reports():
product = request.args.get("product", "")
r_list = []
for tid, r in reports_db.items():
if tid in tasks_db and not tasks_db[tid].get("is_deleted"):
if product and tasks_db[tid].get("product") != product:
continue
item = r.copy()
item["thumbnail"] = None
if os.path.exists(SCREENSHOTS_DIR):
try:
for f in os.listdir(SCREENSHOTS_DIR):
if f.startswith(tid):
item["thumbnail"] = f
break
except Exception:
pass
r_list.append(item)
return jsonify(r_list)
@app.route("/api/reports/<tid>")
def get_report(tid):
r = reports_db.get(tid)
if not r:
return jsonify({"error": "Not Found"}), 404
res = r.copy()
res["results"] = []
res["logs"] = []
res["screenshots"] = []
log_path = os.path.join(REPORTS_DIR, f"{tid}.json")
if os.path.exists(log_path):
try:
with open(log_path, 'r', encoding='utf-8') as f:
res["logs"] = json.load(f).get("logs", [])
except Exception:
pass
results_path = os.path.join(REPORTS_DIR, f"{tid}_results.json")
if os.path.exists(results_path):
try:
with open(results_path, 'r', encoding='utf-8') as f:
res["results"] = json.load(f)
except Exception:
pass
if os.path.exists(SCREENSHOTS_DIR):
try:
res["screenshots"] = [s for s in os.listdir(SCREENSHOTS_DIR) if s.startswith(tid)]
except Exception:
pass
html_path = os.path.join(REPORTS_DIR, f"{tid}.html")
if os.path.exists(html_path):
res["html_report"] = f"/api/reports/{tid}/html"
return jsonify(res)
@app.route("/artifacts/screenshots/<path:filename>")
def serve_screenshot(filename):
return send_from_directory(SCREENSHOTS_DIR, filename)
@app.route("/api/reports/<tid>/html")
def serve_report_html(tid):
html_path = os.path.join(REPORTS_DIR, f"{tid}.html")
if os.path.exists(html_path):
return send_from_directory(REPORTS_DIR, f"{tid}.html")
return jsonify({"error": "Not Found"}), 404
# ── Serve React SPA (production) ───────────────────────────────────────────────
@app.route("/assets/<path:filename>")
def serve_assets(filename):
return send_from_directory(str(PROJECT_ROOT / "frontend" / "dist" / "assets"), filename)
@app.route("/")
@app.route("/<path:path>")
def serve_spa(path=""):
dist_dir = str(PROJECT_ROOT / "frontend" / "dist")
if os.path.isdir(dist_dir) and os.path.isfile(os.path.join(dist_dir, "index.html")):
return send_from_directory(dist_dir, "index.html")
return jsonify({"message": "AutoFlow API server running. Frontend not built. Run: cd frontend && pnpm build"}), 200
# ── E2E Step Runner ────────────────────────────────────────────────────────────
@app.route("/api/run-e2e-step")
def run_e2e_step():
name = request.args.get("name", "")
step = request.args.get("step", "1")
if not name:
return jsonify({"error": "Missing name parameter"}), 400
script = str(PROJECT_ROOT / "scripts" / "run_e2e_split.py")
def event_stream():
import re
start = time.time()
try:
p = subprocess.Popen(
[PYTHON_BIN, script, "--name", name, "--step", step],
cwd=str(PROJECT_ROOT),
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
bufsize=1,
)
except Exception as e:
yield f"data: {json.dumps({'type': 'log', 'msg': f'Failed: {e}', 'cls': 'text-red-400'})}\n\n"
yield f"data: {json.dumps({'type': 'result', 'passed': False, 'duration': 0, 'summary': str(e)})}\n\n"
return
try:
for line in iter(p.stdout.readline, ""):
if not line:
break
line = line.rstrip("\n")
if not line:
continue
if "ERROR" in line or "error" in line.lower():
yield f"data: {json.dumps({'type': 'log', 'msg': line, 'cls': 'text-red-400'})}\n\n"
elif "完成" in line or "成功" in line or "ok" in line.lower():
yield f"data: {json.dumps({'type': 'log', 'msg': line, 'cls': 'text-green-400'})}\n\n"
elif "创建" in line or "验证" in line or "清理" in line:
yield f"data: {json.dumps({'type': 'log', 'msg': line, 'cls': 'text-cyan-400'})}\n\n"
else:
yield f"data: {json.dumps({'type': 'log', 'msg': line, 'cls': ''})}\n\n"
p.wait()
except (BrokenPipeError, ConnectionResetError):
p.kill()
return
duration = round(time.time() - start, 1)
passed = p.returncode == 0
status = "passed" if passed else "failed"
yield f"data: {json.dumps({'type': 'result', 'passed': passed, 'duration': duration, 'summary': f'Step {step} {status}'})}\n\n"
return Response(
event_stream(),
content_type="text/event-stream",
headers={"Cache-Control": "no-cache", "Connection": "keep-alive", "Access-Control-Allow-Origin": "*"},
)
# ── Health Check ───────────────────────────────────────────────────────────────
@app.route("/api/health")
def health():
return jsonify({
"status": "ok",
"version": "2.0.0",
"uptime": datetime.now().isoformat(),
"tasks_count": len(tasks_db),
"reports_count": len(reports_db),
})
# ── Scheduler ──────────────────────────────────────────────────────────────────
class Scheduler:
def _is_in_window(self, now, window_str):
if not window_str or window_str == "all":
return True
current_time = now.strftime("%H:%M")
try:
for part in window_str.split(","):
if "-" not in part:
continue
start, end = part.strip().split("-")
if start <= current_time <= end:
return True
except Exception:
pass
return False
def start(self):
threading.Thread(target=self._loop, daemon=True).start()
def _loop(self):
while True:
try:
now = datetime.now()
for tid, task in list(tasks_db.items()):
if task["status"] == "running":
continue
stype = task.get("schedule_type", "once")
last_run = task.get("last_scheduled_run")
should_run = False
if stype == "once" and task.get("scheduled_at") and task["status"] == "pending":
if now >= datetime.fromisoformat(task["scheduled_at"]):
should_run = True
elif stype == "continuous":
window = task.get("schedule_window", "00:00-23:59")
if self._is_in_window(now, window):
if last_run:
delta = (now - datetime.fromisoformat(last_run)).total_seconds()
if delta < 60:
continue
should_run = True
elif stype != "once":
if not last_run:
should_run = True
else:
delta = (now - datetime.fromisoformat(last_run)).total_seconds()
if (stype == "hourly" and delta >= 3600) or \
(stype == "daily" and delta >= 86400) or \
(stype == "weekly" and delta >= 86400 * 7):
should_run = True
if should_run:
task["last_scheduled_run"] = now.isoformat()
if stype == "once":
task["status"] = "running"
print(f"⏰ Triggered: {task['name']} (type: {stype})")
threading.Thread(target=run_task_process, args=(task,), daemon=True).start()
except Exception as e:
print(f"❌ Scheduler error: {e}")
time.sleep(30)
# ── Startup ────────────────────────────────────────────────────────────────────
def create_app():
return app
if __name__ == "__main__":
port = int(os.getenv("PLATFORM_PORT", "5001"))
print(f"🚀 AutoFlow Platform starting... http://127.0.0.1:{port}")
Scheduler().start()
app.run(host="0.0.0.0", port=port)