Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

fix: sql review rule update (#12644)

ecmadaoe5f8d7c8adaa
diff --git a/frontend/src/components/InstanceForm/DataSourceSection/DataSourceForm.vue b/frontend/src/components/InstanceForm/DataSourceSection/DataSourceForm.vue
index 6d1c0eb127..9d5e265661 100644
--- a/frontend/src/components/InstanceForm/DataSourceSection/DataSourceForm.vue
+++ b/frontend/src/components/InstanceForm/DataSourceSection/DataSourceForm.vue
@@ -1085,7 +1085,6 @@ const handleKeytabUpload = (options: { file: UploadFileInfo }) => {
const data = new Uint8Array(arrayBuffer);
const ds = props.dataSource;
if (ds.saslConfig && ds.saslConfig.krbConfig) {
- console.log(data);
ds.saslConfig.krbConfig.keytab = data;
}
};
diff --git a/frontend/src/components/IssueV1/components/PlanCheckSection/PlanCheckBar/PlanCheckDetail.vue b/frontend/src/components/IssueV1/components/PlanCheckSection/PlanCheckBar/PlanCheckDetail.vue
index a7b741792b..cc530578d7 100644
--- a/frontend/src/components/IssueV1/components/PlanCheckSection/PlanCheckBar/PlanCheckDetail.vue
+++ b/frontend/src/components/IssueV1/components/PlanCheckSection/PlanCheckBar/PlanCheckDetail.vue
@@ -87,7 +87,7 @@
)
"
>
- L{{ row.checkResult.sqlReviewReport.line }}
+ Line {{ row.checkResult.sqlReviewReport.line }}
</span>
</template>
@@ -205,11 +205,19 @@ const checkResultList = computed((): PlanCheckRun_Result[] => {
});
const getRuleTemplateByType = (type: string) => {
- const mapByEngine = ruleTemplateMapV2.get(type);
- if (!mapByEngine) {
- return;
+ if (props.database) {
+ return ruleTemplateMapV2
+ .get(props.database.instanceEntity.engine)
+ ?.get(type);
}
- return [...mapByEngine.values()][0];
+
+ // fallback
+ for (const mapByType of ruleTemplateMapV2.values()) {
+ if (mapByType.has(type)) {
+ return mapByType.get(type);
+ }
+ }
+ return;
};
const categoryAndTitle = (
diff --git a/frontend/src/components/IssueV1/components/PlanCheckSection/PlanCheckBar/PlanCheckModal.vue b/frontend/src/components/IssueV1/components/PlanCheckSection/PlanCheckBar/PlanCheckModal.vue
index 94ffd69340..2be2c4a21b 100644
--- a/frontend/src/components/IssueV1/components/PlanCheckSection/PlanCheckBar/PlanCheckModal.vue
+++ b/frontend/src/components/IssueV1/components/PlanCheckSection/PlanCheckBar/PlanCheckModal.vue
@@ -8,6 +8,7 @@
<PlanCheckPanel
:plan-check-run-list="planCheckRunList"
:selected-type="selectedType"
+ @close="$emit('close')"
/>
</BBModal>
</template>
diff --git a/frontend/src/components/Plan/components/PlanCheckSection/PlanCheckBar/PlanCheckDetail.vue b/frontend/src/components/Plan/components/PlanCheckSection/PlanCheckBar/PlanCheckDetail.vue
index f308332a78..e4c126cd97 100644
--- a/frontend/src/components/Plan/components/PlanCheckSection/PlanCheckBar/PlanCheckDetail.vue
+++ b/frontend/src/components/Plan/components/PlanCheckSection/PlanCheckBar/PlanCheckDetail.vue
@@ -87,7 +87,7 @@
)
"
>
- L{{ row.checkResult.sqlReviewReport.line }}
+ Line {{ row.checkResult.sqlReviewReport.line }}
</span>
</template>
@@ -205,11 +205,19 @@ const checkResultList = computed((): PlanCheckRun_Result[] => {
});
const getRuleTemplateByType = (type: string) => {
- const mapByEngine = ruleTemplateMapV2.get(type);
- if (!mapByEngine) {
- return;
+ if (props.database) {
+ return ruleTemplateMapV2
+ .get(props.database.instanceEntity.engine)
+ ?.get(type);
}
- return [...mapByEngine.values()][0];
+
+ // fallback
+ for (const mapByType of ruleTemplateMapV2.values()) {
+ if (mapByType.has(type)) {
+ return mapByType.get(type);
+ }
+ }
+ return;
};
const categoryAndTitle = (
diff --git a/frontend/src/components/SQLCheck/SQLCheckPanel.vue b/frontend/src/components/SQLCheck/SQLCheckPanel.vue
index 05fa86620b..a9c19800af 100644
--- a/frontend/src/components/SQLCheck/SQLCheckPanel.vue
+++ b/frontend/src/components/SQLCheck/SQLCheckPanel.vue
@@ -11,7 +11,7 @@
<PlanCheckDetail
:plan-check-run="planCheckRun"
:database="database"
- :is-latest="true"
+ :is-latest="false"
@close="$emit('close')"
>
<template #row-extra="{ row }">
diff --git a/frontend/src/components/SQLReview/SQLReviewConfig.vue b/frontend/src/components/SQLReview/SQLReviewConfig.vue
index 07308058f4..c48216a713 100644
--- a/frontend/src/components/SQLReview/SQLReviewConfig.vue
+++ b/frontend/src/components/SQLReview/SQLReviewConfig.vue
@@ -1,5 +1,5 @@
<template>
- <SQLReviewTabsByEngine :rule-list="selectedRuleList">
+ <SQLReviewTabsByEngine :rule-map-by-engine="ruleMapByEngine">
<template
#default="{
ruleList: ruleListFilteredByEngine,
@@ -24,11 +24,10 @@ import type { Engine } from "@/types/proto/v1/common";
import type { RuleTemplateV2 } from "@/types/sqlReview";
defineProps<{
- selectedRuleList: RuleTemplateV2[];
+ ruleMapByEngine: Map<Engine, Map<string, RuleTemplateV2>>;
}>();
const emit = defineEmits<{
- (event: "apply-template", index: number): void;
(
event: "rule-change",
rule: RuleTemplateV2,
diff --git a/frontend/src/components/SQLReview/SQLReviewCreation.vue b/frontend/src/components/SQLReview/SQLReviewCreation.vue
index caecf31c94..9d22245623 100644
--- a/frontend/src/components/SQLReview/SQLReviewCreation.vue
+++ b/frontend/src/components/SQLReview/SQLReviewCreation.vue
@@ -34,7 +34,7 @@
</template>
<template #1>
<SQLReviewConfig
- :selected-rule-list="state.selectedRuleList"
+ :rule-map-by-engine="state.selectedRuleMapByEngine"
@rule-change="change"
/>
</template>
@@ -63,12 +63,13 @@ import type {
RuleTemplateV2,
SQLReviewPolicyTemplateV2,
SQLReviewPolicy,
- SchemaPolicyRule,
} from "@/types";
import {
- convertRuleTemplateToPolicyRule,
+ getRuleMapByEngine,
+ convertRuleMapToPolicyRuleList,
ruleIsAvailableInSubscription,
} from "@/types";
+import { Engine } from "@/types/proto/v1/common";
import { SQLReviewRuleLevel } from "@/types/proto/v1/org_policy_service";
import { hasWorkspacePermissionV2 } from "@/utils";
import SQLReviewConfig from "./SQLReviewConfig.vue";
@@ -80,7 +81,7 @@ interface LocalState {
name: string;
resourceId: string;
attachedResources: string[];
- selectedRuleList: RuleTemplateV2[];
+ selectedRuleMapByEngine: Map<Engine, Map<string, RuleTemplateV2>>;
selectedTemplate: SQLReviewPolicyTemplateV2 | undefined;
ruleUpdated: boolean;
pendingApplyTemplate: SQLReviewPolicyTemplateV2 | undefined;
@@ -123,7 +124,7 @@ const state = reactive<LocalState>({
name: props.name || t("sql-review.create.basic-info.display-name-default"),
resourceId: props.policy ? getReviewConfigId(props.policy.id) : "",
attachedResources: props.selectedResources,
- selectedRuleList: [...props.selectedRuleList],
+ selectedRuleMapByEngine: getRuleMapByEngine(props.selectedRuleList),
selectedTemplate: props.policy
? rulesToTemplate(props.policy, false)
: undefined,
@@ -147,15 +148,17 @@ const onTemplateApply = (template: SQLReviewPolicyTemplateV2 | undefined) => {
state.selectedTemplate = template;
state.pendingApplyTemplate = undefined;
- state.selectedRuleList = template.ruleList.map((rule) => ({
- ...rule,
- level: ruleIsAvailableInSubscription(
- rule.type,
- subscriptionStore.currentPlan
- )
- ? rule.level
- : SQLReviewRuleLevel.DISABLED,
- }));
+ state.selectedRuleMapByEngine = getRuleMapByEngine(
+ template.ruleList.map((rule) => ({
+ ...rule,
+ level: ruleIsAvailableInSubscription(
+ rule.type,
+ subscriptionStore.currentPlan
+ )
+ ? rule.level
+ : SQLReviewRuleLevel.DISABLED,
+ }))
+ );
};
const onCancel = () => {
@@ -175,10 +178,10 @@ const allowNext = computed((): boolean => {
!!state.name &&
!!state.resourceId &&
state.attachedResources.length > 0 &&
- state.selectedRuleList.length > 0
+ state.selectedRuleMapByEngine.size > 0
);
case CONFIGURE_RULE_STEP:
- return state.selectedRuleList.length > 0;
+ return state.selectedRuleMapByEngine.size > 0;
case PREVIEW_STEP:
return true;
}
@@ -214,14 +217,9 @@ const tryFinishSetup = async () => {
});
}
- const ruleList: SchemaPolicyRule[] = [];
- for (const rule of state.selectedRuleList) {
- ruleList.push(convertRuleTemplateToPolicyRule(rule));
- }
-
const upsert = {
title: state.name,
- ruleList,
+ ruleList: convertRuleMapToPolicyRuleList(state.selectedRuleMapByEngine),
resources: state.attachedResources,
};
@@ -281,17 +279,16 @@ const change = (rule: RuleTemplateV2, overrides: Partial<RuleTemplateV2>) => {
return;
}
- const index = state.selectedRuleList.findIndex(
- (r) => r.type === rule.type && r.engine === rule.engine
- );
- if (index < 0) {
+ const selectedRule = state.selectedRuleMapByEngine
+ .get(rule.engine)
+ ?.get(rule.type);
+ if (!selectedRule) {
return;
}
- const newRule = {
- ...state.selectedRuleList[index],
- ...overrides,
- };
- state.selectedRuleList[index] = newRule;
+ state.selectedRuleMapByEngine
+ .get(rule.engine)
+ ?.set(rule.type, Object.assign(selectedRule, overrides));
+
state.ruleUpdated = true;
};
</script>
diff --git a/frontend/src/components/SQLReview/components/RuleEngineIcon.vue b/frontend/src/components/SQLReview/components/RuleEngineIcon.vue
deleted file mode 100644
index 6fc9d48f22..0000000000
--- a/frontend/src/components/SQLReview/components/RuleEngineIcon.vue
+++ /dev/null
@@ -1,17 +0,0 @@
-<template>
- <NTooltip>
- <template #trigger>
- <EngineIcon :engine="engine" />
- </template>
- <RichEngineName :engine="engine" class="text-white text-xs" />
- </NTooltip>
-</template>
-
-<script lang="ts" setup>
-import RichEngineName from "@/components/v2/Model/Instance/RichEngineName.vue";
-import type { Engine } from "@/types/proto/v1/common";
-
-defineProps<{
- engine: Engine;
-}>();
-</script>
diff --git a/frontend/src/components/SQLReview/components/RuleEngineTabFilter.vue b/frontend/src/components/SQLReview/components/RuleEngineTabFilter.vue
deleted file mode 100644
index 2307998984..0000000000
--- a/frontend/src/components/SQLReview/components/RuleEngineTabFilter.vue
+++ /dev/null
@@ -1,52 +0,0 @@
-<template>
- <TabFilter
- :value="selected"
- :items="items"
- @update:value="$emit('update:engine', $event as Engine)"
- >
- <template #label="{ item }">
- <div class="flex items-center justify-center gap-x-1">
- <RuleEngineIcon
- v-if="item.value !== Engine.ENGINE_UNSPECIFIED"
- :engine="engineFromJSON(item.value)"
- />
- {{ item.label }}
- </div>
- </template>
- </TabFilter>
-</template>
-
-<script lang="ts" setup>
-import { computed } from "vue";
-import { useI18n } from "vue-i18n";
-import { Engine, engineFromJSON } from "@/types/proto/v1/common";
-import { engineNameV1 } from "@/utils";
-
-const props = defineProps<{
- selected: Engine;
- engineList: Engine[];
- individualEngineList: Engine[];
-}>();
-
-defineEmits<{
- (event: "update:engine", id: Engine): void;
-}>();
-
-const { t } = useI18n();
-
-const items = computed(() => {
- const resp = props.individualEngineList.map((engine) => {
- return {
- value: engine,
- label: engineNameV1(engine),
- };
- });
- if (props.individualEngineList.length < props.engineList.length) {
- resp.unshift({
- value: Engine.ENGINE_UNSPECIFIED,
- label: t("sql-review.other-engines"),
- });
- }
- return resp;
-});
-</script>
diff --git a/frontend/src/components/SQLReview/components/SQLReviewTabsByEngine.vue b/frontend/src/components/SQLReview/components/SQLReviewTabsByEngine.vue
index 90866a9181..c7b2862cf0 100644
--- a/frontend/src/components/SQLReview/components/SQLReviewTabsByEngine.vue
+++ b/frontend/src/components/SQLReview/components/SQLReviewTabsByEngine.vue
@@ -1,26 +1,27 @@
<template>
- <NTabs v-model:value="selectedEngine" :type="'line'">
- <NTabPane v-for="data in engineList" :key="data.engine" :name="data.engine">
+ <NTabs v-model:value="selectedEngine" :type="'line'" :size="'large'">
+ <NTabPane
+ v-for="[engine, ruleMap] in ruleMapByEngine.entries()"
+ :key="engine"
+ :name="engine"
+ >
<template #tab>
<div class="flex items-center space-x-2">
- <EngineIcon
- :engine="engineFromJSON(data.engine)"
- custom-class="ml-1"
- />
+ <EngineIcon :engine="engine" custom-class="ml-1" />
<RichEngineName
- :engine="data.engine"
+ :engine="engine"
tag="p"
class="text-center text-sm !text-main"
/>
<span
class="items-center text-xs px-1 py-0.5 rounded-full bg-gray-200 text-gray-800"
>
- {{ data.ruleList.length }}
+ {{ ruleMap.size }}
</span>
</div>
</template>
<template #default>
- <slot :rule-list="data.ruleList" :engine="selectedEngine" />
+ <slot :rule-list="[...ruleMap.values()]" :engine="selectedEngine" />
</template>
</NTabPane>
</NTabs>
@@ -31,37 +32,15 @@ import { NTabs, NTabPane } from "naive-ui";
import { ref, watchEffect } from "vue";
import type { RuleTemplateV2 } from "@/types";
import { Engine } from "@/types/proto/v1/common";
-import { engineFromJSON } from "@/types/proto/v1/common";
-
-type EngineTypeStats = {
- engine: Engine;
- ruleList: RuleTemplateV2[];
-};
const selectedEngine = ref<Engine>(Engine.UNRECOGNIZED);
-const engineList = ref<EngineTypeStats[]>([]);
const props = defineProps<{
- ruleList: RuleTemplateV2[];
+ ruleMapByEngine: Map<Engine, Map<string, RuleTemplateV2>>;
}>();
watchEffect(() => {
- const tmp = props.ruleList.reduce(
- (dict, rule) => {
- if (!dict[rule.engine]) {
- dict[rule.engine] = {
- engine: rule.engine,
- ruleList: [],
- };
- }
- dict[rule.engine].ruleList.push(rule);
- return dict;
- },
- {} as { [id: string]: EngineTypeStats }
- );
- engineList.value = Object.values(tmp);
- if (engineList.value.length > 0) {
- selectedEngine.value = engineList.value[0].engine;
- }
+ selectedEngine.value =
+ [...props.ruleMapByEngine.keys()][0] ?? Engine.UNRECOGNIZED;
});
</script>
diff --git a/frontend/src/components/SQLReview/components/SQLRuleEditDialog.vue b/frontend/src/components/SQLReview/components/SQLRuleEditDialog.vue
index 7aa36d88c0..756c86c059 100644
--- a/frontend/src/components/SQLReview/components/SQLRuleEditDialog.vue
+++ b/frontend/src/components/SQLReview/components/SQLRuleEditDialog.vue
@@ -1,21 +1,22 @@
<template>
<BBModal
- :title="
- editable
- ? $t('sql-review.edit-rule.self')
- : $t('sql-review.edit-rule.readonly')
- "
+ :title="getRuleLocalization(rule.type).title"
@close="$emit('cancel')"
>
<div class="space-y-4 w-[calc(100vw-5rem)] sm:w-[40rem] pb-1">
<div class="space-y-1">
<div class="flex items-center space-x-2">
- <div>
- <RuleEngineIcon :engine="rule.engine" />
- </div>
<h3 class="text-lg text-control font-medium">
{{ $t("common.name") }}
</h3>
+ <div class="flex items-center space-x-2">
+ <EngineIcon :engine="rule.engine" custom-class="ml-1" />
+ <RichEngineName
+ :engine="rule.engine"
+ tag="p"
+ class="text-center text-sm !text-main"
+ />
+ </div>
</div>
<div class="textinfolabel flex items-center gap-x-2">
{{ getRuleLocalization(rule.type).title }}
@@ -147,6 +148,7 @@
import { NSwitch } from "naive-ui";
import { computed, nextTick, reactive, watch } from "vue";
import { useI18n } from "vue-i18n";
+import { payloadValueListToComponentList } from "@/components/SQLReview/components";
import { SQLReviewRuleLevel } from "@/types/proto/v1/org_policy_service";
import type { RuleConfigComponent, RuleTemplateV2 } from "@/types/sqlReview";
import { getRuleLocalization, getRuleLocalizationKey } from "@/types/sqlReview";
@@ -158,7 +160,6 @@ import {
StringArrayComponent,
TemplateComponent,
} from "./RuleConfigComponents";
-import RuleEngineIcon from "./RuleEngineIcon.vue";
import RuleLevelSwitch from "./RuleLevelSwitch.vue";
type LocalState = {
@@ -174,9 +175,7 @@ const props = defineProps<{
}>();
const emit = defineEmits<{
- (event: "update:payload", payload: PayloadValueType[]): void;
- (event: "update:level", level: SQLReviewRuleLevel): void;
- (event: "update:comment", comment: string): void;
+ (event: "update:rule", update: Partial<RuleTemplateV2>): void;
(event: "cancel"): void;
}>();
@@ -232,9 +231,11 @@ watch(
);
const confirm = () => {
- emit("update:level", state.level);
- emit("update:payload", state.payload);
- emit("update:comment", state.comment);
+ emit("update:rule", {
+ componentList: payloadValueListToComponentList(props.rule, state.payload),
+ level: state.level,
+ comment: state.comment,
+ });
nextTick(() => {
emit("cancel");
});
diff --git a/frontend/src/components/SQLReview/components/SQLRuleTable.vue b/frontend/src/components/SQLReview/components/SQLRuleTable.vue
index b98e63372e..f92ebcc694 100644
--- a/frontend/src/components/SQLReview/components/SQLRuleTable.vue
+++ b/frontend/src/components/SQLReview/components/SQLRuleTable.vue
@@ -163,9 +163,7 @@
:rule="state.activeRule"
:disabled="!isRuleAvailable(state.activeRule)"
@cancel="state.activeRule = undefined"
- @update:payload="updatePayload(state.activeRule!, $event)"
- @update:level="updateLevel(state.activeRule!, $event)"
- @update:comment="updateComment(state.activeRule!, $event)"
+ @update:rule="onRuleChanged"
/>
</div>
</template>
@@ -176,7 +174,6 @@ import { NSwitch } from "naive-ui";
import { computed, reactive } from "vue";
import { useI18n } from "vue-i18n";
import { BBGrid, type BBGridColumn } from "@/bbkit";
-import { payloadValueListToComponentList } from "@/components/SQLReview/components";
import { useCurrentPlan } from "@/store";
import type { RuleTemplateV2 } from "@/types";
import {
@@ -186,7 +183,6 @@ import {
planTypeToString,
} from "@/types";
import { SQLReviewRuleLevel } from "@/types/proto/v1/org_policy_service";
-import type { PayloadValueType } from "./RuleConfigComponents";
import RuleLevelSwitch from "./RuleLevelSwitch.vue";
import SQLRuleEditDialog from "./SQLRuleEditDialog.vue";
@@ -256,18 +252,14 @@ const toggleActivity = (rule: RuleTemplateV2, on: boolean) => {
);
};
-const updatePayload = (rule: RuleTemplateV2, data: PayloadValueType[]) => {
- if (!rule.componentList) {
+const onRuleChanged = (update: Partial<RuleTemplateV2>) => {
+ if (!state.activeRule) {
return;
}
- emit("rule-change", rule, payloadValueListToComponentList(rule, data));
+ emit("rule-change", state.activeRule, update);
};
const updateLevel = (rule: RuleTemplateV2, level: SQLReviewRuleLevel) => {
emit("rule-change", rule, { level });
};
-
-const updateComment = (rule: RuleTemplateV2, comment: string) => {
- emit("rule-change", rule, { comment });
-};
</script>
diff --git a/frontend/src/components/SQLReview/components/utils.ts b/frontend/src/components/SQLReview/components/utils.ts
index e22d9551fa..4fcc5553f7 100644
--- a/frontend/src/components/SQLReview/components/utils.ts
+++ b/frontend/src/components/SQLReview/components/utils.ts
@@ -12,15 +12,15 @@ export const rulesToTemplate = (
withDisabled: boolean
) => {
const ruleTemplateList: RuleTemplateV2[] = [];
- const usedRule = new Set();
+ const usedRule = new Set<string>();
for (const rule of review.ruleList) {
- const ruleTemplate = ruleTemplateMapV2.get(rule.type)?.get(rule.engine);
+ const ruleTemplate = ruleTemplateMapV2.get(rule.engine)?.get(rule.type);
if (!ruleTemplate) {
continue;
}
- usedRule.add(rule.type);
+ usedRule.add(`${rule.engine}-${rule.type}`);
const data = convertPolicyRuleToRuleTemplate(rule, ruleTemplate);
if (data) {
ruleTemplateList.push(data);
@@ -28,11 +28,11 @@ export const rulesToTemplate = (
}
if (withDisabled) {
- for (const [key, map] of ruleTemplateMapV2.entries()) {
- if (usedRule.has(key)) {
- continue;
- }
+ for (const map of ruleTemplateMapV2.values()) {
for (const rule of map.values()) {
+ if (usedRule.has(`${rule.engine}-${rule.type}`)) {
+ continue;
+ }
ruleTemplateList.push({
...rule,
level: SQLReviewRuleLevel.DISABLED,
@@ -52,7 +52,7 @@ export const payloadValueListToComponentList = (
rule: RuleTemplateV2,
data: PayloadValueType[]
) => {
- const componentList = rule.componentList.reduce<RuleConfigComponent[]>(
+ return rule.componentList.reduce<RuleConfigComponent[]>(
(list, component, index) => {
switch (component.payload.type) {
case "STRING_ARRAY":
@@ -96,6 +96,4 @@ export const payloadValueListToComponentList = (
},
[]
);
-
- return { componentList };
};
diff --git a/frontend/src/store/modules/v1/setting.ts b/frontend/src/store/modules/v1/setting.ts
index 0ed700b029..fbb2f98fb8 100644
--- a/frontend/src/store/modules/v1/setting.ts
+++ b/frontend/src/store/modules/v1/setting.ts
@@ -77,7 +77,6 @@ export const useSettingV1Store = defineStore("setting_v1", {
},
async fetchSettingList() {
const { settings } = await settingServiceClient.listSettings({});
- // console.log(JSON.stringify(settings, null, " "));
for (const setting of settings) {
this.settingMapByName.set(setting.name, setting);
}
diff --git a/frontend/src/types/sqlReview.ts b/frontend/src/types/sqlReview.ts
index 28611c3fa3..614c8f3c2e 100644
--- a/frontend/src/types/sqlReview.ts
+++ b/frontend/src/types/sqlReview.ts
@@ -154,20 +154,25 @@ export interface SQLReviewPolicyTemplateV2 {
ruleList: RuleTemplateV2[];
}
-export const ruleTemplateMapV2 = (sqlReviewSchema as RuleTemplateV2[]).reduce(
- (map, ruleSchema) => {
- if (!map.has(ruleSchema.type)) {
- map.set(ruleSchema.type, new Map());
+export const getRuleMapByEngine = (
+ ruleList: RuleTemplateV2[]
+): Map<Engine, Map<string, RuleTemplateV2>> => {
+ return ruleList.reduce((map, rule) => {
+ const engine = engineFromJSON(rule.engine);
+ if (!map.has(engine)) {
+ map.set(engine, new Map());
}
- const engine = engineFromJSON(ruleSchema.engine);
- map.get(ruleSchema.type)?.set(engine, {
- ...ruleSchema,
+ map.get(engine)?.set(rule.type, {
+ ...rule,
engine,
- componentList: ruleSchema.componentList || [],
+ componentList: rule.componentList || [],
});
return map;
- },
- new Map<string, Map<Engine, RuleTemplateV2>>()
+ }, new Map<Engine, Map<string, RuleTemplateV2>>());
+};
+
+export const ruleTemplateMapV2 = getRuleMapByEngine(
+ sqlReviewSchema as RuleTemplateV2[]
);
// Build the frontend template list based on schema and template.
@@ -194,8 +199,8 @@ export const TEMPLATE_LIST_V2: SQLReviewPolicyTemplateV2[] = (function () {
for (const rule of template.ruleList) {
const ruleTemplate = ruleTemplateMapV2
- .get(rule.type)
- ?.get(engineFromJSON(rule.engine));
+ .get(engineFromJSON(rule.engine))
+ ?.get(rule.type);
if (!ruleTemplate) {
continue;
}
@@ -683,9 +688,23 @@ const mergeIndividualConfigAsRule = (
throw new Error(`Invalid rule ${template.type}`);
};
+export const convertRuleMapToPolicyRuleList = (
+ ruleMapByEngine: Map<Engine, Map<string, RuleTemplateV2>>
+): SchemaPolicyRule[] => {
+ const resp: SchemaPolicyRule[] = [];
+
+ for (const mapByType of ruleMapByEngine.values()) {
+ for (const rule of mapByType.values()) {
+ resp.push(convertRuleTemplateToPolicyRule(rule));
+ }
+ }
+
+ return resp;
+};
+
// The convertRuleTemplateToPolicyRule will convert rule template to review policy rule for backend useage.
// Will throw exception if we don't implement the payload handler for specific type of rule.
-export const convertRuleTemplateToPolicyRule = (
+const convertRuleTemplateToPolicyRule = (
rule: RuleTemplateV2
): SchemaPolicyRule => {
const base: SchemaPolicyRule = {
diff --git a/frontend/src/views/SettingWorkspaceSQLReviewDetail.vue b/frontend/src/views/SettingWorkspaceSQLReviewDetail.vue
index d24582fc7c..f9d9f92a95 100644
--- a/frontend/src/views/SettingWorkspaceSQLReviewDetail.vue
+++ b/frontend/src/views/SettingWorkspaceSQLReviewDetail.vue
@@ -65,13 +65,16 @@
</div>
</div>
- <SQLReviewTabsByEngine class="mt-5" :rule-list="state.ruleList">
+ <SQLReviewTabsByEngine
+ class="mt-5"
+ :rule-map-by-engine="state.ruleMapByEngine"
+ >
<template
- #default="{ ruleList, engine }: { ruleList: RuleTemplateV2[]; engine: Engine; }"
+ #default="{ ruleList: ruleListFilteredByEngine, engine }: { ruleList: RuleTemplateV2[]; engine: Engine; }"
>
<SQLRuleTableWithFilter
:engine="engine"
- :rule-list="ruleList"
+ :rule-list="ruleListFilteredByEngine"
:editable="hasPermission"
@rule-change="markChange"
/>
@@ -132,7 +135,6 @@
<script lang="tsx" setup>
import { useTitle } from "@vueuse/core";
-import { cloneDeep } from "lodash-es";
import { computed, reactive, watch, watchEffect } from "vue";
import { useI18n } from "vue-i18n";
import { useRouter } from "vue-router";
@@ -148,8 +150,10 @@ import {
import type { RuleTemplateV2 } from "@/types";
import {
unknown,
+ UNKNOWN_ID,
+ getRuleMapByEngine,
ruleIsAvailableInSubscription,
- convertRuleTemplateToPolicyRule,
+ convertRuleMapToPolicyRuleList,
} from "@/types";
import type { Engine } from "@/types/proto/v1/common";
import { hasWorkspacePermissionV2 } from "@/utils";
@@ -163,7 +167,7 @@ interface LocalState {
showEnableModal: boolean;
selectedCategory?: string;
editMode: boolean;
- ruleList: RuleTemplateV2[];
+ ruleMapByEngine: Map<Engine, Map<string, RuleTemplateV2>>;
rulesUpdated: boolean;
updating: boolean;
editingTitle: boolean;
@@ -179,7 +183,7 @@ const state = reactive<LocalState>({
showDisableModal: false,
showEnableModal: false,
editMode: false,
- ruleList: [],
+ ruleMapByEngine: new Map(),
rulesUpdated: false,
updating: false,
editingTitle: false,
@@ -201,7 +205,7 @@ const reviewPolicy = computed(() => {
});
const ruleListOfPolicy = computed((): RuleTemplateV2[] => {
- if (!reviewPolicy.value) {
+ if (reviewPolicy.value.id === `${UNKNOWN_ID}`) {
return [];
}
return rulesToTemplate(reviewPolicy.value, true).ruleList;
@@ -210,7 +214,7 @@ const ruleListOfPolicy = computed((): RuleTemplateV2[] => {
watch(
ruleListOfPolicy,
(ruleList) => {
- state.ruleList = cloneDeep(ruleList);
+ state.ruleMapByEngine = getRuleMapByEngine(ruleList);
},
{ immediate: true }
);
@@ -251,20 +255,19 @@ const markChange = (
return;
}
- const index = state.ruleList.findIndex((r) => r.type === rule.type);
- if (index < 0) {
+ const selectedRule = state.ruleMapByEngine.get(rule.engine)?.get(rule.type);
+ if (!selectedRule) {
return;
}
- const newRule = {
- ...state.ruleList[index],
- ...overrides,
- };
- state.ruleList[index] = newRule;
+ state.ruleMapByEngine
+ .get(rule.engine)
+ ?.set(rule.type, Object.assign(selectedRule, overrides));
+
state.rulesUpdated = true;
};
const onCancelChanges = () => {
- state.ruleList = cloneDeep(ruleListOfPolicy.value);
+ state.ruleMapByEngine = getRuleMapByEngine(ruleListOfPolicy.value);
state.rulesUpdated = false;
};
@@ -276,7 +279,7 @@ const onApplyChanges = async () => {
await store.updateReviewPolicy({
id: policy.id,
title: policy.name,
- ruleList: state.ruleList.map(convertRuleTemplateToPolicyRule),
+ ruleList: convertRuleMapToPolicyRuleList(state.ruleMapByEngine),
});
state.rulesUpdated = false;
pushUpdatedNotify();