Skip to content

Commit

Permalink
Merge pull request #599 from actiontech/feature/sqle-issue-2935
Browse files Browse the repository at this point in the history
[refactor](RuleTemplate): replace temporary solution with proper API integration
  • Loading branch information
Rain-1214 authored Feb 27, 2025
2 parents 82b1451 + f8baa25 commit 4fb48cd
Show file tree
Hide file tree
Showing 40 changed files with 532 additions and 449 deletions.
2 changes: 1 addition & 1 deletion packages/shared/lib/api/base/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ export { default as OAuth2Service } from './service/OAuth2';
export { default as OpPermissionService } from './service/OpPermission';
export { default as ProjectService } from './service/Project';
export { default as RoleService } from './service/Role';
export { default as SMSService } from './service/SMS';
export { default as SessionService } from './service/Session';
export { default as UserService } from './service/User';
export { default as UserGroupService } from './service/UserGroup';
export { default as WebhookService } from './service/Webhook';
export { default as SMSService } from './service/SMS';
28 changes: 21 additions & 7 deletions packages/shared/lib/api/sqle/service/common.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ export interface ICreateProjectRuleTemplateReqV1 {

rule_template_name?: string;

rule_version?: string;
rule_version?: number;
}

export interface ICreateRollbackWorkflowReq {
Expand Down Expand Up @@ -772,7 +772,7 @@ export interface ICreateRuleTemplateReqV1 {

rule_template_name?: string;

rule_version?: string;
rule_version?: number;
}

export interface ICreateSQLAuditRecordResV1 {
Expand Down Expand Up @@ -1391,6 +1391,20 @@ export interface IGetDingTalkConfigurationResV1 {
message?: string;
}

export interface IGetDriverRuleVersionTipsResV1 {
code?: number;

data?: IGetDriverRuleVersionTipsV1[];

message?: string;
}

export interface IGetDriverRuleVersionTipsV1 {
db_type?: string;

rule_versions?: number[];
}

export interface IGetDriversResV1 {
code?: number;

Expand Down Expand Up @@ -2560,7 +2574,7 @@ export interface IParseProjectRuleTemplateFileResDataV1 {

rule_list?: IRuleResV1[];

rule_version?: string;
rule_version?: number;
}

export interface IParseProjectRuleTemplateFileResV1 {
Expand Down Expand Up @@ -2756,7 +2770,7 @@ export interface IRuleProjectTemplateDetailResV1 {

rule_template_name?: string;

rule_version?: string;
rule_version?: number;
}

export interface IRuleReqV1 {
Expand Down Expand Up @@ -2810,7 +2824,7 @@ export interface IRuleTemplateDetailResV1 {

rule_template_name?: string;

rule_version?: string;
rule_version?: number;
}

export interface IRuleTemplateResV1 {
Expand All @@ -2820,7 +2834,7 @@ export interface IRuleTemplateResV1 {

rule_template_name?: string;

rule_version?: string;
rule_version?: number;
}

export interface IRuleTemplateTipResV1 {
Expand All @@ -2832,7 +2846,7 @@ export interface IRuleTemplateTipResV1 {

rule_template_name?: string;

rule_version?: string;
rule_version?: number;
}

export interface IRuleTips {
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/lib/api/sqle/service/instance/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
import { getInstanceTipListV1FunctionalModuleEnum } from './index.enum';

export interface IGetDatabaseDriverLogosParams {
db_types: string;
db_types: string[];
}

export interface IGetDatabaseDriverLogosReturn
Expand Down
8 changes: 6 additions & 2 deletions packages/shared/lib/api/sqle/service/rule_template/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import {
IUpdateRuleTemplateReqV1,
ICloneRuleTemplateReqV1,
IGetRulesResV1,
IGetRuleCategoryStatisticResV1
IGetRuleCategoryStatisticResV1,
IGetDriverRuleVersionTipsResV1
} from '../common.d';

import {
Expand Down Expand Up @@ -252,7 +253,7 @@ export interface IGetRuleListV1Params {

filter_rule_names?: string;

filter_rule_version?: string;
filter_rule_version?: number;

tags?: string;
}
Expand All @@ -261,3 +262,6 @@ export interface IGetRuleListV1Return extends IGetRulesResV1 {}

export interface IGetCategoryStatisticsReturn
extends IGetRuleCategoryStatisticResV1 {}

export interface IGetDriverRuleVersionTipsReturn
extends IGetDriverRuleVersionTipsResV1 {}
11 changes: 10 additions & 1 deletion packages/shared/lib/api/sqle/service/rule_template/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ import {
IExportRuleTemplateV1Params,
IGetRuleListV1Params,
IGetRuleListV1Return,
IGetCategoryStatisticsReturn
IGetCategoryStatisticsReturn,
IGetDriverRuleVersionTipsReturn
} from './index.d';

class RuleTemplateService extends ServiceBase {
Expand Down Expand Up @@ -520,6 +521,14 @@ class RuleTemplateService extends ServiceBase {
options
);
}

public GetDriverRuleVersionTips(options?: AxiosRequestConfig) {
return this.get<IGetDriverRuleVersionTipsReturn>(
'/v1/rules_version_tips',
undefined,
options
);
}
}

export default new RuleTemplateService();
21 changes: 14 additions & 7 deletions packages/sqle/src/components/RuleDetail/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ describe('sqle/components/RuleDetail', () => {
await act(async () => jest.advanceTimersByTime(3000));
expect(requestGetAllRule).toHaveBeenCalledTimes(1);
expect(requestGetAllRule).toHaveBeenNthCalledWith(1, {
filter_db_type: mockRouteParams.dbType
filter_db_type: mockRouteParams.dbType,
filter_rule_version: 2
});
});

Expand Down Expand Up @@ -149,7 +150,8 @@ describe('sqle/components/RuleDetail', () => {
await act(async () => jest.advanceTimersByTime(3000));
expect(requestGetAllRule).toHaveBeenNthCalledWith(2, {
fuzzy_keyword_rule: 'text1',
filter_db_type: mockRouteParams.dbType
filter_db_type: mockRouteParams.dbType,
filter_rule_version: 2
});
});

Expand All @@ -176,7 +178,8 @@ describe('sqle/components/RuleDetail', () => {
await act(async () => jest.advanceTimersByTime(3000));
expect(requestGetAllRule).toHaveBeenNthCalledWith(2, {
tags: 'column',
filter_db_type: mockRouteParams.dbType
filter_db_type: mockRouteParams.dbType,
filter_rule_version: 2
});

fireEvent.mouseDown(getBySelector('#audit_purpose'));
Expand All @@ -192,7 +195,8 @@ describe('sqle/components/RuleDetail', () => {
await act(async () => jest.advanceTimersByTime(3000));
expect(requestGetAllRule).toHaveBeenNthCalledWith(3, {
tags: 'column,correction',
filter_db_type: mockRouteParams.dbType
filter_db_type: mockRouteParams.dbType,
filter_rule_version: 2
});

fireEvent.mouseDown(getBySelector('#sql'));
Expand All @@ -208,7 +212,8 @@ describe('sqle/components/RuleDetail', () => {
await act(async () => jest.advanceTimersByTime(3000));
expect(requestGetAllRule).toHaveBeenNthCalledWith(4, {
tags: 'column,correction,dcl',
filter_db_type: mockRouteParams.dbType
filter_db_type: mockRouteParams.dbType,
filter_rule_version: 2
});
fireEvent.mouseDown(getBySelector('#audit_accuracy'));
await act(async () => jest.advanceTimersByTime(0));
Expand All @@ -222,7 +227,8 @@ describe('sqle/components/RuleDetail', () => {
await act(async () => jest.advanceTimersByTime(3000));
expect(requestGetAllRule).toHaveBeenNthCalledWith(5, {
tags: 'column,correction,offline,dcl',
filter_db_type: mockRouteParams.dbType
filter_db_type: mockRouteParams.dbType,
filter_rule_version: 2
});

fireEvent.mouseDown(getBySelector('#performance_cost'));
Expand All @@ -236,7 +242,8 @@ describe('sqle/components/RuleDetail', () => {
await act(async () => jest.advanceTimersByTime(3000));
expect(requestGetAllRule).toHaveBeenNthCalledWith(6, {
tags: 'column,correction,offline,dcl,high',
filter_db_type: mockRouteParams.dbType
filter_db_type: mockRouteParams.dbType,
filter_rule_version: 2
});
});

Expand Down
16 changes: 4 additions & 12 deletions packages/sqle/src/components/RuleDetail/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,13 @@ import {
import useThemeStyleData from '../../hooks/useThemeStyleData';
import { ROUTE_PATHS } from '@actiontech/shared/lib/data/routePaths';
import { RuleFilter, useRuleFilterForm } from '../RuleList';
import useRuleVersionTips, {
RuleVersionDictionaryEnum
} from '../../hooks/useRuleVersionTips';

const RuleDetail = () => {
const { t } = useTranslation();
const { sqleTheme } = useThemeStyleData();
const { goBack } = useBack();

const { form, fuzzyKeyword, tags } = useRuleFilterForm();
const { transformRuleVersion2BackendParams } = useRuleVersionTips();

const { templateName, dbType } =
useTypedParams<typeof ROUTE_PATHS.SQLE.RULE_TEMPLATE.detail>();
Expand All @@ -58,9 +54,7 @@ const RuleDetail = () => {
tags
})
.then((res) => {
getAllRulesAction(
res.data.data?.rule_version as RuleVersionDictionaryEnum
);
getAllRulesAction(res.data.data?.rule_version);
return res.data?.data?.rule_list ?? [];
}),
{
Expand All @@ -82,9 +76,7 @@ const RuleDetail = () => {
tags
})
.then((res) => {
getAllRulesAction(
res.data.data?.rule_version as RuleVersionDictionaryEnum
);
getAllRulesAction(res.data.data?.rule_version);
return res.data?.data?.rule_list ?? [];
}),
{
Expand All @@ -99,13 +91,13 @@ const RuleDetail = () => {
error: allRulesError,
run: getAllRulesAction
} = useRequest(
(ruleVersion: RuleVersionDictionaryEnum) =>
(ruleVersion?: number) =>
rule_template
.getRuleListV1({
filter_db_type: dbType,
fuzzy_keyword_rule: fuzzyKeyword,
tags,
filter_rule_version: transformRuleVersion2BackendParams(ruleVersion)
filter_rule_version: ruleVersion
})
.then((res) => res.data?.data ?? []),
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { ruleListData } from '../../../testUtils/mockApi/rule_template/data';
import { Form } from 'antd';
import { RuleFilterFieldsType } from '../../../components/RuleList';
import { NamePath } from 'antd/es/form/interface';
import { RuleVersionDictionaryEnum } from '../../useRuleVersionTips';

describe('sqle/hooks/useRuleTemplateForm/useRules', () => {
let getRulesSpy: jest.SpyInstance;
Expand Down Expand Up @@ -60,10 +59,7 @@ describe('sqle/hooks/useRuleTemplateForm/useRules', () => {
return mockFormValue[key as keyof RuleFilterFieldsType];
});

const { result } = renderHooksWithRedux(
() => useRules('MySQL', RuleVersionDictionaryEnum.v2),
{}
);
const { result } = renderHooksWithRedux(() => useRules('MySQL', 2), {});
expect(result.current.getAllRulesLoading).toEqual(true);
await act(async () => jest.advanceTimersByTime(3000));
expect(result.current.allRules).toEqual(ruleListData);
Expand All @@ -78,7 +74,7 @@ describe('sqle/hooks/useRuleTemplateForm/useRules', () => {
expect(getRulesSpy).toHaveBeenNthCalledWith(1, {
filter_db_type: 'MySQL',
fuzzy_keyword_rule: 'keyword',
filter_rule_version: 'v2',
filter_rule_version: 2,
tags: 'table,security,offline,dcl,high'
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
isExportFileResponse,
jsonParse
} from '@actiontech/shared/lib/utils/Common';
import useRuleVersionTips from '../useRuleVersionTips';

const useImportRuleTemplate = () => {
const { t } = useTranslation();
Expand Down Expand Up @@ -63,7 +62,6 @@ const useImportRuleTemplate = () => {
ruleFilterForm,
filterCategoryTags
} = useRules(dbType, ruleVersion);
const { transformBackendRuleVersion2FormValues } = useRuleVersionTips();

const importServicesCheck = useCallback(
(
Expand Down Expand Up @@ -142,18 +140,10 @@ const useImportRuleTemplate = () => {
templateDesc: importFileData.current.desc,
templateName: importFileData.current.name,
db_type: importFileData.current.db_type,
ruleVersion: transformBackendRuleVersion2FormValues(
importFileData.current.db_type!,
importFileData.current.rule_version
)
ruleVersion: importFileData.current.rule_version
});
}
}, [
ruleTemplateForm,
selectFileForm,
showRuleTemplateForm,
transformBackendRuleVersion2FormValues
]);
}, [ruleTemplateForm, selectFileForm, showRuleTemplateForm]);

const removeUploadFile = useCallback(() => {
setUploadCheckStatus({
Expand Down
9 changes: 2 additions & 7 deletions packages/sqle/src/hooks/useRuleTemplateForm/useRules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,22 @@ import { IRuleResV1 } from '@actiontech/shared/lib/api/sqle/service/common';
import { useState, useCallback } from 'react';
import { useRuleFilterForm } from '../../components/RuleList';
import { cloneDeep } from 'lodash';
import useRuleVersionTips, {
RuleVersionDictionaryEnum
} from '../useRuleVersionTips';

const useRules = (dbType: string, ruleVersion?: RuleVersionDictionaryEnum) => {
const useRules = (dbType: string, ruleVersion?: number) => {
const [activeRule, setActiveRule] = useState<IRuleResV1[]>([]);

const [filteredRule, setFilteredRule] = useState<IRuleResV1[]>([]);

const { form: ruleFilterForm, fuzzyKeyword, tags } = useRuleFilterForm();

const { transformRuleVersion2BackendParams } = useRuleVersionTips();

const { data: allRules, loading: getAllRulesLoading } = useRequest(
() =>
rule_template
.getRuleListV1({
filter_db_type: dbType,
fuzzy_keyword_rule: fuzzyKeyword,
tags,
filter_rule_version: transformRuleVersion2BackendParams(ruleVersion)
filter_rule_version: ruleVersion
})
.then((res) => res.data.data ?? []),
{
Expand Down
Loading

0 comments on commit 4fb48cd

Please sign in to comment.