diff --git a/src/components/logs-viewer/config.ts b/src/components/logs-viewer/config.ts index e986b545..fb81e382 100644 --- a/src/components/logs-viewer/config.ts +++ b/src/components/logs-viewer/config.ts @@ -2,3 +2,32 @@ export const controlSeqRegex = /\x1b\[(\d*);?(\d*)?([A-DJKHfm])/g; export const replaceLineRegex = /\r\n/g; export const PageSize = 500; + +export const throttle = void>( + func: T, + wait: number +): ((this: ThisParameterType, ...args: Parameters) => void) => { + let timeout: ReturnType | null = null; + let previous = Date.now(); + + return function (this: ThisParameterType, ...args: Parameters): void { + const now = Date.now(); + const remaining = wait - (now - previous); + const context = this as ThisParameterType; + + 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); + } + }; +}; diff --git a/src/components/logs-viewer/logs-pagination.tsx b/src/components/logs-viewer/logs-pagination.tsx index 2d9ed6d0..178b75b0 100644 --- a/src/components/logs-viewer/logs-pagination.tsx +++ b/src/components/logs-viewer/logs-pagination.tsx @@ -15,10 +15,11 @@ interface LogsPaginationProps { onPrev?: () => void; onNext?: () => void; onBackend?: () => void; + onToFirst?: () => void; } const LogsPagination: React.FC = (props) => { - const { page, total, pageSize, onNext, onPrev, onBackend } = props; + const { page, total, pageSize, onNext, onPrev, onBackend, onToFirst } = props; const intl = useIntl(); const handleOnPrev = () => { @@ -32,22 +33,37 @@ const LogsPagination: React.FC = (props) => { return (
{page > 1 && ( - - - + + + + + + )} {page} /{' '} diff --git a/src/components/logs-viewer/parse-worker.ts b/src/components/logs-viewer/parse-worker.ts index dd8492c4..9994c568 100644 --- a/src/components/logs-viewer/parse-worker.ts +++ b/src/components/logs-viewer/parse-worker.ts @@ -22,7 +22,7 @@ class AnsiParser { this.cursorCol = 0; this.screen = [['']]; this.rawDataRows = 0; - this.uid = 0; + this.uid = this.uid + 1; } private setId() { @@ -129,14 +129,16 @@ class AnsiParser { this.isProcessing = true; while (this.taskQueue.length > 0) { - const input = this.taskQueue.join(''); - this.taskQueue = []; - const result = this.processInput(input); - - self.postMessage({ - result: result.data, - lines: result.lines - }); + const input = this.taskQueue.shift(); + + if (input) { + try { + 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) => { setTimeout(resolve, 0); @@ -144,18 +146,25 @@ class AnsiParser { } this.isProcessing = false; + if (this.taskQueue.length > 0) { + this.processQueue(); + } } public enqueueData(input: string): void { this.taskQueue.push(input); - this.processQueue(); + if (!this.isProcessing) { + this.processQueue(); + } } } const parser = new AnsiParser(); self.onmessage = function (event) { - const { inputStr } = event.data; - + const { inputStr, reset } = event.data; + if (reset) { + parser.reset(); + } parser.enqueueData(inputStr); }; diff --git a/src/components/logs-viewer/virtual-log-list.tsx b/src/components/logs-viewer/virtual-log-list.tsx index 9b826017..e69f623d 100644 --- a/src/components/logs-viewer/virtual-log-list.tsx +++ b/src/components/logs-viewer/virtual-log-list.tsx @@ -49,6 +49,7 @@ const LogsViewer: React.FC = forwardRef((props, ref) => { page: 1 }); const lineCountRef = useRef(0); + const clearScreen = useRef(false); useImperativeHandle(ref, () => ({ abort() { @@ -103,7 +104,7 @@ const LogsViewer: React.FC = forwardRef((props, ref) => { const handleonBackend = useCallback(() => { pageRef.current = totalPageRef.current; getCurrent(); - setPage(pageRef.current); + console.log('pageRef.current', pageRef.current); setScrollPos(['bottom', pageRef.current]); scrollPosRef.current = { @@ -112,6 +113,17 @@ const LogsViewer: React.FC = forwardRef((props, ref) => { }; }, [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) => { if (isLoadingMoreRef.current) { setLoading(true); @@ -120,8 +132,10 @@ const LogsViewer: React.FC = forwardRef((props, ref) => { } } logParseWorker.current.postMessage({ - inputStr: data + inputStr: data, + reset: clearScreen.current }); + clearScreen.current = false; }; const createChunkConnection = async () => { @@ -181,6 +195,7 @@ const LogsViewer: React.FC = forwardRef((props, ref) => { createChunkConnection(); loadMoreDone.current = true; isLoadingMoreRef.current = true; + clearScreen.current = true; } else if (isTop && page <= totalPage && page > 1) { // getPrePage(); } else if (isBottom && page < totalPage) { @@ -264,7 +279,7 @@ const LogsViewer: React.FC = forwardRef((props, ref) => { const start = (pageRef.current - 1) * pageSize; const end = pageRef.current * pageSize; const currentLogs = result.slice(start, end); - + console.log('result+++++++', result); setLogs(result); setTotalPage(totalPageRef.current); setPage(pageRef.current); @@ -309,6 +324,7 @@ const LogsViewer: React.FC = forwardRef((props, ref) => { pageSize={pageSize} onNext={getNextPage} onPrev={getPrePage} + onToFirst={handleonToFirst} onBackend={handleonBackend} >
diff --git a/src/locales/en-US/models.ts b/src/locales/en-US/models.ts index cf16403a..6a9760c7 100644 --- a/src/locales/en-US/models.ts +++ b/src/locales/en-US/models.ts @@ -75,6 +75,7 @@ export default { 'models.logs.pagination.prev': 'Previous {lines} Lines', 'models.logs.pagination.next': 'Next {lines} Lines', 'models.logs.pagination.last': 'Last Page', + 'models.logs.pagination.first': 'First Page', 'models.form.localPath': 'Local Path', 'models.form.filePath': 'Model Path', 'models.form.backendVersion': 'Backend Version', diff --git a/src/locales/zh-CN/models.ts b/src/locales/zh-CN/models.ts index 3d44c17f..660150b8 100644 --- a/src/locales/zh-CN/models.ts +++ b/src/locales/zh-CN/models.ts @@ -72,6 +72,7 @@ export default { 'models.logs.pagination.prev': '上一 {lines} 行', 'models.logs.pagination.next': '下一 {lines} 行', 'models.logs.pagination.last': '最后一页', + 'models.logs.pagination.first': '第一页', 'models.form.localPath': '本地路径', 'models.form.filePath': '模型路径', 'models.form.backendVersion': '后端版本',