1.Excel文件字段顺序调整
2.测试排程时增加执行记录,编号与其他场景编号分开
This commit is contained in:
1 parent
ddc461f548
commit
ae6ced00b5
10 files changed
+194
-76
No files matched your search
@@ -10,7 +10,7 @@ from app import db
|
||||
from app.models import ScheduleRun, ScheduleTask, ScheduleTaskConfig
|
||||
from app.auth_jwt import get_current_user
|
||||
from app.services.schedule.query_service import get_run, list_runs, list_tasks, serialize_run, serialize_task
|
||||
from app.services.schedule.scheduler_service import create_task, run_task_now, update_task, _generate_run_no
|
||||
from app.services.schedule.scheduler_service import create_task, run_task_now, update_task, _generate_run_no, _generate_test_run_no
|
||||
from app.services.schedule.sql_config import get_schedule_sql_config_by_task_id, upsert_schedule_task_config, _build_fetch_sql
|
||||
from app.services.schedule.srm_service import _fetch_records
|
||||
|
||||
@@ -140,6 +140,9 @@ def test_schedule_sql(task_id: int):
|
||||
@schedule_admin_bp.route('/schedule-config/<int:task_id>/test-run', methods=['POST'])
|
||||
@require_admin
|
||||
def test_schedule_run(task_id: int):
|
||||
import time as _time
|
||||
from app.services.schedule.excel_service import export_schedule_result_excel
|
||||
|
||||
task = db.session.get(ScheduleTask, task_id)
|
||||
if not task:
|
||||
return jsonify({'error': 'Not found'}), 404
|
||||
@@ -176,10 +179,25 @@ def test_schedule_run(task_id: int):
|
||||
wait_seconds = task.wait_seconds if task else 120
|
||||
dify_agent_id = task.dify_agent_id if task else None
|
||||
|
||||
run = ScheduleRun(
|
||||
run_no=_generate_test_run_no(),
|
||||
schedule_task_id=task_id,
|
||||
trigger_type='test',
|
||||
triggered_by_user_id=getattr(get_current_user(), 'id', None),
|
||||
started_at=datetime.utcnow(),
|
||||
status='running',
|
||||
)
|
||||
db.session.add(run)
|
||||
db.session.commit()
|
||||
|
||||
if not cfg.fetch_sql:
|
||||
run_no = f"TEST{datetime.utcnow().strftime('%Y%m%d%H%M%S')}"
|
||||
run.status = 'success'
|
||||
run.finished_at = datetime.utcnow()
|
||||
run.error_message = '无抓取SQL,跳过外部系统数据抓取'
|
||||
db.session.commit()
|
||||
return jsonify({
|
||||
"run_no": run_no,
|
||||
"run_no": run.run_no,
|
||||
"run_id": run.id,
|
||||
"sql": "",
|
||||
"limit": 0,
|
||||
"total_count": 0,
|
||||
@@ -222,7 +240,7 @@ def test_schedule_run(task_id: int):
|
||||
test_pre_dify = _test_pre_dify
|
||||
|
||||
def _test_build_with_detail(conn_str_arg, item, row_id_value):
|
||||
detail_rows = item.pop("_detail_rows", [])
|
||||
detail_rows = item.get("_detail_rows", [])
|
||||
text = _build_record_text(item, cfg.alias_order, cfg.header_labels, detail_rows, cfg.detail_alias_order)
|
||||
return text, item.get("attachment")
|
||||
test_build_record = _test_build_with_detail
|
||||
@@ -232,16 +250,15 @@ def test_schedule_run(task_id: int):
|
||||
return text, item.get("attachment")
|
||||
test_build_record = _test_build_simple
|
||||
|
||||
run_no = f"TEST{datetime.utcnow().strftime('%Y%m%d%H%M%S')}"
|
||||
|
||||
start_ts = _time.time()
|
||||
try:
|
||||
total_count, success_count, failed_count, skipped_count, details, errors, step_logs = _run_check_job_loop(
|
||||
conn_str=conn_str,
|
||||
run_no=run_no,
|
||||
run_no=run.run_no,
|
||||
agent=agent,
|
||||
user=user,
|
||||
wait_seconds=wait_seconds,
|
||||
run_id=None,
|
||||
run_id=run.id,
|
||||
fetch_fn=_test_fetch_fn,
|
||||
build_record_fn=test_build_record,
|
||||
writeback_fn=_test_writeback_fn,
|
||||
@@ -253,6 +270,45 @@ def test_schedule_run(task_id: int):
|
||||
pre_dify_fn=test_pre_dify,
|
||||
)
|
||||
|
||||
duration_ms = int((_time.time() - start_ts) * 1000)
|
||||
if total_count == 0 and not errors:
|
||||
status = 'success'
|
||||
elif failed_count == 0:
|
||||
status = 'success'
|
||||
elif success_count == 0:
|
||||
status = 'failed'
|
||||
else:
|
||||
status = 'partial_failed'
|
||||
|
||||
header_order = cfg.alias_order if cfg.alias_order else None
|
||||
header_labels = cfg.header_labels if cfg.header_labels else None
|
||||
detail_alias_order = cfg.detail_alias_order or []
|
||||
try:
|
||||
file_data, file_name, file_size = export_schedule_result_excel(
|
||||
run_no=run.run_no,
|
||||
summary={"status": status, "total_count": total_count, "success_count": success_count, "failed_count": failed_count, "duration_ms": duration_ms},
|
||||
details=details,
|
||||
header_order=header_order,
|
||||
header_labels=header_labels,
|
||||
detail_alias_order=detail_alias_order,
|
||||
)
|
||||
except Exception:
|
||||
file_data, file_name, file_size = None, None, 0
|
||||
|
||||
run.finished_at = datetime.utcnow()
|
||||
run.duration_ms = duration_ms
|
||||
run.status = status
|
||||
run.total_count = total_count
|
||||
run.success_count = success_count
|
||||
run.failed_count = failed_count
|
||||
run.skipped_count = skipped_count
|
||||
run.step_logs = step_logs if step_logs else None
|
||||
run.error_message = "; ".join(errors[:3]) if errors else None
|
||||
run.excel_file_data = file_data
|
||||
run.excel_file_name = file_name
|
||||
run.excel_file_size = file_size or 0
|
||||
db.session.commit()
|
||||
|
||||
import decimal, datetime as dt
|
||||
def _safe(v):
|
||||
if isinstance(v, decimal.Decimal):
|
||||
@@ -269,7 +325,8 @@ def test_schedule_run(task_id: int):
|
||||
safe_details.append(safe_item)
|
||||
|
||||
return jsonify({
|
||||
"run_no": run_no,
|
||||
"run_no": run.run_no,
|
||||
"run_id": run.id,
|
||||
"sql": test_sql,
|
||||
"limit": limit,
|
||||
"total_count": total_count,
|
||||
@@ -282,6 +339,12 @@ def test_schedule_run(task_id: int):
|
||||
"dify_agent_id": dify_agent_id,
|
||||
})
|
||||
except Exception as e:
|
||||
duration_ms = int((_time.time() - start_ts) * 1000)
|
||||
run.finished_at = datetime.utcnow()
|
||||
run.duration_ms = duration_ms
|
||||
run.status = 'failed'
|
||||
run.error_message = str(e)
|
||||
db.session.commit()
|
||||
import traceback
|
||||
return jsonify({"error": str(e), "sql": test_sql, "traceback": traceback.format_exc()}), 400
|
||||
|
||||
@@ -373,16 +436,6 @@ def delete_schedule(task_id: int):
|
||||
if running_runs:
|
||||
db.session.commit()
|
||||
|
||||
runs = db.session.query(ScheduleRun).filter(ScheduleRun.schedule_task_id == task_id).all()
|
||||
for r in runs:
|
||||
if r.excel_file_path:
|
||||
try:
|
||||
from pathlib import Path
|
||||
p = Path(r.excel_file_path)
|
||||
if p.exists():
|
||||
p.unlink()
|
||||
except Exception:
|
||||
pass
|
||||
db.session.query(ScheduleRun).filter(ScheduleRun.schedule_task_id == task_id).delete(synchronize_session=False)
|
||||
db.session.delete(task)
|
||||
db.session.commit()
|
||||
@@ -508,9 +561,11 @@ def get_schedule_run(run_id: int):
|
||||
@schedule_admin_bp.route('/schedule-runs/<int:run_id>/excel', methods=['GET'])
|
||||
@require_admin
|
||||
def get_schedule_run_excel(run_id: int):
|
||||
from io import BytesIO
|
||||
run = get_run(run_id)
|
||||
if not run:
|
||||
return jsonify({'error': 'Not found'}), 404
|
||||
if not run.excel_file_path:
|
||||
if not run.excel_file_data:
|
||||
return jsonify({'error': 'Excel not found'}), 404
|
||||
return send_file(run.excel_file_path, as_attachment=True, download_name=run.excel_file_name or 'result.xlsx')
|
||||
buf = BytesIO(run.excel_file_data)
|
||||
return send_file(buf, as_attachment=True, download_name=run.excel_file_name or 'result.xlsx', mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
|
||||
Reference in new issue
Block a user