Skip to content

Commit

Permalink
rename mark high priority params
Browse files Browse the repository at this point in the history
  • Loading branch information
LordofAvernus committed Aug 16, 2024
1 parent 55f3760 commit 8794699
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 126 deletions.
26 changes: 13 additions & 13 deletions sqle/api/controller/v1/audit_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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{
Expand All @@ -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,
},
})
}
Expand Down
62 changes: 31 additions & 31 deletions sqle/api/controller/v1/instance_audit_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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,
})
}

Expand Down Expand Up @@ -331,20 +331,20 @@ 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.
if dbAuditPlan, ok := dbAuditPlansMap[auditPlan.Type]; ok {
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 {
Expand Down Expand Up @@ -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 获取实例扫描任务详情
Expand Down Expand Up @@ -686,17 +686,17 @@ 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
break
}
}
resAuditPlan.HighPriorityConditions = hppParamsRes
resAuditPlan.MarkHighPrioritySQL = v.MarkHighPrioritySQL
resAuditPlan.NeedMarkHighPrioritySQL = v.NeedMarkHighPrioritySQL
}

resAuditPlans = append(resAuditPlans, resAuditPlan)
Expand Down
16 changes: 8 additions & 8 deletions sqle/model/instance_audit_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand Down
2 changes: 2 additions & 0 deletions sqle/model/sql_manage.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,5 @@ type SqlManageRuleTips struct {
RuleName string `json:"rule_name"`
Desc string `json:"desc"`
}

const PriorityHigh = "high"
49 changes: 24 additions & 25 deletions sqle/pkg/params/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
},
}

Expand Down Expand Up @@ -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) {
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
29 changes: 15 additions & 14 deletions sqle/server/auditplan/job_task_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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聚合
Expand Down Expand Up @@ -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,
}
}
Expand Down
6 changes: 6 additions & 0 deletions sqle/server/auditplan/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ const (
paramKeyRegion = "region"
)

const (
OperationParamAuditLevel = "audit_level"
OperationParamQueryTimeAvg = MetricNameQueryTimeAvg
OperationParamRowExaminedAvg = MetricNameRowExaminedAvg
)

var MetaBuilderList = []MetaBuilder{
{
Type: TypeDefault,
Expand Down
Loading

0 comments on commit 8794699

Please sign in to comment.