Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature](SqlAudit&SqlManagement&SqlManagementConf): Automatic refresh logic #422

Merged
merged 3 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions packages/sqle/src/page/SqlAudit/List/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,42 @@ describe('sqle/SqlAudit/List', () => {
expect(screen.getByText('更新业务标签成功')).toBeInTheDocument();
expect(sqlAuditRecordsSpy).toHaveBeenCalled();
});

it('render polling request when sql audit status is auditing', async () => {
sqlAuditRecordsSpy.mockImplementation(() =>
createSpySuccessResponse({
data: [
{
...sqlAuditRecordMockData[0],
sql_audit_status: 'auditing'
}
],
total_nums: 2
})
);
renderWithReduxAndTheme(customRender());
await act(async () => jest.advanceTimersByTime(3000));
expect(sqlAuditRecordsSpy).toHaveBeenCalledTimes(1);
await act(async () => jest.advanceTimersByTime(3000));
expect(sqlAuditRecordsSpy).toHaveBeenCalledTimes(2);
});

it('render stop polling request when sql audit status is not auditing', async () => {
sqlAuditRecordsSpy.mockImplementation(() =>
createSpySuccessResponse({
data: [
{
...sqlAuditRecordMockData[0],
sql_audit_status: 'successfully'
}
],
total_nums: 2
})
);
renderWithReduxAndTheme(customRender());
await act(async () => jest.advanceTimersByTime(3000));
expect(sqlAuditRecordsSpy).toHaveBeenCalledTimes(1);
await act(async () => jest.advanceTimersByTime(3000));
expect(sqlAuditRecordsSpy).not.toHaveBeenCalledTimes(2);
LZS911 marked this conversation as resolved.
Show resolved Hide resolved
});
});
38 changes: 32 additions & 6 deletions packages/sqle/src/page/SqlAudit/List/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import SqlAuditListColumn, {
import { getSQLAuditRecordsV1FilterSqlAuditStatusEnum } from '@actiontech/shared/lib/api/sqle/service/sql_audit_record/index.enum';
import { SQLAuditRecordListUrlParamsKey } from '../../SqlManagement/component/SQLEEIndex/index.data';
import { PlusOutlined } from '@actiontech/icons';
import { useBoolean } from 'ahooks';

const SqlAuditList = () => {
const { t } = useTranslation();
Expand All @@ -39,6 +40,9 @@ const SqlAuditList = () => {
const { projectName, projectID, projectArchive } = useCurrentProject();
const { username } = useCurrentUser();

const [polling, { setFalse: finishPollRequest, setTrue: startPollRequest }] =
useBoolean();

const { requestErrorMessage, handleTableRequestError } =
useTableRequestError();
const {
Expand Down Expand Up @@ -71,7 +75,8 @@ const SqlAuditList = () => {
const {
data: dataList,
loading,
refresh
refresh,
cancel
} = useRequest(
() => {
const params: IGetSQLAuditRecordsV1Params = {
Expand All @@ -94,7 +99,23 @@ const SqlAuditList = () => {
pagination,
filterStatus,
filterDataFromUrl
]
],
pollingInterval: 1000,
pollingErrorRetryCount: 3,
onSuccess: (res) => {
if (
res.list?.some(
(i) =>
i.sql_audit_status ===
getSQLAuditRecordsV1FilterSqlAuditStatusEnum.auditing
)
) {
startPollRequest();
} else {
cancel();
finishPollRequest();
}
}
}
);

Expand Down Expand Up @@ -156,6 +177,11 @@ const SqlAuditList = () => {
});
}, [projectName, updateInstanceList]);

const pageLoading = useMemo(
() => (polling ? false : loading),
[polling, loading]
);

return (
<>
{messageContextHolder}
Expand All @@ -180,7 +206,7 @@ const SqlAuditList = () => {
<div className="margin-top-60" />
{/* table */}
<TableToolbar
refreshButton={{ refresh, disabled: loading }}
refreshButton={{ refresh, disabled: pageLoading }}
setting={tableSetting}
filterButton={{
filterButtonMeta,
Expand All @@ -193,7 +219,7 @@ const SqlAuditList = () => {
},
placeholder: t('sqlAudit.list.filter.inputTagPlaceholder')
}}
loading={loading}
loading={pageLoading}
>
<SqlAuditStatusFilter
status={filterStatus}
Expand All @@ -203,7 +229,7 @@ const SqlAuditList = () => {
<TableFilterContainer
filterContainerMeta={filterContainerMeta}
updateTableFilterInfo={updateTableFilterInfo}
disabled={loading}
disabled={pageLoading}
filterCustomProps={filterCustomProps}
/>
<ActiontechTable
Expand All @@ -216,7 +242,7 @@ const SqlAuditList = () => {
total: dataList?.total ?? 0,
current: pagination.page_index
}}
loading={loading}
loading={pageLoading}
columns={columns}
errorMessage={requestErrorMessage}
onChange={tableChange}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -593,4 +593,36 @@ describe('page/SqlManagement/SQLEEIndex', () => {
payload: sqlManageListData.data[0]
});
});

it('render polling request when sql audit status is auditing', async () => {
const request = sqlManage.getSqlManageList();
request.mockImplementation(() =>
createSpySuccessResponse({
data: [
{
...sqlManageListData.data[sqlManageListData.data.length - 1]
}
]
})
);
superRender(<SQLEEIndex />);
await act(async () => jest.advanceTimersByTime(3000));
expect(request).toHaveBeenCalledTimes(1);
await act(async () => jest.advanceTimersByTime(3000));
expect(request).toHaveBeenCalledTimes(2);
});

it('render stop polling request when sql audit status is auditing', async () => {
const request = sqlManage.getSqlManageList();
request.mockImplementation(() =>
createSpySuccessResponse({
data: [sqlManageListData.data[0]]
})
);
superRender(<SQLEEIndex />);
await act(async () => jest.advanceTimersByTime(3000));
expect(request).toHaveBeenCalledTimes(1);
await act(async () => jest.advanceTimersByTime(3000));
expect(request).not.toHaveBeenCalledTimes(2);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import { DownArrowLineOutlined } from '@actiontech/icons';
import useSqlManagementExceptionRedux from '../../../SqlManagementException/hooks/useSqlManagementExceptionRedux';
import useWhitelistRedux from '../../../Whitelist/hooks/useWhitelistRedux';
import { SqlManagementTableStyleWrapper } from './style';
import { SqlManageAuditStatusEnum } from '@actiontech/shared/lib/api/sqle/service/common.enum';

const SQLEEIndex = () => {
const { t } = useTranslation();
Expand All @@ -67,6 +68,9 @@ const SQLEEIndex = () => {

const { preferredEnUS } = usePreferredLanguages();

const [polling, { setFalse: finishPollRequest, setTrue: startPollRequest }] =
useBoolean();

const { setSelectData, setBatchSelectData, updateModalStatus } =
useSqlManagementRedux();

Expand Down Expand Up @@ -121,7 +125,8 @@ const SQLEEIndex = () => {
data: sqlList,
loading: getListLoading,
refresh,
error: getListError
error: getListError,
cancel
} = useRequest(
() => {
const { filter_rule_name, ...otherTableFilterInfo } = tableFilterInfo;
Expand Down Expand Up @@ -155,12 +160,26 @@ const SQLEEIndex = () => {
tableFilterInfo,
sortInfo
],
pollingInterval: 1000,
pollingErrorRetryCount: 3,
onFinally: (params, data) => {
setSQLNum({
SQLTotalNum: data?.otherData?.sql_manage_total_num ?? 0,
problemSQlNum: data?.otherData?.sql_manage_bad_num ?? 0,
optimizedSQLNum: data?.otherData?.sql_manage_optimized_num ?? 0
});

if (
data?.list?.some(
(item) =>
item?.audit_status === SqlManageAuditStatusEnum.being_audited
)
) {
startPollRequest();
} else {
cancel();
finishPollRequest();
}
}
}
);
Expand Down Expand Up @@ -384,8 +403,13 @@ const SQLEEIndex = () => {
: defaultButton;
};

const loading = useMemo(
() => (polling ? false : getListLoading),
[polling, getListLoading]
);

return (
<Spin spinning={getListLoading} delay={300}>
<Spin spinning={loading} delay={300}>
{messageContextHolder}
<PageHeader
title={t('sqlManagement.pageTitle')}
Expand All @@ -404,11 +428,11 @@ const SQLEEIndex = () => {
<SQLStatistics
data={SQLNum}
errorMessage={getListError}
loading={getListLoading}
loading={loading}
/>
{/* table */}
<TableToolbar
refreshButton={{ refresh, disabled: getListLoading }}
refreshButton={{ refresh, disabled: loading }}
setting={tableSetting}
actions={getTableActions()}
filterButton={{
Expand All @@ -427,7 +451,7 @@ const SQLEEIndex = () => {
<TableFilterContainer
filterContainerMeta={filterContainerMeta}
updateTableFilterInfo={updateTableFilterInfo}
disabled={getListLoading}
disabled={loading}
filterCustomProps={filterCustomProps}
/>
<SqlManagementTableStyleWrapper
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { mockProjectInfo } from '@actiontech/shared/lib/testUtil/mockHook/data';
import { mockUseCurrentUser } from '@actiontech/shared/lib/testUtil/mockHook/mockUseCurrentUser';
import { getAllBySelector } from '@actiontech/shared/lib/testUtil/customQuery';
import rule_template from '../../../../../testUtils/mockApi/rule_template';
import { createSpySuccessResponse } from '@actiontech/shared/lib/testUtil/mockApi';
import { mockAuditPlanSQLData } from '../../../../../testUtils/mockApi/instanceAuditPlan/data';

describe('test ScanTypeSqlCollection', () => {
let getInstanceAuditPlanSQLMetaSpy: jest.SpyInstance;
Expand Down Expand Up @@ -130,4 +132,46 @@ describe('test ScanTypeSqlCollection', () => {
await act(async () => jest.advanceTimersByTime(3000));
expect(baseElement).toMatchSnapshot();
});

it('should polling request when sql audit status is being_audited', async () => {
getInstanceAuditPlanSQLDataSpy.mockImplementation(() => {
LZS911 marked this conversation as resolved.
Show resolved Hide resolved
return createSpySuccessResponse({
data: {
rows: [
{
...mockAuditPlanSQLData?.rows?.[0],
audit_results: 'being_audited'
}
]
}
});
});

customRender();
await act(async () => jest.advanceTimersByTime(3000));
expect(getInstanceAuditPlanSQLDataSpy).toHaveBeenCalledTimes(1);
await act(async () => jest.advanceTimersByTime(3000));
expect(getInstanceAuditPlanSQLDataSpy).toHaveBeenCalledTimes(2);
});

it('should stop polling request when sql audit status is not being_audited', async () => {
getInstanceAuditPlanSQLDataSpy.mockImplementation(() => {
return createSpySuccessResponse({
data: {
rows: [
{
...mockAuditPlanSQLData?.rows?.[0],
audit_results: ''
}
]
}
});
});

customRender();
await act(async () => jest.advanceTimersByTime(3000));
expect(getInstanceAuditPlanSQLDataSpy).toHaveBeenCalledTimes(1);
await act(async () => jest.advanceTimersByTime(3000));
expect(getInstanceAuditPlanSQLDataSpy).not.toHaveBeenCalledTimes(2);
});
});
Loading
Loading