Skip to content

Commit 0018c48

Browse files
committed
fix:添加扫描取消功能及状态处理
1 parent 8029d91 commit 0018c48

2 files changed

Lines changed: 134 additions & 13 deletions

File tree

src/common/network/sensitiveColumn.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ export enum ScannResultType {
9696
RUNNING = 'RUNNING',
9797
SUCCESS = 'SUCCESS',
9898
FAILED = 'FAILED',
99+
CANCELLED = 'CANCELLED',
99100
}
100101
export interface IScannResult {
101102
taskId: string;
@@ -111,9 +112,20 @@ export interface IScannResult {
111112

112113
export async function getScanningResults(projectId: number, taskId: string): Promise<IScannResult> {
113114
const ret = await request.get(
114-
`/api/v2/collaboration/projects/${projectId}/sensitiveColumns/getScanningResults?taskId=${encodeURIComponent(
115-
taskId,
116-
)}`,
115+
`/api/v2/collaboration/projects/${projectId}/sensitiveColumns/getScanningResults`,
116+
{
117+
params: { taskId },
118+
},
119+
);
120+
return ret?.data;
121+
}
122+
123+
export async function stopScanning(projectId: number, taskId: string): Promise<boolean> {
124+
const ret = await request.post(
125+
`/api/v2/collaboration/projects/${projectId}/sensitiveColumns/stopScanning`,
126+
{
127+
params: { taskId },
128+
},
117129
);
118130
return ret?.data;
119131
}

src/page/Project/Sensitive/components/SensitiveColumn/components/ScanForm.tsx

Lines changed: 119 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,21 @@ import {
1818
getScanningResults,
1919
ScannResultType,
2020
startScanning,
21+
stopScanning,
2122
} from '@/common/network/sensitiveColumn';
2223
import { ESensitiveColumnType, ISensitiveColumn } from '@/d.ts/sensitiveColumn';
2324
import ProjectContext from '@/page/Project/ProjectContext';
2425
import { maskRuleTypeMap } from '@/page/Secure/MaskingAlgorithm';
2526
import { ReactComponent as TableOutlined } from '@/svgr/menuTable.svg';
2627
import { ReactComponent as ViewSvg } from '@/svgr/menuView.svg';
2728
import { formatMessage } from '@/util/intl';
28-
import Icon, { CheckCircleFilled, DeleteOutlined, SyncOutlined } from '@ant-design/icons';
29+
import Icon, {
30+
CheckCircleFilled,
31+
CloseCircleFilled,
32+
DeleteOutlined,
33+
StopOutlined,
34+
SyncOutlined,
35+
} from '@ant-design/icons';
2936
import {
3037
Button,
3138
Collapse,
@@ -80,6 +87,7 @@ const ScanForm = (props: IScanFormProps, ref) => {
8087
const [activeKeys, setActiveKeys] = useState<string | string[]>(['0']);
8188
const [sensitiveColumnMap, setSensitiveColumnMap] = useState<Map<string, any>>(new Map());
8289
const reset = () => {
90+
clearTimeout(timer.current);
8391
setTaskId(null);
8492
setScanStatus(null);
8593
setScanLoading(false);
@@ -142,10 +150,26 @@ const ScanForm = (props: IScanFormProps, ref) => {
142150
setScanStatus(ScannResultType.CREATED);
143151
}
144152
};
153+
154+
const handleStopScan = async () => {
155+
if (taskId) {
156+
try {
157+
await stopScanning(projectContext.projectId, taskId);
158+
clearTimeout(timer.current);
159+
setScanLoading(false);
160+
setScanStatus(ScannResultType.CANCELLED);
161+
} catch (error) {
162+
console.error('停止扫描失败:', error);
163+
}
164+
}
165+
};
145166
const handleScanning = async (taskId: string) => {
146167
const rawData = await getScanningResults(projectContext.projectId, taskId);
147168
const { status, sensitiveColumns, allTableCount, finishedTableCount } = rawData;
148-
if ([ScannResultType.FAILED, ScannResultType.SUCCESS].includes(status)) {
169+
if (
170+
[ScannResultType.FAILED, ScannResultType.SUCCESS, ScannResultType.CANCELLED].includes(status)
171+
) {
172+
clearTimeout(timer.current);
149173
const dataSourceMap = new Map();
150174
setSensitiveColumns(sensitiveColumns);
151175
sensitiveColumns?.forEach((d) => {
@@ -218,8 +242,14 @@ const ScanForm = (props: IScanFormProps, ref) => {
218242
});
219243
});
220244
setFormData(scanTableData);
221-
setSuccessful(true);
222-
setScanStatus(ScannResultType.SUCCESS);
245+
246+
// 根据实际扫描状态设置UI状态
247+
if (status === ScannResultType.SUCCESS) {
248+
setSuccessful(true);
249+
} else {
250+
setSuccessful(false);
251+
}
252+
setScanStatus(status);
223253
setPercent(Math.floor((finishedTableCount * 100) / allTableCount));
224254
await _formRef.setFieldsValue({
225255
scanTableData,
@@ -234,7 +264,6 @@ const ScanForm = (props: IScanFormProps, ref) => {
234264
setPercent(Math.floor((finishedTableCount * 100) / allTableCount));
235265
timer.current = setTimeout(() => {
236266
handleScanning(taskId);
237-
clearTimeout(timer.current);
238267
}, 500);
239268
}
240269
};
@@ -326,7 +355,7 @@ const ScanForm = (props: IScanFormProps, ref) => {
326355
};
327356
});
328357
useEffect(() => {
329-
if (taskId && [ScannResultType.CREATED, ScannResultType.RUNNING].includes(scanStatus)) {
358+
if (taskId && scanStatus === ScannResultType.CREATED) {
330359
handleScanning(taskId);
331360
}
332361
return () => {
@@ -355,7 +384,9 @@ const ScanForm = (props: IScanFormProps, ref) => {
355384
<ScanButton
356385
scanLoading={scanLoading}
357386
successful={successful}
387+
scanStatus={scanStatus}
358388
handleStartScan={handleStartScan}
389+
handleStopScan={handleStopScan}
359390
/>
360391
</Form>
361392
<PreviewHeader
@@ -386,6 +417,7 @@ const ScanForm = (props: IScanFormProps, ref) => {
386417
hasScan={hasScan}
387418
scanLoading={scanLoading}
388419
successful={successful}
420+
scanStatus={scanStatus}
389421
empty={true}
390422
/>
391423
) : (
@@ -410,7 +442,16 @@ const EmptyOrSpin: React.FC<{
410442
percent: number;
411443
successful: boolean;
412444
scanLoading: boolean;
413-
}> = ({ empty = false, isSearch = false, scanLoading, hasScan, percent, successful }) => {
445+
scanStatus?: ScannResultType;
446+
}> = ({
447+
empty = false,
448+
isSearch = false,
449+
scanLoading,
450+
hasScan,
451+
percent,
452+
successful,
453+
scanStatus,
454+
}) => {
414455
const gentDescription = () => {
415456
if (hasScan && isSearch && isSearch) {
416457
return formatMessage({
@@ -425,6 +466,12 @@ const EmptyOrSpin: React.FC<{
425466
}); //'选中数据库目前暂无可选敏感列'
426467
}
427468
if (hasScan && !successful) {
469+
if (scanStatus === ScannResultType.CANCELLED) {
470+
return formatMessage({
471+
id: 'odc.src.page.Project.Sensitive.components.SensitiveColumn.components.ScanCancelled',
472+
defaultMessage: '扫描已取消',
473+
}); //'扫描已取消'
474+
}
428475
return formatMessage({
429476
id: 'odc.src.page.Project.Sensitive.components.SensitiveColumn.components.ScanFailure',
430477
defaultMessage: '扫描失败',
@@ -475,8 +522,10 @@ const EmptyOrSpin: React.FC<{
475522
const ScanButton: React.FC<{
476523
scanLoading: boolean;
477524
successful: boolean;
525+
scanStatus: ScannResultType;
478526
handleStartScan: () => void;
479-
}> = ({ scanLoading, successful, handleStartScan }) => {
527+
handleStopScan: () => void;
528+
}> = ({ scanLoading, successful, scanStatus, handleStartScan, handleStopScan }) => {
480529
return (
481530
<Space>
482531
<Button
@@ -496,6 +545,14 @@ const ScanButton: React.FC<{
496545
}) //开始扫描
497546
}
498547
</Button>
548+
{scanLoading && (
549+
<Button onClick={handleStopScan} danger>
550+
{formatMessage({
551+
id: 'odc.SensitiveColumn.components.ScanForm.StopScanning',
552+
defaultMessage: '停止扫描',
553+
})}
554+
</Button>
555+
)}
499556
{successful && (
500557
<Space>
501558
<CheckCircleFilled
@@ -514,6 +571,42 @@ const ScanButton: React.FC<{
514571
</div>
515572
</Space>
516573
)}
574+
{scanStatus === ScannResultType.FAILED && (
575+
<Space>
576+
<CloseCircleFilled
577+
style={{
578+
color: '#ff4d4f',
579+
}}
580+
/>
581+
582+
<div>
583+
{
584+
formatMessage({
585+
id: 'odc.SensitiveColumn.components.ScanForm.ScanFailed',
586+
defaultMessage: '扫描失败',
587+
}) /*扫描失败*/
588+
}
589+
</div>
590+
</Space>
591+
)}
592+
{scanStatus === ScannResultType.CANCELLED && (
593+
<Space>
594+
<StopOutlined
595+
style={{
596+
color: '#faad14',
597+
}}
598+
/>
599+
600+
<div>
601+
{
602+
formatMessage({
603+
id: 'odc.SensitiveColumn.components.ScanForm.ScanCancelled',
604+
defaultMessage: '扫描已取消',
605+
}) /*扫描已取消*/
606+
}
607+
</div>
608+
</Space>
609+
)}
517610
</Space>
518611
);
519612
};
@@ -811,7 +904,16 @@ const EmptyCollapse: React.FC<{
811904
hasScan?: boolean;
812905
scanLoading?: boolean;
813906
successful?: boolean;
814-
}> = ({ empty = false, isSearch = false, percent, hasScan, scanLoading, successful }) => {
907+
scanStatus?: ScannResultType;
908+
}> = ({
909+
empty = false,
910+
isSearch = false,
911+
percent,
912+
hasScan,
913+
scanLoading,
914+
successful,
915+
scanStatus,
916+
}) => {
815917
return (
816918
<Collapse defaultActiveKey={['0']} className={styles.collapse}>
817919
<Collapse.Panel
@@ -848,6 +950,7 @@ const EmptyCollapse: React.FC<{
848950
hasScan={hasScan}
849951
scanLoading={scanLoading}
850952
successful={successful}
953+
scanStatus={scanStatus}
851954
/>
852955
</Collapse.Panel>
853956
</Collapse>
@@ -874,7 +977,13 @@ const CollapseItemContent: React.FC<{
874977
handleScanTableDataDeleteByTableName,
875978
}) => {
876979
return scanTableData?.length === 0 ? (
877-
<EmptyCollapse empty={true} hasScan={true} successful={true} isSearch={true} />
980+
<EmptyCollapse
981+
empty={true}
982+
hasScan={true}
983+
successful={true}
984+
isSearch={true}
985+
scanStatus={ScannResultType.SUCCESS}
986+
/>
878987
) : (
879988
<Collapse
880989
defaultActiveKey={activeKeys}

0 commit comments

Comments
 (0)