-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathLogsTable.tsx
More file actions
202 lines (186 loc) · 7.03 KB
/
LogsTable.tsx
File metadata and controls
202 lines (186 loc) · 7.03 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import { useEffect, useState, useRef, useMemo } from "react";
import { useParams } from "react-router";
import { FileClock, Download } from "lucide-react";
import { useTranslation } from "react-i18next";
import { App } from "antd";
import VirtualList from 'rc-virtual-list';
import { streamCleaningTaskLog, downloadCleaningTaskLog } from "../../cleansing.api";
import { TaskStatus } from "../../cleansing.model";
interface LogEntry {
level: string;
message: string;
}
interface LogEntryWithIndex extends LogEntry {
index: number;
}
export default function LogsTable({
taskLog: initialLogs,
fetchTaskLog,
retryCount,
taskName,
taskStatus
}: {
taskLog: LogEntry[],
fetchTaskLog: () => Promise<LogEntry[]>,
retryCount: number,
taskName: string,
taskStatus?: TaskStatus
}) {
const { id = "" } = useParams();
const { t } = useTranslation();
const [selectedLog, setSelectedLog] = useState(retryCount + 1);
const [streamingLogs, setStreamingLogs] = useState<LogEntry[]>([]);
const [isStreaming, setIsStreaming] = useState(false);
const eventSourceRef = useRef<EventSource | null>(null);
const { message } = App.useApp();
// Only stream when task is RUNNING and viewing the latest run
const shouldStream = taskStatus === TaskStatus.RUNNING && selectedLog - 1 === retryCount;
useEffect(() => {
if (shouldStream) {
startStreaming();
} else {
stopStreaming();
fetchTaskLog(selectedLog - 1).then(() => {
// Static logs loaded, safe to clear streaming logs now
setStreamingLogs([]);
});
}
return () => stopStreaming();
}, [id, selectedLog, retryCount, shouldStream]);
const startStreaming = () => {
stopStreaming();
setStreamingLogs([]);
setIsStreaming(true);
const eventSource = streamCleaningTaskLog(id, selectedLog - 1);
eventSourceRef.current = eventSource;
eventSource.onmessage = (event) => {
try {
const logEntry: LogEntry = JSON.parse(event.data);
if (logEntry.message === "[END_OF_STREAM]" || logEntry.message === "[HEARTBEAT]") {
if (logEntry.message === "[END_OF_STREAM]") {
// Don't clear streamingLogs immediately - keep them visible
// while the static fetch completes
eventSourceRef.current?.close();
eventSourceRef.current = null;
setIsStreaming(false);
}
return;
}
setStreamingLogs(prev => [...prev, logEntry]);
} catch (e) {
console.error("Failed to parse log entry:", e);
}
};
eventSource.onerror = (error) => {
console.error("SSE error:", error);
stopStreaming();
};
};
const stopStreaming = () => {
if (eventSourceRef.current) {
eventSourceRef.current.close();
eventSourceRef.current = null;
}
setIsStreaming(false);
};
// Use streaming logs only when actively streaming, otherwise use initial logs
// Keep streamingLogs visible until initialLogs are populated (avoids blank flash)
const displayLogs = isStreaming || streamingLogs.length > 0
? (streamingLogs.length > 0 ? streamingLogs : initialLogs)
: initialLogs;
// Add index to logs for virtual list key
const logsWithIndex: LogEntryWithIndex[] = useMemo(() => {
return (displayLogs || []).map((log, index) => ({ ...log, index }));
}, [displayLogs]);
// Get log level color class
const getLevelClass = (level: string) => {
if (level === "ERROR" || level === "FATAL") return "text-red-500";
if (level === "WARNING" || level === "WARN") return "text-yellow-500";
return "text-green-500";
};
const handleSelectChange = (value: number) => {
setSelectedLog(value);
setStreamingLogs([]);
};
const handleDownload = async () => {
try {
const blob = await downloadCleaningTaskLog(id, selectedLog - 1);
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `${taskName}_${t("dataCleansing.logTable.nthRun", { num: selectedLog })}.log`;
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
document.body.removeChild(a);
} catch (error) {
console.error("Failed to download log:", error);
message.error(t("dataCleansing.detail.logTable.downloadFailed"));
}
};
return displayLogs?.length > 0 || isStreaming ? (
<>
<div className="flex items-center justify-between pb-3">
<div className="flex items-center gap-3">
<label className="text-sm font-medium text-gray-500">{t("dataCleansing.detail.logTable.selectRun")}:</label>
<select
value={selectedLog}
onChange={(e) => handleSelectChange(Number(e.target.value))}
className="bg-gray-700 border border-gray-600 !text-white text-sm rounded-md focus:ring-blue-500 focus:border-blue-500 block px-2.5 py-1.5 min-w-[120px]"
>
{Array.from({ length: retryCount + 1 }, (_, i) => retryCount + 1 - i).map((num) => (
<option key={num} value={num}>
{t("dataCleansing.detail.logTable.currentDisplay", { num: num })}
</option>
))}
</select>
{isStreaming && (
<span className="text-xs text-blue-400 animate-pulse">{t("dataCleansing.detail.logTable.streaming")}</span>
)}
</div>
<div className="flex items-center gap-3">
<span className="text-s text-gray-500 px-2">{t("dataCleansing.detail.logTable.nthRun", { num: selectedLog })}</span>
<button
onClick={handleDownload}
disabled={isStreaming}
className="flex items-center gap-2 px-3 py-1.5 text-sm text-white bg-blue-600 hover:bg-blue-700 border border-blue-500 rounded-md transition-colors disabled:opacity-50 disabled:cursor-not-allowed shadow-sm"
>
<Download className="w-4 h-4" />
<span>{t("dataCleansing.detail.logTable.download")}</span>
</button>
</div>
</div>
<div className="text-gray-300 p-4 border border-gray-700 bg-gray-800 rounded-lg">
<div className="font-mono text-sm">
<VirtualList
data={logsWithIndex}
height={580}
itemHeight={22}
itemKey="index"
>
{(log: LogEntryWithIndex) => (
<div className="flex gap-3 py-0.5">
<span className={`min-w-20 ${getLevelClass(log.level)}`}>
[{log.level}]
</span>
<span className="text-gray-100">{log.message}</span>
</div>
)}
</VirtualList>
{isStreaming && (
<div className="flex gap-3 animate-pulse py-0.5">
<span className="text-gray-400">...</span>
</div>
)}
</div>
</div>
</>
) : (
<div className="text-center py-12">
<FileClock className="w-16 h-16 text-gray-300 mx-auto mb-4" />
<h3 className="text-lg font-medium text-gray-900 mb-2">
{t("dataCleansing.detail.logTable.noLogs")}
</h3>
</div>
);
}