fix: log pagination

This commit is contained in:
QuentinHsu 2024-08-11 11:25:32 +08:00
parent d6f6403fd3
commit 9edb7c4ade
No known key found for this signature in database
GPG Key ID: 20D465A435D740D0

View File

@ -1,11 +1,12 @@
import React, { useEffect, useState } from 'react'; import React, { useEffect, useState } from 'react';
import { import {
API, API,
copy, getTodayStartTimestamp, copy,
getTodayStartTimestamp,
isAdmin, isAdmin,
showError, showError,
showSuccess, showSuccess,
timestamp2string timestamp2string,
} from '../helpers'; } from '../helpers';
import { import {
@ -29,7 +30,7 @@ import {
stringToColor, stringToColor,
} from '../helpers/render'; } from '../helpers/render';
import Paragraph from '@douyinfe/semi-ui/lib/es/typography/paragraph'; import Paragraph from '@douyinfe/semi-ui/lib/es/typography/paragraph';
import {getLogOther} from "../helpers/other.js"; import { getLogOther } from '../helpers/other.js';
const { Header } = Layout; const { Header } = Layout;
@ -144,7 +145,7 @@ function renderUseTime(type) {
function renderFirstUseTime(type) { function renderFirstUseTime(type) {
let time = parseFloat(type) / 1000.0; let time = parseFloat(type) / 1000.0;
time = time.toFixed(1) time = time.toFixed(1);
if (time < 3) { if (time < 3) {
return ( return (
<Tag color='green' size='large'> <Tag color='green' size='large'>
@ -344,7 +345,7 @@ const LogsTable = () => {
if (record.other !== '') { if (record.other !== '') {
let other = JSON.parse(record.other); let other = JSON.parse(record.other);
if (other === null) { if (other === null) {
return <></> return <></>;
} }
if (other.admin_info !== undefined) { if (other.admin_info !== undefined) {
if ( if (
@ -414,8 +415,6 @@ const LogsTable = () => {
const [activePage, setActivePage] = useState(1); const [activePage, setActivePage] = useState(1);
const [logCount, setLogCount] = useState(ITEMS_PER_PAGE); const [logCount, setLogCount] = useState(ITEMS_PER_PAGE);
const [pageSize, setPageSize] = useState(ITEMS_PER_PAGE); const [pageSize, setPageSize] = useState(ITEMS_PER_PAGE);
const [searchKeyword, setSearchKeyword] = useState('');
const [searching, setSearching] = useState(false);
const [logType, setLogType] = useState(0); const [logType, setLogType] = useState(0);
const isAdminUser = isAdmin(); const isAdminUser = isAdmin();
let now = new Date(); let now = new Date();
@ -451,9 +450,7 @@ const LogsTable = () => {
let localEndTimestamp = Date.parse(end_timestamp) / 1000; let localEndTimestamp = Date.parse(end_timestamp) / 1000;
let url = `/api/log/self/stat?type=${logType}&token_name=${token_name}&model_name=${model_name}&start_timestamp=${localStartTimestamp}&end_timestamp=${localEndTimestamp}`; let url = `/api/log/self/stat?type=${logType}&token_name=${token_name}&model_name=${model_name}&start_timestamp=${localStartTimestamp}&end_timestamp=${localEndTimestamp}`;
url = encodeURI(url); url = encodeURI(url);
let res = await API.get( let res = await API.get(url);
url,
);
const { success, message, data } = res.data; const { success, message, data } = res.data;
if (success) { if (success) {
setStat(data); setStat(data);
@ -467,9 +464,7 @@ const LogsTable = () => {
let localEndTimestamp = Date.parse(end_timestamp) / 1000; let localEndTimestamp = Date.parse(end_timestamp) / 1000;
let url = `/api/log/stat?type=${logType}&username=${username}&token_name=${token_name}&model_name=${model_name}&start_timestamp=${localStartTimestamp}&end_timestamp=${localEndTimestamp}&channel=${channel}`; let url = `/api/log/stat?type=${logType}&username=${username}&token_name=${token_name}&model_name=${model_name}&start_timestamp=${localStartTimestamp}&end_timestamp=${localEndTimestamp}&channel=${channel}`;
url = encodeURI(url); url = encodeURI(url);
let res = await API.get( let res = await API.get(url);
url,
);
const { success, message, data } = res.data; const { success, message, data } = res.data;
if (success) { if (success) {
setStat(data); setStat(data);
@ -521,10 +516,7 @@ const LogsTable = () => {
logs[i].timestamp2string = timestamp2string(logs[i].created_at); logs[i].timestamp2string = timestamp2string(logs[i].created_at);
logs[i].key = '' + logs[i].id; logs[i].key = '' + logs[i].id;
} }
// data.key = '' + data.id
setLogs(logs); setLogs(logs);
setLogCount(logs.length + ITEMS_PER_PAGE);
// console.log(logCount);
}; };
const loadLogs = async (startIdx, pageSize, logType = 0) => { const loadLogs = async (startIdx, pageSize, logType = 0) => {
@ -542,37 +534,28 @@ const LogsTable = () => {
const res = await API.get(url); const res = await API.get(url);
const { success, message, data } = res.data; const { success, message, data } = res.data;
if (success) { if (success) {
if (startIdx === 0) { const newPageData = data.items;
setLogsFormat(data); setActivePage(data.page);
} else { setPageSize(data.page_size);
let newLogs = [...logs]; setLogCount(data.total);
newLogs.splice(startIdx * pageSize, data.length, ...data);
setLogsFormat(newLogs); setLogsFormat(newPageData);
}
} else { } else {
showError(message); showError(message);
} }
setLoading(false); setLoading(false);
}; };
const pageData = logs.slice(
(activePage - 1) * pageSize,
activePage * pageSize,
);
const handlePageChange = (page) => { const handlePageChange = (page) => {
setActivePage(page); setActivePage(page);
if (page === Math.ceil(logs.length / pageSize) + 1) { loadLogs(page, pageSize, logType).then((r) => {});
// In this case we have to load more data and then append them.
loadLogs(page - 1, pageSize, logType).then((r) => {});
}
}; };
const handlePageSizeChange = async (size) => { const handlePageSizeChange = async (size) => {
localStorage.setItem('page-size', size + ''); localStorage.setItem('page-size', size + '');
setPageSize(size); setPageSize(size);
setActivePage(1); setActivePage(1);
loadLogs(0, size) loadLogs(activePage, size)
.then() .then()
.catch((reason) => { .catch((reason) => {
showError(reason); showError(reason);
@ -580,27 +563,24 @@ const LogsTable = () => {
}; };
const refresh = async () => { const refresh = async () => {
// setLoading(true);
setActivePage(1); setActivePage(1);
handleEyeClick(); handleEyeClick();
await loadLogs(0, pageSize, logType); await loadLogs(activePage, pageSize, logType);
}; };
const copyText = async (text) => { const copyText = async (text) => {
if (await copy(text)) { if (await copy(text)) {
showSuccess('已复制:' + text); showSuccess('已复制:' + text);
} else { } else {
// setSearchKeyword(text);
Modal.error({ title: '无法复制到剪贴板,请手动复制', content: text }); Modal.error({ title: '无法复制到剪贴板,请手动复制', content: text });
} }
}; };
useEffect(() => { useEffect(() => {
// console.log('default effect')
const localPageSize = const localPageSize =
parseInt(localStorage.getItem('page-size')) || ITEMS_PER_PAGE; parseInt(localStorage.getItem('page-size')) || ITEMS_PER_PAGE;
setPageSize(localPageSize); setPageSize(localPageSize);
loadLogs(0, localPageSize) loadLogs(activePage, localPageSize)
.then() .then()
.catch((reason) => { .catch((reason) => {
showError(reason); showError(reason);
@ -608,25 +588,6 @@ const LogsTable = () => {
handleEyeClick(); handleEyeClick();
}, []); }, []);
const searchLogs = async () => {
if (searchKeyword === '') {
// if keyword is blank, load files instead.
await loadLogs(0, pageSize);
setActivePage(1);
return;
}
setSearching(true);
const res = await API.get(`/api/log/self/search?keyword=${searchKeyword}`);
const { success, message, data } = res.data;
if (success) {
setLogs(data);
setActivePage(1);
} else {
showError(message);
}
setSearching(false);
};
return ( return (
<> <>
<Layout> <Layout>
@ -719,15 +680,13 @@ const LogsTable = () => {
> >
查询 查询
</Button> </Button>
<Form.Section> <Form.Section></Form.Section>
</Form.Section>
</> </>
</Form> </Form>
<Table <Table
style={{ marginTop: 5 }} style={{ marginTop: 5 }}
columns={columns} columns={columns}
dataSource={pageData} dataSource={logs}
pagination={{ pagination={{
currentPage: activePage, currentPage: activePage,
pageSize: pageSize, pageSize: pageSize,
@ -735,7 +694,7 @@ const LogsTable = () => {
pageSizeOpts: [10, 20, 50, 100], pageSizeOpts: [10, 20, 50, 100],
showSizeChanger: true, showSizeChanger: true,
onPageSizeChange: (size) => { onPageSizeChange: (size) => {
handlePageSizeChange(size).then(); handlePageSizeChange(size);
}, },
onPageChange: handlePageChange, onPageChange: handlePageChange,
}} }}