From 87946991c59e052a578c4832f1cc0f1cb484ea87 Mon Sep 17 00:00:00 2001 From: LordofAvernus Date: Fri, 16 Aug 2024 07:03:29 +0000 Subject: [PATCH] rename mark high priority params --- sqle/api/controller/v1/audit_plan.go | 26 ++++---- sqle/api/controller/v1/instance_audit_plan.go | 62 +++++++++---------- sqle/model/instance_audit_plan.go | 16 ++--- sqle/model/sql_manage.go | 2 + sqle/pkg/params/params.go | 49 +++++++-------- sqle/server/auditplan/job_task_handler.go | 29 ++++----- sqle/server/auditplan/meta.go | 6 ++ sqle/server/auditplan/task_type_default.go | 40 ++++++++++++ .../auditplan/task_type_mysql_schema_meta.go | 36 +---------- 9 files changed, 140 insertions(+), 126 deletions(-) diff --git a/sqle/api/controller/v1/audit_plan.go b/sqle/api/controller/v1/audit_plan.go index 4b1ba06e5..bec71ee26 100644 --- a/sqle/api/controller/v1/audit_plan.go +++ b/sqle/api/controller/v1/audit_plan.go @@ -67,17 +67,17 @@ type AuditPlanParamResV1 struct { } type HighPriorityCondition struct { - Key string `json:"key"` - Desc string `json:"desc"` - Value string `json:"value"` - Type string `json:"type" enums:"string,int,bool,password"` - EnumsValues []params.EnumsValue `json:"enums_value"` - BooleanOperator BooleanOperator `json:"boolean_operator"` + Key string `json:"key"` + Desc string `json:"desc"` + Value string `json:"value"` + Type string `json:"type" enums:"string,int,bool,password"` + EnumsValues []params.EnumsValue `json:"enums_value"` + Operator Operator `json:"operator"` } -type BooleanOperator struct { - Value string `json:"boolean_operator_value"` - EnumsValue []params.EnumsValue `json:"boolean_operator_enums_value"` +type Operator struct { + Value string `json:"operator_value"` + EnumsValue []params.EnumsValue `json:"operator_enums_value"` } func ConvertAuditPlanMetaWithInstanceIdToRes(meta auditplan.Meta, instanceId string) AuditPlanMetaV1 { @@ -104,7 +104,7 @@ func ConvertAuditPlanMetaWithInstanceIdToRes(meta auditplan.Meta, instanceId str res.Params = paramsRes } // 高优先级参数 - if meta.HighPriorityParams != nil && len(meta.HighPriorityParams) > 0 { + if len(meta.HighPriorityParams) > 0 { highPriorityparamsRes := make([]HighPriorityCondition, 0, len(meta.HighPriorityParams)) for _, hpc := range meta.HighPriorityParams { highPriorityparamsRes = append(highPriorityparamsRes, HighPriorityCondition{ @@ -113,9 +113,9 @@ func ConvertAuditPlanMetaWithInstanceIdToRes(meta auditplan.Meta, instanceId str Type: string(hpc.Type), Value: hpc.Value, EnumsValues: hpc.Enums, - BooleanOperator: BooleanOperator{ - string(hpc.BooleanOperatorParam.Value), - hpc.BooleanOperatorParam.EnumsValue, + Operator: Operator{ + string(hpc.Operator.Value), + hpc.Operator.EnumsValue, }, }) } diff --git a/sqle/api/controller/v1/instance_audit_plan.go b/sqle/api/controller/v1/instance_audit_plan.go index bd65074cb..3c73007bb 100644 --- a/sqle/api/controller/v1/instance_audit_plan.go +++ b/sqle/api/controller/v1/instance_audit_plan.go @@ -39,16 +39,16 @@ type CreateInstanceAuditPlanReqV1 struct { } type AuditPlan struct { - RuleTemplateName string `json:"rule_template_name" from:"rule_template_name" example:"default_MySQL"` - Type string `json:"audit_plan_type" form:"audit_plan_type" example:"slow log"` - Params []AuditPlanParamReqV1 `json:"audit_plan_params" valid:"dive,required"` - HighPriorityConditions []HighPriorityConditionReq `json:"high_priority_conditions" valid:"dive,required"` - MarkHighPrioritySQL bool `json:"mark_high_priority_sql"` + RuleTemplateName string `json:"rule_template_name" from:"rule_template_name" example:"default_MySQL"` + Type string `json:"audit_plan_type" form:"audit_plan_type" example:"slow log"` + Params []AuditPlanParamReqV1 `json:"audit_plan_params" valid:"dive,required"` + HighPriorityConditions []HighPriorityConditionReq `json:"high_priority_conditions" valid:"dive,required"` + NeedMarkHighPrioritySQL bool `json:"need_mark_high_priority_sql"` } type HighPriorityConditionReq struct { - Key string `json:"key" form:"key" valid:"required"` - Value string `json:"value" form:"value" valid:"required"` - BooleanOperator string `json:"boolean_operator" form:"boolean_operator" default:">" enums:">,=,<" valid:"oneof=> = <"` + Key string `json:"key" form:"key" valid:"required"` + Value string `json:"value" form:"value" valid:"required"` + Operator string `json:"operator" form:"operator" default:">" enums:">,=,<" valid:"oneof=> = <"` } type CreatInstanceAuditPlanResV1 struct { @@ -76,7 +76,7 @@ func checkAndGenerateHighPriorityParams(auditPlanType, instanceType string, hpcP } // set and valid param. p.Value = hpcParam.Value - p.BooleanOperatorParam.Value = params.BooleanOperatorValue(hpcParam.BooleanOperator) + p.Operator.Value = params.OperatorValue(hpcParam.Operator) resetParams = append(resetParams, p) break } @@ -174,12 +174,12 @@ func CreateInstanceAuditPlan(c echo.Context) error { } auditPlans = append(auditPlans, &model.AuditPlanV2{ - Type: auditPlan.Type, - RuleTemplateName: ruleTemplateName, - Params: ps, - HighPriorityParams: hpc, - MarkHighPrioritySQL: auditPlan.MarkHighPrioritySQL, - ActiveStatus: model.ActiveStatusNormal, + Type: auditPlan.Type, + RuleTemplateName: ruleTemplateName, + Params: ps, + HighPriorityParams: hpc, + NeedMarkHighPrioritySQL: auditPlan.NeedMarkHighPrioritySQL, + ActiveStatus: model.ActiveStatusNormal, }) } @@ -331,12 +331,12 @@ func UpdateInstanceAuditPlan(c echo.Context) error { return controller.JSONBaseErrorReq(c, errors.New(errors.DataConflict, err)) } res := &model.AuditPlanV2{ - Type: auditPlan.Type, - RuleTemplateName: ruleTemplateName, - Params: ps, - HighPriorityParams: hpc, - MarkHighPrioritySQL: auditPlan.MarkHighPrioritySQL, - InstanceAuditPlanID: dbAuditPlans.ID, + Type: auditPlan.Type, + RuleTemplateName: ruleTemplateName, + Params: ps, + HighPriorityParams: hpc, + NeedMarkHighPrioritySQL: auditPlan.NeedMarkHighPrioritySQL, + InstanceAuditPlanID: dbAuditPlans.ID, } // if the data exists in the database, update the data; if it does not exist, insert the data. @@ -344,7 +344,7 @@ func UpdateInstanceAuditPlan(c echo.Context) error { dbAuditPlan.RuleTemplateName = res.RuleTemplateName dbAuditPlan.Params = res.Params dbAuditPlan.HighPriorityParams = res.HighPriorityParams - dbAuditPlan.MarkHighPrioritySQL = res.MarkHighPrioritySQL + dbAuditPlan.NeedMarkHighPrioritySQL = res.NeedMarkHighPrioritySQL result := dbAuditPlan resultAuditPlans = append(resultAuditPlans, result) } else { @@ -595,11 +595,11 @@ type InstanceAuditPlanDetailResV1 struct { } type AuditPlanRes struct { - RuleTemplateName string `json:"rule_template_name" from:"rule_template_name" example:"default_MySQL"` - Type AuditPlanTypeResBase `json:"audit_plan_type" form:"audit_plan_type"` - Params []AuditPlanParamResV1 `json:"audit_plan_params" valid:"dive,required"` - MarkHighPrioritySQL bool `json:"mark_high_priority_sql"` - HighPriorityConditions []HighPriorityCondition `json:"high_priority_conditions"` + RuleTemplateName string `json:"rule_template_name" from:"rule_template_name" example:"default_MySQL"` + Type AuditPlanTypeResBase `json:"audit_plan_type" form:"audit_plan_type"` + Params []AuditPlanParamResV1 `json:"audit_plan_params" valid:"dive,required"` + NeedMarkHighPrioritySQL bool `json:"need_mark_high_priority_sql"` + HighPriorityConditions []HighPriorityCondition `json:"high_priority_conditions"` } // @Summary 获取实例扫描任务详情 @@ -686,9 +686,9 @@ func ConvertAuditPlansToRes(auditPlans []*model.AuditPlanV2) ([]AuditPlanRes, er Desc: metaHpp.Desc, Value: hpp.Value, Type: string(metaHpp.Type), - BooleanOperator: BooleanOperator{ - Value: string(hpp.BooleanOperatorParam.Value), - EnumsValue: metaHpp.BooleanOperatorParam.EnumsValue, + Operator: Operator{ + Value: string(hpp.Operator.Value), + EnumsValue: metaHpp.Operator.EnumsValue, }, } hppParamsRes[i] = highParamRes @@ -696,7 +696,7 @@ func ConvertAuditPlansToRes(auditPlans []*model.AuditPlanV2) ([]AuditPlanRes, er } } resAuditPlan.HighPriorityConditions = hppParamsRes - resAuditPlan.MarkHighPrioritySQL = v.MarkHighPrioritySQL + resAuditPlan.NeedMarkHighPrioritySQL = v.NeedMarkHighPrioritySQL } resAuditPlans = append(resAuditPlans, resAuditPlan) diff --git a/sqle/model/instance_audit_plan.go b/sqle/model/instance_audit_plan.go index 91e9f866e..a677d0388 100644 --- a/sqle/model/instance_audit_plan.go +++ b/sqle/model/instance_audit_plan.go @@ -116,14 +116,14 @@ func (s *Storage) GetLatestAuditPlanRecordsV2() ([]*AuditPlanDetail, error) { type AuditPlanV2 struct { Model - InstanceAuditPlanID uint `json:"instance_audit_plan_id" gorm:"not null"` - Type string `json:"type" gorm:"type:varchar(255)"` - RuleTemplateName string `json:"rule_template_name" gorm:"type:varchar(255)"` - Params params.Params `json:"params" gorm:"type:varchar(1000)"` - HighPriorityParams params.ParamsWithOperator `json:"high_priority_params" gorm:"type:varchar(1000)"` - MarkHighPrioritySQL bool `json:"mark_high_priority_sql"` - ActiveStatus string `json:"active_status" gorm:"type:varchar(255)"` - LastCollectionTime *time.Time `json:"last_collection_time" gorm:"type:datetime(3)"` + InstanceAuditPlanID uint `json:"instance_audit_plan_id" gorm:"not null"` + Type string `json:"type" gorm:"type:varchar(255)"` + RuleTemplateName string `json:"rule_template_name" gorm:"type:varchar(255)"` + Params params.Params `json:"params" gorm:"type:varchar(1000)"` + HighPriorityParams params.ParamsWithOperator `json:"high_priority_params" gorm:"type:varchar(1000)"` + NeedMarkHighPrioritySQL bool `json:"need_mark_high_priority_sql"` + ActiveStatus string `json:"active_status" gorm:"type:varchar(255)"` + LastCollectionTime *time.Time `json:"last_collection_time" gorm:"type:datetime(3)"` AuditPlanSQLs []*SQLManageRecord `gorm:"foreignKey:SourceId"` } diff --git a/sqle/model/sql_manage.go b/sqle/model/sql_manage.go index f72ea8231..e0621ad2f 100644 --- a/sqle/model/sql_manage.go +++ b/sqle/model/sql_manage.go @@ -52,3 +52,5 @@ type SqlManageRuleTips struct { RuleName string `json:"rule_name"` Desc string `json:"desc"` } + +const PriorityHigh = "high" diff --git a/sqle/pkg/params/params.go b/sqle/pkg/params/params.go index 41468b0b7..8da636edd 100644 --- a/sqle/pkg/params/params.go +++ b/sqle/pkg/params/params.go @@ -191,7 +191,7 @@ func (r *Params) Copy() Params { type ParamsWithOperator []*ParamWithOperator type ParamWithOperator struct { Param - BooleanOperatorParam BooleanOperator `json:"boolean_operator"` + Operator Operator `json:"operator"` } // Scan impl sql.Scanner interface @@ -238,9 +238,9 @@ func (r ParamsWithOperator) Value() (driver.Value, error) { Desc: p.Desc, Type: p.Type, }, - BooleanOperatorParam: BooleanOperator{ - Value: p.BooleanOperatorParam.Value, - EnumsValue: p.BooleanOperatorParam.EnumsValue, + Operator: Operator{ + Value: p.Operator.Value, + EnumsValue: p.Operator.EnumsValue, }, } @@ -275,23 +275,23 @@ func (r *ParamsWithOperator) GetParam(key string) *ParamWithOperator { return nil } -type BooleanOperator struct { - Value BooleanOperatorValue `json:"boolean_operator_value"` - EnumsValue []EnumsValue `json:"boolean_operator_enums_value"` +type Operator struct { + Value OperatorValue `json:"boolean_operator_value"` + EnumsValue []EnumsValue `json:"boolean_operator_enums_value"` } -type BooleanOperatorValue string +type OperatorValue string const ( - LessThanOperator BooleanOperatorValue = "<" - GreaterThanOperator BooleanOperatorValue = ">" - LessThanOrEqualToOperator BooleanOperatorValue = "<=" - GreaterThanOrEqualToOperator BooleanOperatorValue = ">=" - EqualToOperator BooleanOperatorValue = "=" - NotEqualToOperator BooleanOperatorValue = "<>" - InOperator BooleanOperatorValue = "IN" - IsOperator BooleanOperatorValue = "IS" - ContainsOperator BooleanOperatorValue = "CONTAINS" + LessThanOperator OperatorValue = "<" + GreaterThanOperator OperatorValue = ">" + LessThanOrEqualToOperator OperatorValue = "<=" + GreaterThanOrEqualToOperator OperatorValue = ">=" + EqualToOperator OperatorValue = "=" + NotEqualToOperator OperatorValue = "<>" + InOperator OperatorValue = "IN" + IsOperator OperatorValue = "IS" + ContainsOperator OperatorValue = "CONTAINS" ) func (r *ParamsWithOperator) CompareParamValue(key string, inputValue string) (bool, error) { @@ -316,7 +316,7 @@ func (r *ParamsWithOperator) CompareParamValue(key string, inputValue string) (b if err != nil { return false, fmt.Errorf("failed to convert input value to int: %v", err) } - return compareInt(paramValue, inputIntValue, param.BooleanOperatorParam.Value), nil + return compareInt(paramValue, inputIntValue, param.Operator.Value), nil case ParamTypeFloat64: paramValue, err := strconv.ParseFloat(param.Value, 64) @@ -327,19 +327,18 @@ func (r *ParamsWithOperator) CompareParamValue(key string, inputValue string) (b if err != nil { return false, fmt.Errorf("failed to convert input value to float64: %v", err) } - return compareFloat64(paramValue, inputFloatValue, param.BooleanOperatorParam.Value), nil + return compareFloat64(paramValue, inputFloatValue, param.Operator.Value), nil case ParamTypeString: - return compareString(param.Value, inputValue, param.BooleanOperatorParam.Value), nil + return compareString(param.Value, inputValue, param.Operator.Value), nil default: return false, fmt.Errorf("unsupported ParamType: %s", param.Type) } } -// Helper functions to perform comparison based on BooleanOperator - -func compareInt(paramValue, inputValue int, operator BooleanOperatorValue) bool { +// Helper functions to perform comparison based on Operator +func compareInt(paramValue, inputValue int, operator OperatorValue) bool { switch operator { case LessThanOperator: return inputValue < paramValue @@ -358,7 +357,7 @@ func compareInt(paramValue, inputValue int, operator BooleanOperatorValue) bool } } -func compareFloat64(paramValue, inputValue float64, operator BooleanOperatorValue) bool { +func compareFloat64(paramValue, inputValue float64, operator OperatorValue) bool { switch operator { case LessThanOperator: return inputValue < paramValue @@ -377,7 +376,7 @@ func compareFloat64(paramValue, inputValue float64, operator BooleanOperatorValu } } -func compareString(paramValue, inputValue string, operator BooleanOperatorValue) bool { +func compareString(paramValue, inputValue string, operator OperatorValue) bool { switch operator { case LessThanOperator: return inputValue < paramValue diff --git a/sqle/server/auditplan/job_task_handler.go b/sqle/server/auditplan/job_task_handler.go index d2a195c80..444fd0c72 100644 --- a/sqle/server/auditplan/job_task_handler.go +++ b/sqle/server/auditplan/job_task_handler.go @@ -5,6 +5,7 @@ import ( "fmt" "time" + driverV2 "github.com/actiontech/sqle/sqle/driver/v2" "github.com/actiontech/sqle/sqle/log" "github.com/actiontech/sqle/sqle/model" "github.com/actiontech/sqle/sqle/server" @@ -66,7 +67,7 @@ func (j *AuditPlanHandlerJob) HandlerSQL(entry *logrus.Entry) { entry.Warnf("batch audit origin manager sql failed, error: %v", err) return } - sqlList, err = CheckSQLPriority(sqlList) + sqlList, err = SetSQLPriority(sqlList) if err != nil { entry.Warnf("check sql priority sql failed, error: %v", err) return @@ -139,7 +140,7 @@ func batchAuditSQLs(sqlList []*model.SQLManageRecord) ([]*model.SQLManageRecord, return auditSQLs, nil } -func CheckSQLPriority(sqlList []*model.SQLManageRecord) ([]*model.SQLManageRecord, error) { +func SetSQLPriority(sqlList []*model.SQLManageRecord) ([]*model.SQLManageRecord, error) { var err error s := model.GetStorage() // SQL聚合 @@ -167,29 +168,29 @@ func CheckSQLPriority(sqlList []*model.SQLManageRecord) ([]*model.SQLManageRecor } highPriorityConditions := auditPlan.HighPriorityParams for _, highPriorityCondition := range highPriorityConditions { - var cpmpareParamVale string + var compareParamVale string // 审核级别特殊处理 - if highPriorityCondition.Key == "audit_level" { + if highPriorityCondition.Key == OperationParamAuditLevel { switch sql_.AuditLevel { - case "notice": - cpmpareParamVale = "1" - case "warn": - cpmpareParamVale = "2" - case "error": - cpmpareParamVale = "3" + case string(driverV2.RuleLevelNotice): + compareParamVale = "1" + case string(driverV2.RuleLevelWarn): + compareParamVale = "2" + case string(driverV2.RuleLevelError): + compareParamVale = "3" default: - cpmpareParamVale = "0" + compareParamVale = "0" } } else { infoV, ok := info[highPriorityCondition.Key] if !ok { continue } - cpmpareParamVale = fmt.Sprintf("%v", infoV) + compareParamVale = fmt.Sprintf("%v", infoV) } - if high, err := highPriorityConditions.CompareParamValue(highPriorityCondition.Key, cpmpareParamVale); err == nil && high { + if high, err := highPriorityConditions.CompareParamValue(highPriorityCondition.Key, compareParamVale); err == nil && high { sqlList[i].Priority = sql.NullString{ - String: "high", + String: model.PriorityHigh, Valid: true, } } diff --git a/sqle/server/auditplan/meta.go b/sqle/server/auditplan/meta.go index 31b045785..732bf82fa 100644 --- a/sqle/server/auditplan/meta.go +++ b/sqle/server/auditplan/meta.go @@ -62,6 +62,12 @@ const ( paramKeyRegion = "region" ) +const ( + OperationParamAuditLevel = "audit_level" + OperationParamQueryTimeAvg = MetricNameQueryTimeAvg + OperationParamRowExaminedAvg = MetricNameRowExaminedAvg +) + var MetaBuilderList = []MetaBuilder{ { Type: TypeDefault, diff --git a/sqle/server/auditplan/task_type_default.go b/sqle/server/auditplan/task_type_default.go index 75a3ea4e1..1cd17bd51 100644 --- a/sqle/server/auditplan/task_type_default.go +++ b/sqle/server/auditplan/task_type_default.go @@ -28,6 +28,46 @@ func (at *DefaultTaskV2) InstanceType() string { func (at *DefaultTaskV2) Params(instanceId ...string) params.Params { return []*params.Param{} } + +var defaultOperatorEnums = []params.EnumsValue{ + { + Value: ">", + Desc: "大于", + }, { + Value: "=", + Desc: "等于", + }, { + Value: "<", + Desc: "小于", + }, +} + +var defaultAuditLevelOperateParams = ¶ms.ParamWithOperator{ + Param: params.Param{ + Key: OperationParamAuditLevel, + Value: "warn", + Desc: "触发审核级别", + Type: params.ParamTypeString, + Enums: []params.EnumsValue{ + { + Value: "1", + Desc: string(driverV2.RuleLevelNotice), + }, { + Value: "2", + Desc: string(driverV2.RuleLevelWarn), + }, + { + Value: "3", + Desc: string(driverV2.RuleLevelError), + }, + }, + }, + Operator: params.Operator{ + Value: ">", + EnumsValue: defaultOperatorEnums, + }, +} + func (at *DefaultTaskV2) HighPriorityParams() params.ParamsWithOperator { return []*params.ParamWithOperator{} } diff --git a/sqle/server/auditplan/task_type_mysql_schema_meta.go b/sqle/server/auditplan/task_type_mysql_schema_meta.go index 0effe60ef..1a2866cc6 100644 --- a/sqle/server/auditplan/task_type_mysql_schema_meta.go +++ b/sqle/server/auditplan/task_type_mysql_schema_meta.go @@ -140,41 +140,7 @@ func (at *BaseSchemaMetaTaskV2) Params(instanceId ...string) params.Params { func (at *BaseSchemaMetaTaskV2) HighPriorityParams() params.ParamsWithOperator { return []*params.ParamWithOperator{ - { - Param: params.Param{ - Key: "audit_level", - Value: "warn", - Desc: "告警级别", - Type: params.ParamTypeInt, - Enums: []params.EnumsValue{ - { - Value: "0", - Desc: "notice", - }, { - Value: "1", - Desc: "warn", - }, { - Value: "2", - Desc: "error", - }, - }, - }, - BooleanOperatorParam: params.BooleanOperator{ - Value: ">", - EnumsValue: []params.EnumsValue{ - { - Value: ">", - Desc: "大于", - }, { - Value: "=", - Desc: "等于", - }, { - Value: "<", - Desc: "小于", - }, - }, - }, - }, + defaultAuditLevelOperateParams, } }