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

Add audit result error result ce #2879

Merged
merged 5 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
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
8 changes: 5 additions & 3 deletions sqle/api/controller/v1/sql_manage.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,11 @@ type SqlManage struct {

// AuditResult 用于SQL全生命周期展示的AuditResult
type AuditResult struct {
Level string `json:"level" example:"warn"`
Message string `json:"message" example:"避免使用不必要的内置函数md5()"`
RuleName string `json:"rule_name"`
Level string `json:"level" example:"warn"`
Message string `json:"message" example:"避免使用不必要的内置函数md5()"`
RuleName string `json:"rule_name"`
ErrorInfo string `json:"error_info"`
ExecutionFailed bool `json:"execution_failed"`
}

type Source struct {
Expand Down
4 changes: 4 additions & 0 deletions sqle/api/controller/v2/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ type AssociatedRollbackWorkflow struct {

type AuditResult struct {
Level string `json:"level" example:"warn"`
ExecutionFailed bool `json:"execution_failed" example:"false"`
ErrorInfo string `json:"error_info"`
Message string `json:"message" example:"避免使用不必要的内置函数md5()"`
RuleName string `json:"rule_name"`
DbType string `json:"db_type"`
Expand Down Expand Up @@ -161,6 +163,8 @@ func GetTaskSQLs(c echo.Context) error {
ar := taskSQL.AuditResults[i]
taskSQLRes.AuditResult = append(taskSQLRes.AuditResult, &AuditResult{
Level: ar.Level,
ExecutionFailed: ar.ExecutionFailed,
ErrorInfo: ar.GetAuditErrorMsgByLangTag(locale.Bundle.GetLangTagFromCtx(c.Request().Context())),
Message: ar.GetAuditMsgByLangTag(locale.Bundle.GetLangTagFromCtx(c.Request().Context())),
RuleName: ar.RuleName,
DbType: task.DBType,
Expand Down
16 changes: 16 additions & 0 deletions sqle/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11480,6 +11480,9 @@ var doc = `{
"model.AuditResultInfo": {
"type": "object",
"properties": {
"errorInfo": {
"type": "string"
},
"message": {
"type": "string"
}
Expand Down Expand Up @@ -11996,6 +11999,12 @@ var doc = `{
"v1.AuditResult": {
"type": "object",
"properties": {
"error_info": {
"type": "string"
},
"execution_failed": {
"type": "boolean"
},
"level": {
"type": "string",
"example": "warn"
Expand Down Expand Up @@ -19663,6 +19672,13 @@ var doc = `{
"db_type": {
"type": "string"
},
"error_info": {
"type": "string"
},
"execution_failed": {
"type": "boolean",
"example": false
},
"i18n_audit_result_info": {
"type": "object",
"$ref": "#/definitions/model.I18nAuditResultInfo"
Expand Down
16 changes: 16 additions & 0 deletions sqle/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -11464,6 +11464,9 @@
"model.AuditResultInfo": {
"type": "object",
"properties": {
"errorInfo": {
"type": "string"
},
"message": {
"type": "string"
}
Expand Down Expand Up @@ -11980,6 +11983,12 @@
"v1.AuditResult": {
"type": "object",
"properties": {
"error_info": {
"type": "string"
},
"execution_failed": {
"type": "boolean"
},
"level": {
"type": "string",
"example": "warn"
Expand Down Expand Up @@ -19647,6 +19656,13 @@
"db_type": {
"type": "string"
},
"error_info": {
"type": "string"
},
"execution_failed": {
"type": "boolean",
"example": false
},
"i18n_audit_result_info": {
"type": "object",
"$ref": "#/definitions/model.I18nAuditResultInfo"
Expand Down
11 changes: 11 additions & 0 deletions sqle/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ definitions:
type: object
model.AuditResultInfo:
properties:
errorInfo:
type: string
message:
type: string
type: object
Expand Down Expand Up @@ -365,6 +367,10 @@ definitions:
type: object
v1.AuditResult:
properties:
error_info:
type: string
execution_failed:
type: boolean
level:
example: warn
type: string
Expand Down Expand Up @@ -5618,6 +5624,11 @@ definitions:
properties:
db_type:
type: string
error_info:
type: string
execution_failed:
example: false
type: boolean
i18n_audit_result_info:
$ref: '#/definitions/model.I18nAuditResultInfo'
type: object
Expand Down
12 changes: 4 additions & 8 deletions sqle/driver/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ func (inspect *MysqlDriverImpl) applyConfig(cfg *driverV2.Config) {
inspect.isOfflineAudit = cfg.DSN == nil

inspect.cnf = &Config{
DDLOSCMinSize: -1,
DDLGhostMinSize: -1,
DDLOSCMinSize: -1,
DDLGhostMinSize: -1,
}
for _, rule := range cfg.Rules {
if rule.Name == rulepkg.ConfigDDLOSCMinSize {
Expand Down Expand Up @@ -390,12 +390,8 @@ func (i *MysqlDriverImpl) audit(ctx context.Context, sql string) (*driverV2.Audi
}

if err := handler.Func(input); err != nil {
// todo #1630 临时跳过解析建表语句失败导致的规则
if session.IsParseShowCreateTableContentErr(err) {
i.Logger().Errorf("skip rule, rule_desc_name=%v rule_desc=%v err:%v", rule.Name, rule.I18nRuleInfo[i18nPkg.DefaultLang].Desc, err.Error())
continue
}
return nil, err
i.result.AddResultWithError(rule.Level, rule.Name, err.Error(), true, plocale.Bundle.LocalizeAll(rulepkg.RuleHandlerMap[rule.Name].Message))
i.Logger().Errorf("rule_desc_name=%v rule_desc=%v err:%v", rule.Name, rule.I18nRuleInfo[i18nPkg.DefaultLang].Desc, err.Error())
}
}

Expand Down
18 changes: 15 additions & 3 deletions sqle/driver/v2/driver_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,13 @@ type AuditResults struct {
type AuditResult struct {
Level RuleLevel
RuleName string
ExecutionFailed bool
I18nAuditResultInfo map[language.Tag]AuditResultInfo
}

type AuditResultInfo struct {
Message string
Message string
ErrorInfo string
}

func NewAuditResults() *AuditResults {
Expand All @@ -203,6 +205,9 @@ func NewAuditResults() *AuditResults {
func (rs *AuditResults) Level() RuleLevel {
level := RuleLevelNull
for _, curr := range rs.Results {
if curr.ExecutionFailed {
continue
}
if ruleLevelMap[curr.Level] > ruleLevelMap[level] {
level = curr.Level
}
Expand Down Expand Up @@ -235,6 +240,10 @@ func (rs *AuditResults) Message() string {
}

func (rs *AuditResults) Add(level RuleLevel, ruleName string, i18nMsgPattern i18nPkg.I18nStr, args ...interface{}) {
rs.AddResultWithError(level, ruleName, "", false, i18nMsgPattern, args...)
}

func (rs *AuditResults) AddResultWithError(level RuleLevel, ruleName, errorMsg string, executionFailed bool, i18nMsgPattern i18nPkg.I18nStr, args ...interface{}) {
if level == "" || len(i18nMsgPattern) == 0 {
return
}
Expand All @@ -251,7 +260,8 @@ func (rs *AuditResults) Add(level RuleLevel, ruleName string, i18nMsgPattern i18
msg = fmt.Sprintf(msg, args...)
}
v.I18nAuditResultInfo[langTag] = AuditResultInfo{
Message: msg,
Message: msg,
ErrorInfo: errorMsg,
}
}
return
Expand All @@ -262,14 +272,16 @@ func (rs *AuditResults) Add(level RuleLevel, ruleName string, i18nMsgPattern i18
ar := &AuditResult{
Level: level,
RuleName: ruleName,
ExecutionFailed: executionFailed,
I18nAuditResultInfo: make(map[language.Tag]AuditResultInfo, len(i18nMsgPattern)),
}
for langTag, msg := range i18nMsgPattern {
if len(args) > 0 {
msg = fmt.Sprintf(msg, args...)
}
ari := AuditResultInfo{
Message: msg,
Message: msg,
ErrorInfo: errorMsg,
}
ar.I18nAuditResultInfo[langTag] = ari
}
Expand Down
32 changes: 23 additions & 9 deletions sqle/model/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,21 @@ func (s *BaseSQL) GetExecStatusDesc(ctx context.Context) string {
type AuditResult struct {
Level string `json:"level"`
RuleName string `json:"rule_name"`
ExecutionFailed bool `json:"execution_failed"`
I18nAuditResultInfo I18nAuditResultInfo `json:"i18n_audit_result_info"`
}

func (ar *AuditResult) GetAuditMsgByLangTag(lang language.Tag) string {
return ar.I18nAuditResultInfo.GetAuditResultInfoByLangTag(lang).Message
}

func (ar *AuditResult) GetAuditErrorMsgByLangTag(lang language.Tag) string {
return ar.I18nAuditResultInfo.GetAuditResultInfoByLangTag(lang).ErrorInfo
}

type AuditResultInfo struct {
Message string
Message string
ErrorInfo string
}

type I18nAuditResultInfo map[language.Tag]AuditResultInfo
Expand Down Expand Up @@ -233,16 +239,20 @@ type AuditResults []AuditResult

func (a *AuditResults) GetAuditJsonStrByLangTag(lang language.Tag) string {
type AuditResultRes struct {
Level string `json:"level"`
Message string `json:"message"`
RuleName string `json:"rule_name"`
Level string `json:"level"`
Message string `json:"message"`
RuleName string `json:"rule_name"`
ErrorInfo string `json:"error_info"`
ExecutionFailed bool `json:"execution_failed"`
}
results := make([]AuditResultRes, len(*a))
for k, v := range *a {
results[k] = AuditResultRes{
Level: v.Level,
Message: v.GetAuditMsgByLangTag(lang),
RuleName: v.RuleName,
Level: v.Level,
Message: v.GetAuditMsgByLangTag(lang),
ErrorInfo: v.GetAuditErrorMsgByLangTag(lang),
RuleName: v.RuleName,
ExecutionFailed: v.ExecutionFailed,
}
}
data, _ := json.Marshal(results)
Expand Down Expand Up @@ -291,11 +301,13 @@ func ConvertAuditResultFromDriverToModel(dar *driverV2.AuditResult) *AuditResult
newAr := &AuditResult{
Level: string(dar.Level),
RuleName: dar.RuleName,
ExecutionFailed: dar.ExecutionFailed,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

遗留到之后处理,这种变量命名失去了原有对象的含义

I18nAuditResultInfo: make(map[language.Tag]AuditResultInfo, len(dar.I18nAuditResultInfo)),
}
for langTag, info := range dar.I18nAuditResultInfo {
newAr.I18nAuditResultInfo[langTag] = AuditResultInfo{
Message: info.Message,
Message: info.Message,
ErrorInfo: info.ErrorInfo,
}
}
return newAr
Expand All @@ -305,11 +317,13 @@ func ConvertAuditResultFromModelToDriver(mar *AuditResult) *driverV2.AuditResult
newAr := &driverV2.AuditResult{
Level: driverV2.RuleLevel(mar.Level),
RuleName: mar.RuleName,
ExecutionFailed: mar.ExecutionFailed,
I18nAuditResultInfo: make(map[language.Tag]driverV2.AuditResultInfo, len(mar.I18nAuditResultInfo)),
}
for langTag, info := range mar.I18nAuditResultInfo {
newAr.I18nAuditResultInfo[langTag] = driverV2.AuditResultInfo{
Message: info.Message,
Message: info.Message,
ErrorInfo: info.ErrorInfo,
}
}
return newAr
Expand Down
Loading