Skip to content

Commit

Permalink
Merge pull request #2900 from actiontech/feat-MySQL-ai-rules-ce
Browse files Browse the repository at this point in the history
Feat my sql ai rules ce
  • Loading branch information
LordofAvernus authored Jan 24, 2025
2 parents 918b895 + 088cd00 commit 9f4dca2
Show file tree
Hide file tree
Showing 297 changed files with 30,133 additions and 145 deletions.
129 changes: 79 additions & 50 deletions sqle/api/controller/v1/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ import (
var ErrRuleTemplateNotExist = errors.New(errors.DataNotExist, fmt.Errorf("rule template not exist"))

type CreateRuleTemplateReqV1 struct {
Name string `json:"rule_template_name" valid:"required,name"`
Desc string `json:"desc"`
DBType string `json:"db_type" valid:"required"`
RuleList []RuleReqV1 `json:"rule_list" form:"rule_list" valid:"required,dive,required"`
Name string `json:"rule_template_name" valid:"required,name"`
Desc string `json:"desc"`
DBType string `json:"db_type" valid:"required"`
RuleVersion string `json:"rule_version"`
RuleList []RuleReqV1 `json:"rule_list" form:"rule_list" valid:"required,dive,required"`
}

type RuleReqV1 struct {
Expand Down Expand Up @@ -176,10 +177,11 @@ func CreateRuleTemplate(c echo.Context) error {
}

ruleTemplate := &model.RuleTemplate{
ProjectId: model.ProjectIdForGlobalRuleTemplate,
Name: req.Name,
Desc: req.Desc,
DBType: req.DBType,
ProjectId: model.ProjectIdForGlobalRuleTemplate,
Name: req.Name,
Desc: req.Desc,
DBType: req.DBType,
RuleVersion: req.RuleVersion,
}
templateRules := []model.RuleTemplateRule{}
templateCustomRules := []model.RuleTemplateCustomRule{}
Expand Down Expand Up @@ -277,10 +279,11 @@ type GetRuleTemplateResV1 struct {
}

type RuleTemplateDetailResV1 struct {
Name string `json:"rule_template_name"`
Desc string `json:"desc"`
DBType string `json:"db_type"`
RuleList []RuleResV1 `json:"rule_list,omitempty"`
Name string `json:"rule_template_name"`
Desc string `json:"desc"`
DBType string `json:"db_type"`
RuleVersion string `json:"rule_version"`
RuleList []RuleResV1 `json:"rule_list,omitempty"`
}

func convertRuleTemplateToRes(ctx context.Context, template *model.RuleTemplate) *RuleTemplateDetailResV1 {
Expand All @@ -298,10 +301,11 @@ func convertRuleTemplateToRes(ctx context.Context, template *model.RuleTemplate)
ruleList = append(ruleList, convertCustomRuleToRuleResV1(r.GetRule()))
}
return &RuleTemplateDetailResV1{
Name: template.Name,
Desc: template.Desc,
DBType: template.DBType,
RuleList: ruleList,
Name: template.Name,
Desc: template.Desc,
DBType: template.DBType,
RuleVersion: template.RuleVersion,
RuleList: ruleList,
}
}

Expand Down Expand Up @@ -402,9 +406,10 @@ type GetRuleTemplatesResV1 struct {
}

type RuleTemplateResV1 struct {
Name string `json:"rule_template_name"`
Desc string `json:"desc"`
DBType string `json:"db_type"`
Name string `json:"rule_template_name"`
Desc string `json:"desc"`
DBType string `json:"db_type"`
RuleVersion string `json:"rule_version"`
}

// @Summary 全局规则模板列表
Expand Down Expand Up @@ -454,9 +459,10 @@ func convertDefaultRuleTemplatesToRes(ctx context.Context, ruleTemplates []*mode
ruleTemplatesReq := make([]RuleTemplateResV1, 0, len(ruleTemplates))
for _, ruleTemplate := range ruleTemplates {
ruleTemplateReq := RuleTemplateResV1{
Name: ruleTemplate.Name,
Desc: locale.Bundle.LocalizeMsgByCtx(ctx, locale.DefaultRuleTemplatesDesc),
DBType: ruleTemplate.DBType,
Name: ruleTemplate.Name,
Desc: locale.Bundle.LocalizeMsgByCtx(ctx, locale.DefaultRuleTemplatesDesc),
DBType: ruleTemplate.DBType,
RuleVersion: ruleTemplate.RuleVersion,
}
ruleTemplatesReq = append(ruleTemplatesReq, ruleTemplateReq)
}
Expand All @@ -467,6 +473,7 @@ type GetRulesReqV1 struct {
FilterDBType string `json:"filter_db_type" query:"filter_db_type"`
FilterGlobalRuleTemplateName string `json:"filter_global_rule_template_name" query:"filter_global_rule_template_name"`
FilterRuleNames string `json:"filter_rule_names" query:"filter_rule_names"`
FilterRuleVersion string `json:"filter_rule_version" query:"filter_rule_version"`
FuzzyKeywordRule string `json:"fuzzy_keyword_rule" query:"fuzzy_keyword_rule"`
Tags string `json:"tags" query:"tags"`
}
Expand Down Expand Up @@ -591,6 +598,7 @@ func convertRulesToRes(ctx context.Context, rules interface{}) []RuleResV1 {
// @Param fuzzy_keyword_rule query string false "fuzzy rule,keyword for desc and annotation"
// @Param filter_global_rule_template_name query string false "filter global rule template name"
// @Param filter_rule_names query string false "filter rule name list"
// @Param filter_rule_version query string false "filter rule version"
// @Param tags query string false "filter tags"
// @Success 200 {object} v1.GetRulesResV1
// @router /v1/rules [get]
Expand All @@ -608,6 +616,7 @@ func GetRules(c echo.Context) error {
"filter_global_rule_template_name": req.FilterGlobalRuleTemplateName,
"filter_db_type": req.FilterDBType,
"filter_rule_names": req.FilterRuleNames,
"filter_rule_version": req.FilterRuleVersion,
"fuzzy_keyword_rule": req.FuzzyKeywordRule,
"tags": req.Tags,
})
Expand Down Expand Up @@ -687,6 +696,7 @@ type RuleTemplateTipResV1 struct {
ID string `json:"rule_template_id"`
Name string `json:"rule_template_name"`
DBType string `json:"db_type"`
RuleVersion string `json:"rule_version"`
IsDefaultRuleTemplate bool `json:"is_default_rule_template"`
}

Expand Down Expand Up @@ -726,6 +736,7 @@ func getRuleTemplateTips(c echo.Context, projectId string, filterDBType string)
ID: roleTemplate.GetIDStr(),
Name: roleTemplate.Name,
DBType: roleTemplate.DBType,
RuleVersion: roleTemplate.RuleVersion,
IsDefaultRuleTemplate: isDefaultRuleTemplate,
}
ruleTemplateTipsRes = append(ruleTemplateTipsRes, ruleTemplateTipRes)
Expand Down Expand Up @@ -776,10 +787,11 @@ func CloneRuleTemplate(c echo.Context) error {
}

ruleTemplate := &model.RuleTemplate{
ProjectId: model.ProjectIdForGlobalRuleTemplate,
Name: req.Name,
Desc: req.Desc,
DBType: sourceTpl.DBType,
ProjectId: model.ProjectIdForGlobalRuleTemplate,
Name: req.Name,
Desc: req.Desc,
DBType: sourceTpl.DBType,
RuleVersion: sourceTpl.RuleVersion,
}
err = s.Save(ruleTemplate)
if err != nil {
Expand Down Expand Up @@ -814,10 +826,11 @@ func CheckRuleTemplateCanBeBindEachInstance(s *model.Storage, tplName string, in
}

type CreateProjectRuleTemplateReqV1 struct {
Name string `json:"rule_template_name" valid:"required,name"`
Desc string `json:"desc"`
DBType string `json:"db_type" valid:"required"`
RuleList []RuleReqV1 `json:"rule_list" form:"rule_list" valid:"required,dive,required"`
Name string `json:"rule_template_name" valid:"required,name"`
Desc string `json:"desc"`
DBType string `json:"db_type" valid:"required"`
RuleVersion string `json:"rule_version"`
RuleList []RuleReqV1 `json:"rule_list" form:"rule_list" valid:"required,dive,required"`
}

// CreateProjectRuleTemplate
Expand Down Expand Up @@ -851,10 +864,11 @@ func CreateProjectRuleTemplate(c echo.Context) error {
}

ruleTemplate := &model.RuleTemplate{
ProjectId: model.ProjectUID(projectUid),
Name: req.Name,
Desc: req.Desc,
DBType: req.DBType,
ProjectId: model.ProjectUID(projectUid),
Name: req.Name,
Desc: req.Desc,
DBType: req.DBType,
RuleVersion: req.RuleVersion,
}
templateRules := []model.RuleTemplateRule{}
templateCustomRules := []model.RuleTemplateCustomRule{}
Expand Down Expand Up @@ -980,10 +994,11 @@ type GetProjectRuleTemplateResV1 struct {
}

type RuleProjectTemplateDetailResV1 struct {
Name string `json:"rule_template_name"`
Desc string `json:"desc"`
DBType string `json:"db_type"`
RuleList []RuleResV1 `json:"rule_list,omitempty"`
Name string `json:"rule_template_name"`
Desc string `json:"desc"`
DBType string `json:"db_type"`
RuleVersion string `json:"rule_version"`
RuleList []RuleResV1 `json:"rule_list,omitempty"`
}

type ProjectRuleTemplateInstance struct {
Expand Down Expand Up @@ -1044,10 +1059,11 @@ func convertProjectRuleTemplateToRes(ctx context.Context, template *model.RuleTe
ruleList = append(ruleList, convertCustomRuleToRuleResV1(r.GetRule()))
}
return &RuleProjectTemplateDetailResV1{
Name: template.Name,
Desc: template.Desc,
DBType: template.DBType,
RuleList: ruleList,
Name: template.Name,
Desc: template.Desc,
DBType: template.DBType,
RuleVersion: template.RuleVersion,
RuleList: ruleList,
}
}

Expand Down Expand Up @@ -1244,10 +1260,11 @@ func CloneProjectRuleTemplate(c echo.Context) error {
// }

ruleTemplate := &model.RuleTemplate{
ProjectId: model.ProjectUID(projectUid),
Name: req.Name,
Desc: req.Desc,
DBType: sourceTpl.DBType,
ProjectId: model.ProjectUID(projectUid),
Name: req.Name,
Desc: req.Desc,
DBType: sourceTpl.DBType,
RuleVersion: sourceTpl.RuleVersion,
}
err = s.Save(ruleTemplate)
if err != nil {
Expand Down Expand Up @@ -1298,10 +1315,11 @@ type ParseProjectRuleTemplateFileResV1 struct {
}

type ParseProjectRuleTemplateFileResDataV1 struct {
Name string `json:"name"`
Desc string `json:"desc"`
DBType string `json:"db_type"`
RuleList []RuleResV1 `json:"rule_list"`
Name string `json:"name"`
Desc string `json:"desc"`
DBType string `json:"db_type"`
RuleVersion string `json:"rule_version"`
RuleList []RuleResV1 `json:"rule_list"`
}

// ParseProjectRuleTemplateFile parse rule template
Expand Down Expand Up @@ -1345,8 +1363,19 @@ var ErrRule = e.New("rule has error")

func checkRuleList(file *ParseProjectRuleTemplateFileResDataV1) (*RuleTemplateExportErr, error) {
ruleNameList := make([]string, 0, len(file.RuleList))
var hasNewRule, hasOldRule bool
for _, rule := range file.RuleList {
ruleNameList = append(ruleNameList, rule.Name)
if file.DBType == driverV2.DriverTypeMySQL {
hasNewRule = hasNewRule || strings.HasPrefix(rule.Name, "SQLE")
hasOldRule = hasOldRule || !strings.HasPrefix(rule.Name, "SQLE")
if hasOldRule && hasNewRule {
return nil, fmt.Errorf("cannot import rule template that contains both old and new rules")
}
if hasNewRule {
file.RuleVersion = "v2"
}
}
}

s := model.GetStorage()
Expand Down
27 changes: 27 additions & 0 deletions sqle/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -8781,6 +8781,12 @@ var doc = `{
"name": "filter_rule_names",
"in": "query"
},
{
"type": "string",
"description": "filter rule version",
"name": "filter_rule_version",
"in": "query"
},
{
"type": "string",
"description": "filter tags",
Expand Down Expand Up @@ -12860,6 +12866,9 @@ var doc = `{
},
"rule_template_name": {
"type": "string"
},
"rule_version": {
"type": "string"
}
}
},
Expand Down Expand Up @@ -12931,6 +12940,9 @@ var doc = `{
},
"rule_template_name": {
"type": "string"
},
"rule_version": {
"type": "string"
}
}
},
Expand Down Expand Up @@ -16651,6 +16663,9 @@ var doc = `{
"items": {
"$ref": "#/definitions/v1.RuleResV1"
}
},
"rule_version": {
"type": "string"
}
}
},
Expand Down Expand Up @@ -17060,6 +17075,9 @@ var doc = `{
},
"rule_template_name": {
"type": "string"
},
"rule_version": {
"type": "string"
}
}
},
Expand Down Expand Up @@ -17170,6 +17188,9 @@ var doc = `{
},
"rule_template_name": {
"type": "string"
},
"rule_version": {
"type": "string"
}
}
},
Expand All @@ -17184,6 +17205,9 @@ var doc = `{
},
"rule_template_name": {
"type": "string"
},
"rule_version": {
"type": "string"
}
}
},
Expand All @@ -17201,6 +17225,9 @@ var doc = `{
},
"rule_template_name": {
"type": "string"
},
"rule_version": {
"type": "string"
}
}
},
Expand Down
Loading

0 comments on commit 9f4dca2

Please sign in to comment.