Skip to content

Commit

Permalink
Merge pull request #2709 from actiontech/issue-2675-4
Browse files Browse the repository at this point in the history
modify: extract GetSingleSQLPriorityWithReasons for reusing
  • Loading branch information
ColdWaterLW authored Oct 24, 2024
2 parents 5107abd + c65918e commit e29e502
Showing 1 changed file with 65 additions and 31 deletions.
96 changes: 65 additions & 31 deletions sqle/server/auditplan/job_task_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,42 +187,76 @@ func SetSQLPriority(sqlList []*model.SQLManageRecord) ([]*model.SQLManageRecord,
}
auditPlanMap[sourceId] = auditPlan
}

info, err := sql_.Info.OriginValue()
priority, _, err := GetSingleSQLPriorityWithReasons(auditPlan, sql_)
if err != nil {
return nil, err
}
highPriorityConditions := auditPlan.HighPriorityParams
priority := sql.NullString{}
for _, highPriorityCondition := range highPriorityConditions {
var compareParamVale string
// 审核级别特殊处理
if highPriorityCondition.Key == OperationParamAuditLevel {
switch sql_.AuditLevel {
case string(driverV2.RuleLevelNotice):
compareParamVale = "1"
case string(driverV2.RuleLevelWarn):
compareParamVale = "2"
case string(driverV2.RuleLevelError):
compareParamVale = "3"
default:
compareParamVale = "0"
}
} else {
infoV, ok := info[highPriorityCondition.Key]
if !ok {
continue
}
compareParamVale = fmt.Sprintf("%v", infoV)
}
if high, err := highPriorityConditions.CompareParamValue(highPriorityCondition.Key, compareParamVale); err == nil && high {
priority = sql.NullString{
String: model.PriorityHigh,
Valid: true,
}
if priority == model.PriorityHigh {
sqlList[i].Priority = sql.NullString{
String: model.PriorityHigh,
Valid: true,
}
}
sqlList[i].Priority = priority
}
return sqlList, nil
}

// 获取SQL的优先级以及优先级触发的原因,只有高优先级或者无优先级,若是高优先级,则返回model.PriorityHigh=high,如果无优先级则返回空字符串
func GetSingleSQLPriorityWithReasons(auditPlan *model.AuditPlanV2, sql *model.SQLManageRecord) (priority string, reasons []string, err error) {
if auditPlan == nil || sql == nil {
return "", reasons, nil
}
info, err := sql.Info.OriginValue()
if err != nil {
return "", nil, err
}
toAuditLevel := func(valueToBeCompared string) string {
switch valueToBeCompared {
case "1":
return "提示"
case "2":
return "警告"
case "3":
return "错误"
default:
return valueToBeCompared
}
}
highPriorityConditions := auditPlan.HighPriorityParams
// 遍历优先级条件
for _, highPriorityCondition := range highPriorityConditions {
var valueToBeCompared string
// 特殊处理审核级别
if highPriorityCondition.Key == OperationParamAuditLevel {
switch sql.AuditLevel {
case string(driverV2.RuleLevelNotice):
valueToBeCompared = "1"
case string(driverV2.RuleLevelWarn):
valueToBeCompared = "2"
case string(driverV2.RuleLevelError):
valueToBeCompared = "3"
default:
valueToBeCompared = "0"
}
} else {
// 获取信息中的相应字段值
infoV, ok := info[highPriorityCondition.Key]
if !ok {
continue
}
valueToBeCompared = fmt.Sprintf("%v", infoV)
}
// 检查是否为高优先级条件
if high, err := highPriorityConditions.CompareParamValue(highPriorityCondition.Key, valueToBeCompared); err == nil && high {
// 添加匹配的条件作为原因
if highPriorityCondition.Key == OperationParamAuditLevel {
valueToBeCompared = toAuditLevel(valueToBeCompared)
}
reasons = append(reasons, fmt.Sprintf("【%v %v %v,为:%s】", highPriorityCondition.Desc, highPriorityCondition.Operator.Value, highPriorityCondition.Param.Value, valueToBeCompared))
}
}
if len(reasons) > 0 {
return model.PriorityHigh, reasons, nil
}
return "", reasons, nil
}

0 comments on commit e29e502

Please sign in to comment.