Skip to content

Commit

Permalink
change model name && add log
Browse files Browse the repository at this point in the history
  • Loading branch information
hasa1K committed Oct 26, 2023
1 parent 5f3687c commit ac44b61
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
18 changes: 12 additions & 6 deletions sqle/api/controller/v1/audit_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -792,15 +792,15 @@ func GetAuditPlanReport(c echo.Context) error {
})
}

func filterSQLsByBlankList(sqls []*AuditPlanSQLReqV1, blankList []*model.BlankListAduitPlanSQL) []*AuditPlanSQLReqV1 {
func filterSQLsByBlankList(sqls []*AuditPlanSQLReqV1, blankList []*model.BlankListAuditPlanSQL) []*AuditPlanSQLReqV1 {
fileredSQLs := []*AuditPlanSQLReqV1{}
l := log.NewEntry()
for _, sql := range sqls {
var match bool
for _, blankSQL := range blankList {
regex, err := regexp.Compile(blankSQL.FilterSQL)
if err != nil {
l.Errorf("blanklist regexp compile failed:%v", err)
l.Errorf("blanklist regexp compile failed:%v, regexp:%s", err, blankSQL.FilterSQL)
continue
}
match = regex.MatchString(sql.LastReceiveText)
Expand Down Expand Up @@ -866,18 +866,21 @@ func FullSyncAuditPlanSQLs(c echo.Context) error {
return controller.JSONBaseErrorReq(c, errAuditPlanNotExist)
}

l := log.NewEntry()
reqSQLs := req.SQLs
blankList, err := s.GetBlankListAduitPlanSQLs()
blankList, err := s.GetBlankListAuditPlanSQLs()
if err == nil {
reqSQLs = filterSQLsByBlankList(reqSQLs, blankList)
} else {
l.Infoln("blanklist is not used")
}

sqls, err := convertToModelAuditPlanSQL(c, ap, reqSQLs)
if err != nil {
return controller.JSONBaseErrorReq(c, err)
}

return controller.JSONBaseErrorReq(c, auditplan.UploadSQLs(log.NewEntry(), ap, sqls, false))
return controller.JSONBaseErrorReq(c, auditplan.UploadSQLs(l, ap, sqls, false))
}

type PartialSyncAuditPlanSQLsReqV1 struct {
Expand Down Expand Up @@ -919,17 +922,20 @@ func PartialSyncAuditPlanSQLs(c echo.Context) error {
return controller.JSONBaseErrorReq(c, errAuditPlanNotExist)
}

l := log.NewEntry()
reqSQLs := req.SQLs
blankList, err := s.GetBlankListAduitPlanSQLs()
blankList, err := s.GetBlankListAuditPlanSQLs()
if err == nil {
reqSQLs = filterSQLsByBlankList(reqSQLs, blankList)
} else {
l.Infoln("blanklist is not used")
}

sqls, err := convertToModelAuditPlanSQL(c, ap, reqSQLs)
if err != nil {
return controller.JSONBaseErrorReq(c, err)
}
return controller.JSONBaseErrorReq(c, auditplan.UploadSQLs(log.NewEntry(), ap, sqls, true))
return controller.JSONBaseErrorReq(c, auditplan.UploadSQLs(l, ap, sqls, true))
}

func convertToModelAuditPlanSQL(c echo.Context, auditPlan *model.AuditPlan, reqSQLs []*AuditPlanSQLReqV1) ([]*auditplan.SQL, error) {
Expand Down
8 changes: 4 additions & 4 deletions sqle/model/audit_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ type AuditPlanSQLV2 struct {
Schema string `json:"schema" gorm:"type:varchar(512);not null"`
}

type BlankListAduitPlanSQL struct {
type BlankListAuditPlanSQL struct {
Model

FilterSQL string `json:"filter_sql" gorm:"type:varchar(512);not null;unique"`
}

func (s *Storage) GetBlankListAduitPlanSQLs() ([]*BlankListAduitPlanSQL, error) {
var blaps []*BlankListAduitPlanSQL
err := s.db.Model(BlankListAduitPlanSQL{}).Find(&blaps).Error
func (s *Storage) GetBlankListAuditPlanSQLs() ([]*BlankListAuditPlanSQL, error) {
var blaps []*BlankListAuditPlanSQL
err := s.db.Model(BlankListAuditPlanSQL{}).Find(&blaps).Error
return blaps, errors.New(errors.ConnectStorageError, err)
}

Expand Down
2 changes: 1 addition & 1 deletion sqle/model/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ var autoMigrateList = []interface{}{
&RuleKnowledge{},
&SqlManage{},
&SqlManageSqlAuditRecord{},
&BlankListAduitPlanSQL{},
&BlankListAuditPlanSQL{},
}

func (s *Storage) AutoMigrate() error {
Expand Down

0 comments on commit ac44b61

Please sign in to comment.