common.js
4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
文件名: app/static/js/common.js
职责: 各页面共用的工具函数和交互逻辑(历史记录渲染、任务详情弹窗、轮询)。
注意: 不要删除这个注释。
*/
// ========== 工具函数 ==========
function fmtDate(d) {
return `${d.getFullYear()}-${String(d.getMonth()+1).padStart(2,'0')}-${String(d.getDate()).padStart(2,'0')}`;
}
function statusColor(status) {
if (status === 'success') return '#52c41a';
if (status === 'failed') return '#f5222d';
return '#1890ff';
}
// ========== 全局状态 ==========
let pollingTimer = null;
let historyCache = {};
// ========== 历史记录渲染 ==========
async function fetchHistory(filterFn) {
try {
const res = await fetch('api/etl/history?limit=15');
const history = await res.json();
const tbody = document.getElementById('history-tbody');
if (!tbody) return false;
tbody.innerHTML = '';
let hasRunning = false;
history.forEach(item => {
if (filterFn && !filterFn(item)) return;
historyCache[item.id] = item.result_info;
if (item.status === 'running') hasRunning = true;
const tr = document.createElement('tr');
tr.style.borderBottom = '1px solid #f0f0f0';
const tableCount = item.tables_involved ? item.tables_involved.split(',').length : 0;
tr.innerHTML =
`<td style="padding:12px;">${item.start_time}</td>` +
`<td style="padding:12px;">${item.data_range || '-'}</td>` +
`<td style="padding:12px;" title="${item.tables_involved || ''}">${tableCount} 张表</td>` +
`<td style="padding:12px;color:${statusColor(item.status)};">${item.status}</td>` +
`<td style="padding:12px;"><a href="#" onclick="showTaskDetail('${item.id}');return false;">查看</a></td>`;
tbody.appendChild(tr);
});
return hasRunning;
} catch (err) {
console.error('获取历史失败:', err);
return false;
}
}
function startPolling(filterFn) {
if (pollingTimer) clearInterval(pollingTimer);
let count = 0;
pollingTimer = setInterval(async () => {
count++;
const hasRunning = await fetchHistory(filterFn);
if (!hasRunning || count > 20) {
clearInterval(pollingTimer);
pollingTimer = null;
}
}, 3000);
}
// ========== 任务详情弹窗 ==========
window.showTaskDetail = function(id) {
const info = historyCache[id];
if (!info || info === "null" || info === "") { alert("暂无详情数据"); return; }
const modal = document.getElementById('detail-modal');
const tbody = document.getElementById('detail-tbody');
if (!modal || !tbody) return;
tbody.innerHTML = '';
try {
const data = JSON.parse(info);
if (Array.isArray(data)) {
data.forEach(item => {
const tr = document.createElement('tr');
const cls = item.status === 'success' ? 'status-success' : 'status-failed';
tr.innerHTML =
`<td>${item.source || item.table || '-'}</td>` +
`<td>${item.target || '-'}</td>` +
`<td style="font-size:11px;color:#666;">${item.range_start ? '从:' + item.range_start + '<br>至:' + item.range_end : '-'}</td>` +
`<td><strong>${item.rows ?? 0}</strong> 行</td>` +
`<td><span class="status-tag ${cls}">${item.status || 'unknown'}</span></td>`;
tbody.appendChild(tr);
});
} else {
tbody.innerHTML = `<tr><td colspan="4"><pre style="white-space:pre-wrap;">${JSON.stringify(data,null,2)}</pre></td></tr>`;
}
modal.style.display = 'block';
} catch(e) {
tbody.innerHTML = `<tr><td colspan="4">${info}</td></tr>`;
modal.style.display = 'block';
}
};
window.closeModal = function() {
const modal = document.getElementById('detail-modal');
if (modal) modal.style.display = 'none';
};
window.onclick = function(event) {
const modal = document.getElementById('detail-modal');
if (modal && event.target == modal) modal.style.display = 'none';
};