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

fix: 细化新的规则分类映射 #2839

Merged
merged 3 commits into from
Dec 26, 2024
Merged
Changes from all 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
62 changes: 36 additions & 26 deletions sqle/model/utils.go
BugsGuru marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -228,17 +228,7 @@ func (s *Storage) UpdateCustomRuleCategoryRels() error {
if existed {
return nil
}
newCategoryMap := categoryMapping[customRule.Typ]
var tags []string
if newCategoryMap == nil {
// 自定义规则可以自己定义规则分类,对于这种分类的统一映射
tags = []string{plocale.RuleTagDatabase.ID, plocale.RuleTagTablespace.ID, plocale.RuleTagTable.ID, plocale.RuleTagColumn.ID, plocale.RuleTagIndex.ID, plocale.RuleTagView.ID, plocale.RuleTagProcedure.ID, plocale.RuleTagFunction.ID, plocale.RuleTagTrigger.ID, plocale.RuleTagEvent.ID, plocale.RuleTagUser.ID}
} else {
// 这里的newCategoryMap只会有一次循环
for _, newTags := range newCategoryMap {
tags = newTags
}
}
tags := mappingToNewCategory(customRule.Desc, customRule.Typ)
// 获取分类表中的分类信息
auditRuleCategories, err := s.GetAuditRuleCategoryByTagIn(tags)
if err != nil {
Expand Down Expand Up @@ -566,12 +556,33 @@ func (s *Storage) CreateDefaultTemplateIfNotExist(projectId ProjectUID, rules ma
return nil
}

func mappingToNewCategory(ruleName string, oldCategory string) []string {
// 当旧规则是命名规范的映射关系
if oldCategory == plocale.RuleTypeNamingConvention.Other {
if strings.Contains(ruleName, "database") || strings.Contains(ruleName, "object") {
return []string{plocale.RuleTagDatabase.ID}
} else if strings.Contains(ruleName, "index") || strings.Contains(ruleName, "pk") {
return []string{plocale.RuleTagIndex.ID}
} else {
return []string{
plocale.RuleTagDatabase.ID, plocale.RuleTagTablespace.ID, plocale.RuleTagTable.ID, plocale.RuleTagColumn.ID, plocale.RuleTagIndex.ID, plocale.RuleTagView.ID, plocale.RuleTagProcedure.ID, plocale.RuleTagFunction.ID, plocale.RuleTagTrigger.ID, plocale.RuleTagEvent.ID, plocale.RuleTagUser.ID}
}
}
newCategoryMap := categoryMapping[oldCategory]
if newCategoryMap == nil {
return []string{
plocale.RuleTagDatabase.ID, plocale.RuleTagTablespace.ID, plocale.RuleTagTable.ID, plocale.RuleTagColumn.ID, plocale.RuleTagIndex.ID, plocale.RuleTagView.ID, plocale.RuleTagProcedure.ID, plocale.RuleTagFunction.ID, plocale.RuleTagTrigger.ID, plocale.RuleTagEvent.ID, plocale.RuleTagUser.ID}
}
tags := make([]string, 0)
for _, newTags := range newCategoryMap {
tags = append(tags, newTags...)
}
return tags
}

var categoryMapping = map[string]map[string][]string{
plocale.RuleTypeGlobalConfig.Other: {
plocale.RuleCategoryAuditPurpose.ID: {plocale.RuleTagMaintenance.ID},
},
plocale.RuleTypeNamingConvention.Other: {
plocale.RuleCategoryOperand.ID: {plocale.RuleTagDatabase.ID, plocale.RuleTagTablespace.ID, plocale.RuleTagTable.ID, plocale.RuleTagColumn.ID, plocale.RuleTagIndex.ID, plocale.RuleTagView.ID, plocale.RuleTagProcedure.ID, plocale.RuleTagFunction.ID, plocale.RuleTagTrigger.ID, plocale.RuleTagEvent.ID, plocale.RuleTagUser.ID},
plocale.RuleCategoryAuditPurpose.ID: {plocale.RuleTagPerformance.ID},
},
plocale.RuleTypeIndexingConvention.Other: {
plocale.RuleCategoryOperand.ID: {plocale.RuleTagIndex.ID},
Expand All @@ -589,7 +600,7 @@ var categoryMapping = map[string]map[string][]string{
plocale.RuleCategorySQL.ID: {plocale.RuleTagDML.ID},
},
plocale.RuleTypeUsageSuggestion.Other: {
plocale.RuleCategoryOperand.ID: {plocale.RuleTagDatabase.ID, plocale.RuleTagTablespace.ID, plocale.RuleTagTable.ID, plocale.RuleTagColumn.ID, plocale.RuleTagIndex.ID, plocale.RuleTagView.ID, plocale.RuleTagProcedure.ID, plocale.RuleTagFunction.ID, plocale.RuleTagTrigger.ID, plocale.RuleTagEvent.ID, plocale.RuleTagUser.ID},
plocale.RuleCategoryAuditPurpose.ID: {plocale.RuleTagMaintenance.ID},
},
plocale.RuleTypeExecutePlan.Other: {
plocale.RuleCategoryAuditPurpose.ID: {plocale.RuleTagPerformance.ID},
Expand All @@ -609,19 +620,18 @@ func (s *Storage) UpdateRuleCategoryRels(rule *Rule) error {
if existed {
return nil
}
for _, tags := range categoryMapping[oldCategory] {
// 获取分类表中的分类信息
auditRuleCategories, err := s.GetAuditRuleCategoryByTagIn(tags)
tags := mappingToNewCategory(rule.Name, oldCategory)
// 获取分类表中的分类信息
auditRuleCategories, err := s.GetAuditRuleCategoryByTagIn(tags)
if err != nil {
return err
}
for _, newCategory := range auditRuleCategories {
auditRuleCategoryRel := AuditRuleCategoryRel{CategoryId: newCategory.ID, RuleName: rule.Name, RuleDBType: rule.DBType}
err = s.db.Create(&auditRuleCategoryRel).Error
if err != nil {
return err
}
for _, newCategory := range auditRuleCategories {
auditRuleCategoryRel := AuditRuleCategoryRel{CategoryId: newCategory.ID, RuleName: rule.Name, RuleDBType: rule.DBType}
err = s.db.Create(&auditRuleCategoryRel).Error
if err != nil {
return err
}
}
}
auditAccuracyCategories, err := s.GetAuditRuleCategoryByCategory(plocale.RuleCategoryAuditAccuracy.ID)
if err != nil {
Expand Down
Loading