fix: clear screen logs after loading all

This commit is contained in:
jialin
2025-01-13 11:15:34 +08:00
parent 8c4997fa37
commit 5c1bcdbd74
6 changed files with 102 additions and 30 deletions
+29
View File
@@ -2,3 +2,32 @@ export const controlSeqRegex = /\x1b\[(\d*);?(\d*)?([A-DJKHfm])/g;
export const replaceLineRegex = /\r\n/g; export const replaceLineRegex = /\r\n/g;
export const PageSize = 500; export const PageSize = 500;
export const throttle = <T extends (...args: any[]) => void>(
func: T,
wait: number
): ((this: ThisParameterType<T>, ...args: Parameters<T>) => void) => {
let timeout: ReturnType<typeof setTimeout> | null = null;
let previous = Date.now();
return function (this: ThisParameterType<T>, ...args: Parameters<T>): void {
const now = Date.now();
const remaining = wait - (now - previous);
const context = this as ThisParameterType<T>;
if (remaining <= 0 || remaining > wait) {
if (timeout) {
clearTimeout(timeout);
timeout = null;
}
previous = now;
func.apply(context, args);
} else if (!timeout) {
timeout = setTimeout(() => {
previous = Date.now();
timeout = null;
func.apply(context, args);
}, remaining);
}
};
};
+17 -1
View File
@@ -15,10 +15,11 @@ interface LogsPaginationProps {
onPrev?: () => void; onPrev?: () => void;
onNext?: () => void; onNext?: () => void;
onBackend?: () => void; onBackend?: () => void;
onToFirst?: () => void;
} }
const LogsPagination: React.FC<LogsPaginationProps> = (props) => { const LogsPagination: React.FC<LogsPaginationProps> = (props) => {
const { page, total, pageSize, onNext, onPrev, onBackend } = props; const { page, total, pageSize, onNext, onPrev, onBackend, onToFirst } = props;
const intl = useIntl(); const intl = useIntl();
const handleOnPrev = () => { const handleOnPrev = () => {
@@ -32,6 +33,20 @@ const LogsPagination: React.FC<LogsPaginationProps> = (props) => {
return ( return (
<div className="pagination"> <div className="pagination">
{page > 1 && ( {page > 1 && (
<>
<Tooltip
title={intl.formatMessage({ id: 'models.logs.pagination.first' })}
placement="left"
>
<Button
onClick={onToFirst}
type="text"
shape="circle"
style={{ color: 'rgba(255,255,255,.7)', marginBottom: 10 }}
>
<VerticalLeftOutlined rotate={-90} />
</Button>
</Tooltip>
<Tooltip <Tooltip
placement="left" placement="left"
title={intl.formatMessage( title={intl.formatMessage(
@@ -48,6 +63,7 @@ const LogsPagination: React.FC<LogsPaginationProps> = (props) => {
<UpOutlined /> <UpOutlined />
</Button> </Button>
</Tooltip> </Tooltip>
</>
)} )}
<span className="pages"> <span className="pages">
<span className="curr">{page}</span> /{' '} <span className="curr">{page}</span> /{' '}
+19 -10
View File
@@ -22,7 +22,7 @@ class AnsiParser {
this.cursorCol = 0; this.cursorCol = 0;
this.screen = [['']]; this.screen = [['']];
this.rawDataRows = 0; this.rawDataRows = 0;
this.uid = 0; this.uid = this.uid + 1;
} }
private setId() { private setId() {
@@ -129,14 +129,16 @@ class AnsiParser {
this.isProcessing = true; this.isProcessing = true;
while (this.taskQueue.length > 0) { while (this.taskQueue.length > 0) {
const input = this.taskQueue.join(''); const input = this.taskQueue.shift();
this.taskQueue = [];
const result = this.processInput(input);
self.postMessage({ if (input) {
result: result.data, try {
lines: result.lines const result = this.processInput(input);
}); self.postMessage({ result: result.data, lines: result.lines });
} catch (error) {
console.error('Error processing input:', error);
}
}
await new Promise((resolve) => { await new Promise((resolve) => {
setTimeout(resolve, 0); setTimeout(resolve, 0);
@@ -144,18 +146,25 @@ class AnsiParser {
} }
this.isProcessing = false; this.isProcessing = false;
if (this.taskQueue.length > 0) {
this.processQueue();
}
} }
public enqueueData(input: string): void { public enqueueData(input: string): void {
this.taskQueue.push(input); this.taskQueue.push(input);
if (!this.isProcessing) {
this.processQueue(); this.processQueue();
} }
} }
}
const parser = new AnsiParser(); const parser = new AnsiParser();
self.onmessage = function (event) { self.onmessage = function (event) {
const { inputStr } = event.data; const { inputStr, reset } = event.data;
if (reset) {
parser.reset();
}
parser.enqueueData(inputStr); parser.enqueueData(inputStr);
}; };
@@ -49,6 +49,7 @@ const LogsViewer: React.FC<LogsViewerProps> = forwardRef((props, ref) => {
page: 1 page: 1
}); });
const lineCountRef = useRef(0); const lineCountRef = useRef(0);
const clearScreen = useRef(false);
useImperativeHandle(ref, () => ({ useImperativeHandle(ref, () => ({
abort() { abort() {
@@ -103,7 +104,7 @@ const LogsViewer: React.FC<LogsViewerProps> = forwardRef((props, ref) => {
const handleonBackend = useCallback(() => { const handleonBackend = useCallback(() => {
pageRef.current = totalPageRef.current; pageRef.current = totalPageRef.current;
getCurrent(); getCurrent();
setPage(pageRef.current);
console.log('pageRef.current', pageRef.current); console.log('pageRef.current', pageRef.current);
setScrollPos(['bottom', pageRef.current]); setScrollPos(['bottom', pageRef.current]);
scrollPosRef.current = { scrollPosRef.current = {
@@ -112,6 +113,17 @@ const LogsViewer: React.FC<LogsViewerProps> = forwardRef((props, ref) => {
}; };
}, [getCurrent]); }, [getCurrent]);
const handleonToFirst = useCallback(() => {
pageRef.current = 1;
getCurrent();
console.log('pageRef.current', pageRef.current);
setScrollPos(['top', pageRef.current]);
scrollPosRef.current = {
pos: 'top',
page: pageRef.current
};
}, [getCurrent]);
const updateContent = (data: string) => { const updateContent = (data: string) => {
if (isLoadingMoreRef.current) { if (isLoadingMoreRef.current) {
setLoading(true); setLoading(true);
@@ -120,8 +132,10 @@ const LogsViewer: React.FC<LogsViewerProps> = forwardRef((props, ref) => {
} }
} }
logParseWorker.current.postMessage({ logParseWorker.current.postMessage({
inputStr: data inputStr: data,
reset: clearScreen.current
}); });
clearScreen.current = false;
}; };
const createChunkConnection = async () => { const createChunkConnection = async () => {
@@ -181,6 +195,7 @@ const LogsViewer: React.FC<LogsViewerProps> = forwardRef((props, ref) => {
createChunkConnection(); createChunkConnection();
loadMoreDone.current = true; loadMoreDone.current = true;
isLoadingMoreRef.current = true; isLoadingMoreRef.current = true;
clearScreen.current = true;
} else if (isTop && page <= totalPage && page > 1) { } else if (isTop && page <= totalPage && page > 1) {
// getPrePage(); // getPrePage();
} else if (isBottom && page < totalPage) { } else if (isBottom && page < totalPage) {
@@ -264,7 +279,7 @@ const LogsViewer: React.FC<LogsViewerProps> = forwardRef((props, ref) => {
const start = (pageRef.current - 1) * pageSize; const start = (pageRef.current - 1) * pageSize;
const end = pageRef.current * pageSize; const end = pageRef.current * pageSize;
const currentLogs = result.slice(start, end); const currentLogs = result.slice(start, end);
console.log('result+++++++', result);
setLogs(result); setLogs(result);
setTotalPage(totalPageRef.current); setTotalPage(totalPageRef.current);
setPage(pageRef.current); setPage(pageRef.current);
@@ -309,6 +324,7 @@ const LogsViewer: React.FC<LogsViewerProps> = forwardRef((props, ref) => {
pageSize={pageSize} pageSize={pageSize}
onNext={getNextPage} onNext={getNextPage}
onPrev={getPrePage} onPrev={getPrePage}
onToFirst={handleonToFirst}
onBackend={handleonBackend} onBackend={handleonBackend}
></LogsPagination> ></LogsPagination>
</div> </div>
+1
View File
@@ -75,6 +75,7 @@ export default {
'models.logs.pagination.prev': 'Previous {lines} Lines', 'models.logs.pagination.prev': 'Previous {lines} Lines',
'models.logs.pagination.next': 'Next {lines} Lines', 'models.logs.pagination.next': 'Next {lines} Lines',
'models.logs.pagination.last': 'Last Page', 'models.logs.pagination.last': 'Last Page',
'models.logs.pagination.first': 'First Page',
'models.form.localPath': 'Local Path', 'models.form.localPath': 'Local Path',
'models.form.filePath': 'Model Path', 'models.form.filePath': 'Model Path',
'models.form.backendVersion': 'Backend Version', 'models.form.backendVersion': 'Backend Version',
+1
View File
@@ -72,6 +72,7 @@ export default {
'models.logs.pagination.prev': '上一 {lines} 行', 'models.logs.pagination.prev': '上一 {lines} 行',
'models.logs.pagination.next': '下一 {lines} 行', 'models.logs.pagination.next': '下一 {lines} 行',
'models.logs.pagination.last': '最后一页', 'models.logs.pagination.last': '最后一页',
'models.logs.pagination.first': '第一页',
'models.form.localPath': '本地路径', 'models.form.localPath': '本地路径',
'models.form.filePath': '模型路径', 'models.form.filePath': '模型路径',
'models.form.backendVersion': '后端版本', 'models.form.backendVersion': '后端版本',