fix: 增加任务详情自动刷新

This commit is contained in:
meijiali
2026-07-03 12:35:35 +08:00
parent 0a3477cc44
commit b9291f9011
3 changed files with 49 additions and 3 deletions
+20
View File
@@ -71,6 +71,26 @@ document.addEventListener("DOMContentLoaded", () => {
updateScaleHint();
});
function pollTaskDetailStatus(taskId) {
if (!taskId) return;
const intervalMs = 3000;
const poll = async () => {
try {
const resp = await fetch(`/api/tasks/${taskId}`, { cache: "no-store" });
if (!resp.ok) return;
const task = await resp.json();
if (task.status !== "running") {
window.location.reload();
return;
}
} catch (_error) {
return;
}
window.setTimeout(poll, intervalMs);
};
window.setTimeout(poll, intervalMs);
}
async function downloadExport(url, defaultFilename, event) {
if (event) event.preventDefault();
const resp = await fetch(url);
+3 -2
View File
@@ -9,9 +9,9 @@
{% block content %}
<div class="d-flex justify-content-between align-items-center mb-3">
<h1 class="h3 mb-0">任务 #{{ task.id }}</h1>
<button class="btn btn-outline-secondary btn-sm" onclick="window.location.href='/'">手动刷新</button>
<button class="btn btn-outline-secondary btn-sm" onclick="window.location.reload()">手动刷新</button>
</div>
<section class="card mb-4"><div class="card-body">
<section class="card mb-4" {% if task.status == "running" %}data-task-id="{{ task.id }}" data-auto-poll="true"{% endif %}><div class="card-body">
{% set label, cls = status_badge_config(task.status) %}
<p>平台:{{ task.platform | platform_label }} <span class="badge {{ cls }}">{{ label }}</span></p>
<p>成功 {{ task.successful_items_count }} / 共 {{ task.total_items_count }} 条内容</p>
@@ -19,6 +19,7 @@
</div></section>
{% if task.status == "running" and not hotspots %}
<div class="text-center text-muted py-5"><div class="spinner-border text-warning mb-3"></div><p>正在抓取热点数据,请稍候...</p></div>
<script>pollTaskDetailStatus("{{ task.id }}");</script>
{% endif %}
<div class="accordion" id="hotspot-list">
{% for hotspot in hotspots %}
+26 -1
View File
@@ -42,6 +42,31 @@ def test_index_page_lists_existing_tasks():
assert "运行中" in response.text
def test_running_task_detail_page_auto_polls_current_task():
with make_test_client() as (client, engine):
from sqlalchemy.orm import Session
with Session(engine) as session:
session.add(
Task(
id="task-running",
platform="xiaohongshu",
status="running",
hotspot_limit=1,
item_limit_per_hotspot=1,
comment_limit_per_item=10,
)
)
session.commit()
response = client.get("/tasks/task-running")
assert response.status_code == 200
assert 'data-task-id="task-running"' in response.text
assert "pollTaskDetailStatus" in response.text
assert 'onclick="window.location.reload()"' in response.text
def seed_result_data(engine):
from sqlalchemy.orm import Session
@@ -119,7 +144,7 @@ def test_result_pages_render_seeded_data():
assert task_response.status_code == 200
assert "热点标题" in task_response.text
assert 'onclick="window.location.href=\'/\'"' in task_response.text
assert 'onclick="window.location.reload()"' in task_response.text
assert hotspot_response.status_code == 200
assert "热点总结" in hotspot_response.text
assert item_response.status_code == 200