Skip to content

Commit

Permalink
Merge pull request #2758 from actiontech/ce-2742-3
Browse files Browse the repository at this point in the history
support quick audit based on rule template name
  • Loading branch information
winfredLIN authored Nov 15, 2024
2 parents 7d29fef + 29acc13 commit 79d5650
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
8 changes: 5 additions & 3 deletions sqle/api/controller/v1/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -643,10 +643,12 @@ func getRuleTemplateTips(c echo.Context, projectId string, filterDBType string)

ruleTemplateTipsRes := make([]RuleTemplateTipResV1, 0, len(ruleTemplates))
for _, roleTemplate := range ruleTemplates {
isDefaultRuleTemplate := roleTemplate.Name == fmt.Sprintf("default_%s", roleTemplate.DBType)
ruleTemplateTipRes := RuleTemplateTipResV1{
ID: roleTemplate.GetIDStr(),
Name: roleTemplate.Name,
DBType: roleTemplate.DBType,
ID: roleTemplate.GetIDStr(),
Name: roleTemplate.Name,
DBType: roleTemplate.DBType,
IsDefaultRuleTemplate: isDefaultRuleTemplate,
}
ruleTemplateTipsRes = append(ruleTemplateTipsRes, ruleTemplateTipRes)
}
Expand Down
14 changes: 14 additions & 0 deletions sqle/api/controller/v1/sql_audit_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,19 @@ func CreateSQLAuditRecord(c echo.Context) error {
}

s := model.GetStorage()

var ruleTemplateID uint
if req.RuleTemplateName != nil {
ruleTemplate, exist, err := s.GetGlobalAndProjectRuleTemplateByNameAndProjectId(*req.RuleTemplateName, projectUid)
if err != nil {
return controller.JSONBaseErrorReq(c, err)
}
if !exist {
return controller.JSONBaseErrorReq(c, errors.New(errors.DataNotExist, fmt.Errorf("rule template %v not exist", *req.RuleTemplateName)))
}
ruleTemplateID = ruleTemplate.ID
}

sqls := getSQLFromFileResp{}
user, err := controller.GetCurrentUser(c, dms.GetUser)
if err != nil {
Expand Down Expand Up @@ -125,6 +138,7 @@ func CreateSQLAuditRecord(c echo.Context) error {
return controller.JSONBaseErrorReq(c, err)
}
}
task.RuleTemplateID = ruleTemplateID
// if task instance is not nil, gorm will update instance when save task.
task.Instance = nil

Expand Down
11 changes: 10 additions & 1 deletion sqle/model/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,26 @@ type Task struct {
Status string `json:"status" gorm:"default:\"initialized\";type:varchar(255)"`
GroupId uint `json:"group_id" gorm:"column:group_id"`
CreateUserId uint64
RuleTemplateID uint `json:"rule_template_id" gorm:"column:rule_template_id"`
ExecStartAt *time.Time
ExecEndAt *time.Time
ExecMode string `json:"exec_mode" gorm:"default:'sqls';type:varchar(255)" example:"sqls"`
EnableBackup bool `gorm:"column:enable_backup"`
FileOrderMethod string `json:"file_order_method" gorm:"column:file_order_method;type:varchar(255)"`
Instance *Instance `json:"-" gorm:"-"`
RuleTemplate *RuleTemplate `json:"-" gorm:"foreignkey:RuleTemplateID"`
ExecuteSQLs []*ExecuteSQL `json:"-" gorm:"foreignkey:TaskId"`
RollbackSQLs []*RollbackSQL `json:"-" gorm:"foreignkey:TaskId"`
AuditFiles []*AuditFile `json:"-" gorm:"foreignkey:TaskId"`
}

func (t *Task) RuleTemplateName() string {
if t.RuleTemplate != nil {
return t.RuleTemplate.Name
}
return ""
}

func (t *Task) InstanceName() string {
if t.Instance != nil {
return t.Instance.Name
Expand Down Expand Up @@ -445,7 +454,7 @@ func (s *Storage) GetTasksByIds(taskIds []uint) (tasks []*Task, foundAllIds bool
func (s *Storage) GetTaskDetailById(taskId string) (*Task, bool, error) {
task := &Task{}
err := s.db.Where("id = ?", taskId).
Preload("ExecuteSQLs").Preload("RollbackSQLs").First(task).Error
Preload("RuleTemplate").Preload("ExecuteSQLs").Preload("RollbackSQLs").First(task).Error
if err == gorm.ErrRecordNotFound {
return nil, false, nil
}
Expand Down
2 changes: 1 addition & 1 deletion sqle/server/sqled.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (s *Sqled) addTask(projectId string, taskId string, typ int) (*action, erro
action.task = task

// plugin will be closed by drvMgr in Sqled.do().
rules, customRules, err = st.GetAllRulesByTmpNameAndProjectIdInstanceDBType("", "", task.Instance, task.DBType)
rules, customRules, err = st.GetAllRulesByTmpNameAndProjectIdInstanceDBType(task.RuleTemplateName(), projectId, task.Instance, task.DBType)
if err != nil {
goto Error
}
Expand Down

0 comments on commit 79d5650

Please sign in to comment.