Skip to content

Commit

Permalink
Merge pull request #1862 from actiontech/add-status-3
Browse files Browse the repository at this point in the history
fix:count error and use default status
  • Loading branch information
sjjian authored Sep 26, 2023
2 parents c745d7d + 23f7eb2 commit 4c3dcf2
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 15 deletions.
51 changes: 38 additions & 13 deletions sqle/model/sql_manage.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ type SqlManageDetail struct {
SqlAuditRecordID *string `json:"sql_audit_record_id"`
}

var sqlManageTotalCount = `
SELECT COUNT(DISTINCT sm.id)
{{- template "body" . -}}
`

var sqlManageQueryTpl = `
SELECT
sm.id,
Expand Down Expand Up @@ -159,6 +165,15 @@ AND sm.last_receive_timestamp <= :filter_last_audit_start_time_to
AND sm.status = :filter_status
{{- end }}
{{- if .count_bad_sql }}
AND sm.audit_level <> ''
AND sm.status = 'unhandled'
{{- end }}
{{- if .count_solved }}
AND sm.status = 'solved'
{{- end }}
{{ end }}
`

Expand All @@ -169,23 +184,33 @@ func (s *Storage) GetSqlManageListByReq(data map[string]interface{}) (list *SqlM
return nil, err
}

totalCount := len(sqlManageList)
totalCount, err := s.getCountResult(sqlManageBodyTpl, sqlManageTotalCount, data)
if err != nil {
return nil, err
}

var badSqlCount uint64
var solvedCount uint64
for _, sqlManage := range sqlManageList {
if sqlManage.AuditLevel != "" && sqlManage.Status != SQLManageStatusSolved {
badSqlCount += 1
fn := func(srcData map[string]interface{}, addSearchKey string) map[string]interface{} {
newData := make(map[string]interface{})
for k, v := range srcData {
newData[k] = v
}
newData[addSearchKey] = true
return newData
}

if sqlManage.Status == SQLManageStatusSolved {
solvedCount += 1
}
badSqlCount, err := s.getCountResult(sqlManageBodyTpl, sqlManageTotalCount, fn(data, "count_bad_sql"))
if err != nil {
return nil, err
}

solvedCount, err := s.getCountResult(sqlManageBodyTpl, sqlManageTotalCount, fn(data, "count_solved"))
if err != nil {
return nil, err
}

return &SqlManageResp{
SqlManageList: sqlManageList,
SqlManageTotalNum: uint64(totalCount),
SqlManageTotalNum: totalCount,
SqlManageBadNum: badSqlCount,
SqlManageOptimizedNum: solvedCount,
}, nil
Expand Down Expand Up @@ -233,16 +258,16 @@ func (s *Storage) InsertOrUpdateSqlManage(sqlManageList []*SqlManage) error {
args := make([]interface{}, 0, len(sqlManageList))
pattern := make([]string, 0, len(sqlManageList))
for _, sqlManage := range sqlManageList {
pattern = append(pattern, "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
pattern = append(pattern, "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
args = append(args, sqlManage.SqlFingerprint, sqlManage.ProjFpSourceInstSchemaMd5, sqlManage.SqlText,
sqlManage.Source, sqlManage.AuditLevel, sqlManage.AuditResults, sqlManage.FpCount, sqlManage.FirstAppearTimestamp,
sqlManage.LastReceiveTimestamp, sqlManage.InstanceName, sqlManage.SchemaName, sqlManage.Status, sqlManage.Remark,
sqlManage.LastReceiveTimestamp, sqlManage.InstanceName, sqlManage.SchemaName, sqlManage.Remark,
sqlManage.AuditPlanId, sqlManage.ProjectId, sqlManage.SqlAuditRecordId)
}

raw := fmt.Sprintf(`
INSERT INTO sql_manages (sql_fingerprint, proj_fp_source_inst_schema_md5, sql_text, source, audit_level, audit_results,
fp_count, first_appear_timestamp, last_receive_timestamp, instance_name, schema_name, status,
fp_count, first_appear_timestamp, last_receive_timestamp, instance_name, schema_name,
remark, audit_plan_id, project_id, sql_audit_record_id)
VALUES %s
ON DUPLICATE KEY UPDATE sql_text = VALUES(sql_text),
Expand Down
2 changes: 1 addition & 1 deletion sqle/server/auditplan/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func audit(auditPlanId uint, task Task) (*model.AuditPlanReportV2, error) {
}

go func() {
syncFromAuditPlan := NewSyncFromAuditPlan(auditPlanReport, auditResultResp.FilteredSqls)
syncFromAuditPlan := NewSyncFromAuditPlan(auditPlanReport, auditResultResp.FilteredSqls, taskResp)
if err := syncFromAuditPlan.SyncSqlManager(); err != nil {
log.NewEntry().WithField("name", auditResultResp.AuditPlanID).Errorf("schedule to save sql manage failed, error: %v", err)
}
Expand Down
4 changes: 3 additions & 1 deletion sqle/server/auditplan/mananger_comm.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ type SqlManager interface {
type SyncFromAuditPlan struct {
AuditReport *model.AuditPlanReportV2
FilterSqls []*model.AuditPlanSQLV2
Task *model.Task
}

func NewSyncFromAuditPlan(auditReport *model.AuditPlanReportV2, filterSqls []*model.AuditPlanSQLV2) SqlManager {
func NewSyncFromAuditPlan(auditReport *model.AuditPlanReportV2, filterSqls []*model.AuditPlanSQLV2, task *model.Task) SqlManager {
return &SyncFromAuditPlan{
AuditReport: auditReport,
FilterSqls: filterSqls,
Task: task,
}
}

Expand Down

0 comments on commit 4c3dcf2

Please sign in to comment.