Skip to content

Commit f398fca

Browse files
authored
Merge pull request #3174 from actiontech/add_sql_optimization
Add sql optimization
2 parents 8085bde + 2326031 commit f398fca

File tree

35 files changed

+3210
-256
lines changed

35 files changed

+3210
-256
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.19
55
require (
66
github.com/DATA-DOG/go-sqlmock v1.5.0
77
github.com/Masterminds/semver/v3 v3.1.1
8-
github.com/actiontech/dms v0.0.0-20231129083427-a97a77db08e0
8+
github.com/actiontech/dms v0.0.0-20251121053148-9ddead1d6d5b
99
github.com/actiontech/java-sql-extractor v0.0.0-20231103015812-cdd5fc040f62
1010
github.com/actiontech/mybatis-mapper-2-sql v0.5.0
1111
github.com/agiledragon/gomonkey v2.0.2+incompatible

go.sum

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,8 @@ github.com/VividCortex/ewma v1.1.1 h1:MnEK4VOv6n0RSY4vtRe3h11qjxL3+t0B8yOL8iMXdc
4848
github.com/VividCortex/ewma v1.1.1/go.mod h1:2Tkkvm3sRDVXaiyucHiACn4cqf7DpdyLvmxzcbUokwA=
4949
github.com/acomagu/bufpipe v1.0.4 h1:e3H4WUzM3npvo5uv95QuJM3cQspFNtFBzvJ2oNjKIDQ=
5050
github.com/acomagu/bufpipe v1.0.4/go.mod h1:mxdxdup/WdsKVreO5GpW4+M/1CE2sMG4jeGJ2sYmHc4=
51-
github.com/actiontech/dms v0.0.0-20231129060158-ff9d70a10557 h1:eeJqVzkX5X5gMfaN67X2wCFbqoVfbDgIsoSYeJJfCzs=
52-
github.com/actiontech/dms v0.0.0-20231129060158-ff9d70a10557/go.mod h1:jG8WjxiSBcUccweOqRPONDx95j88FwZytoa1/PI8I44=
53-
github.com/actiontech/dms v0.0.0-20231129083427-a97a77db08e0 h1:BZlpGNWg6aJhFcWZc17VHlOLWHvuxJZIKFY5ePlY5ZQ=
54-
github.com/actiontech/dms v0.0.0-20231129083427-a97a77db08e0/go.mod h1:jG8WjxiSBcUccweOqRPONDx95j88FwZytoa1/PI8I44=
51+
github.com/actiontech/dms v0.0.0-20251121053148-9ddead1d6d5b h1:pn0EZGvvF39gQ/rldmURRVJdxP7Gic0V6JKs8V9qyT8=
52+
github.com/actiontech/dms v0.0.0-20251121053148-9ddead1d6d5b/go.mod h1:jG8WjxiSBcUccweOqRPONDx95j88FwZytoa1/PI8I44=
5553
github.com/actiontech/java-sql-extractor v0.0.0-20231103015812-cdd5fc040f62 h1:JM7WnLzlvXOGE90KKd+aigi+qUDS+U5dLwQMNpTKZxE=
5654
github.com/actiontech/java-sql-extractor v0.0.0-20231103015812-cdd5fc040f62/go.mod h1:adDZHhAf2LRMx2h0JzofPXn12x2XlyQjVE116KXquwo=
5755
github.com/actiontech/mybatis-mapper-2-sql v0.5.0 h1:TGovwZpLT+DUE5W0ZeSNE//LQLpVuQx8ghx0r8rPVBY=

sqle/api/app.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@ func StartApi(net *gracenet.Net, exitChan chan struct{}, config *config.SqleOpti
155155
// 内部调用
156156
v1Router.POST("/data_resource/handle", v1.OperateDataResourceHandle, sqleMiddleware.AdminUserAllowed())
157157
v1Router.POST(fmt.Sprintf("%s/connection", dmsV1.InternalDBServiceRouterGroup), v1.CheckInstanceIsConnectable, sqleMiddleware.AdminUserAllowed())
158+
159+
// 系统功能开关
160+
v1Router.GET("/system/module_status", v1.GetSystemModuleStatus)
158161
}
159162

160163
// project admin router
@@ -293,6 +296,11 @@ func StartApi(net *gracenet.Net, exitChan chan struct{}, config *config.SqleOpti
293296
// sql managers
294297
v2ProjectRouter.GET("/:project_name/sql_manages", v2.GetSqlManageList)
295298

299+
// sql optimization
300+
v2ProjectRouter.POST("/:project_name/sql_optimization_records", v2.SQLOptimize)
301+
v2ProjectRouter.GET("/:project_name/sql_optimization_records", v2.GetOptimizationRecords)
302+
v2ProjectRouter.GET("/:project_name/sql_optimization_records/:optimization_record_id/detail", v2.GetOptimizationSQLDetail)
303+
296304
}
297305

298306
{

sqle/api/controller/v1/instance.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -326,14 +326,15 @@ func GetInstanceSchemas(c echo.Context) error {
326326
}
327327

328328
const ( // InstanceTipReqV1.FunctionalModule Enums
329-
create_audit_plan = "create_audit_plan"
330-
create_workflow = "create_workflow"
329+
FunctionalModuleCreateAuditPlan = "create_audit_plan"
330+
FunctionalModuleCreateWorkflow = "create_workflow"
331+
FunctionalModuleCreateOptimization = "create_optimization"
331332
)
332333

333334
type InstanceTipReqV1 struct {
334335
FilterDBType string `json:"filter_db_type" query:"filter_db_type"`
335336
FilterWorkflowTemplateId uint32 `json:"filter_workflow_template_id" query:"filter_workflow_template_id"`
336-
FunctionalModule string `json:"functional_module" query:"functional_module" enums:"create_audit_plan,create_workflow,sql_manage" valid:"omitempty,oneof=create_audit_plan create_workflow sql_manage"`
337+
FunctionalModule string `json:"functional_module" query:"functional_module" enums:"create_audit_plan,create_workflow,sql_manage,create_optimization" valid:"omitempty,oneof=create_audit_plan create_workflow sql_manage create_optimization"`
337338
}
338339

339340
type InstanceTipResV1 struct {
@@ -379,10 +380,12 @@ func GetInstanceTips(c echo.Context) error {
379380

380381
var operationType v1.OpPermissionType
381382
switch req.FunctionalModule {
382-
case create_audit_plan:
383+
case FunctionalModuleCreateAuditPlan:
383384
operationType = v1.OpPermissionTypeSaveAuditPlan
384-
case create_workflow:
385+
case FunctionalModuleCreateWorkflow:
385386
operationType = v1.OpPermissionTypeCreateWorkflow
387+
case FunctionalModuleCreateOptimization:
388+
operationType = v1.OpPermissionTypeCreateOptimization
386389
default:
387390
}
388391

sqle/api/controller/v1/sql_audit_record.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,15 @@ func CreateSQLAuditRecord(c echo.Context) error {
9595
}
9696

9797
s := model.GetStorage()
98-
sqls := getSQLFromFileResp{}
98+
sqls := GetSQLFromFileResp{}
9999
user, err := controller.GetCurrentUser(c, dms.GetUser)
100100
if err != nil {
101101
return controller.JSONBaseErrorReq(c, err)
102102
}
103103
if req.Sqls != "" {
104-
sqls = getSQLFromFileResp{model.TaskSQLSourceFromFormData, []SQLsFromFile{{SQLs: req.Sqls}}}
104+
sqls = GetSQLFromFileResp{model.TaskSQLSourceFromFormData, []SQLsFromFile{{SQLs: req.Sqls}}}
105105
} else {
106-
sqls, err = getSQLFromFile(c)
106+
sqls, err = GetSQLFromFile(c)
107107
if err != nil {
108108
return controller.JSONBaseErrorReq(c, err)
109109
}
@@ -165,7 +165,7 @@ func CreateSQLAuditRecord(c echo.Context) error {
165165
})
166166
}
167167

168-
type getSQLFromFileResp struct {
168+
type GetSQLFromFileResp struct {
169169
SourceType string
170170
SQLs []SQLsFromFile
171171
}
@@ -175,7 +175,14 @@ type SQLsFromFile struct {
175175
SQLs string
176176
}
177177

178-
func addSQLsFromFileToTasks(sqls getSQLFromFileResp, task *model.Task, plugin driver.Plugin) error {
178+
func (s GetSQLFromFileResp) MergeSQLs() (sqls string) {
179+
for _, v := range s.SQLs {
180+
sqls += v.SQLs
181+
}
182+
return sqls
183+
}
184+
185+
func addSQLsFromFileToTasks(sqls GetSQLFromFileResp, task *model.Task, plugin driver.Plugin) error {
179186
var num uint = 1
180187
for _, sqlsFromOneFile := range sqls.SQLs {
181188
nodes, err := plugin.Parse(context.TODO(), sqlsFromOneFile.SQLs)
@@ -196,7 +203,7 @@ func addSQLsFromFileToTasks(sqls getSQLFromFileResp, task *model.Task, plugin dr
196203
return nil
197204
}
198205

199-
func buildOnlineTaskForAudit(c echo.Context, s *model.Storage, userId uint64, instanceName, instanceSchema, projectUid string, sqls getSQLFromFileResp) (*model.Task, error) {
206+
func buildOnlineTaskForAudit(c echo.Context, s *model.Storage, userId uint64, instanceName, instanceSchema, projectUid string, sqls GetSQLFromFileResp) (*model.Task, error) {
200207
instance, exist, err := dms.GetInstanceInProjectByName(c.Request().Context(), projectUid, instanceName)
201208
if err != nil {
202209
return nil, err
@@ -241,7 +248,7 @@ func buildOnlineTaskForAudit(c echo.Context, s *model.Storage, userId uint64, in
241248
return task, nil
242249
}
243250

244-
func buildOfflineTaskForAudit(userId uint64, dbType string, sqls getSQLFromFileResp) (*model.Task, error) {
251+
func buildOfflineTaskForAudit(userId uint64, dbType string, sqls GetSQLFromFileResp) (*model.Task, error) {
245252
task := &model.Task{
246253
CreateUserId: userId,
247254
ExecuteSQLs: []*model.ExecuteSQL{},
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package v1
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/actiontech/sqle/sqle/api/controller"
7+
"github.com/actiontech/sqle/sqle/server"
8+
9+
"github.com/labstack/echo/v4"
10+
)
11+
12+
type GetModuleStatusReqV1 struct {
13+
DbType string `json:"db_type" query:"db_type"`
14+
ModuleName string `json:"module_name" query:"module_name"`
15+
}
16+
17+
type GetModuleStatusResV1 struct {
18+
controller.BaseRes
19+
Data ModuleStatusRes `json:"data"`
20+
}
21+
22+
type ModuleStatusRes struct {
23+
IsSupported bool `json:"is_supported"`
24+
}
25+
26+
// @Summary 查询系统功能支持情况信息
27+
// @Description get module status for module in the system
28+
// @Id getSystemModuleStatus
29+
// @Tags system
30+
// @Security ApiKeyAuth
31+
// @Param db_type query string false "db type" Enums(MySQL,Oracle,TiDB,OceanBase For MySQL,PostgreSQL,DB2,SQL Server)
32+
// @Param module_name query string false "module name" Enums(execute_sql_file_mode,sql_optimization,backup,knowledge_base)
33+
// @Success 200 {object} v1.GetModuleStatusResV1
34+
// @router /v1/system/module_status [get]
35+
func GetSystemModuleStatus(c echo.Context) error {
36+
req := new(GetModuleStatusReqV1)
37+
if err := controller.BindAndValidateReq(c, req); err != nil {
38+
return err
39+
}
40+
41+
checker, err := server.NewModuleStatusChecker(req.DbType, req.ModuleName)
42+
if err != nil {
43+
return controller.JSONBaseErrorReq(c, err)
44+
}
45+
46+
return c.JSON(http.StatusOK, &GetModuleStatusResV1{
47+
BaseRes: controller.NewBaseReq(nil),
48+
Data: ModuleStatusRes{
49+
IsSupported: checker.CheckIsSupport(),
50+
},
51+
})
52+
}
53+
54+
type GetSystemModuleRedDotsRes struct {
55+
controller.BaseRes
56+
Data ModuleRedDots `json:"data"`
57+
}
58+
59+
type ModuleRedDots []ModuleRedDot
60+
61+
type ModuleRedDot struct {
62+
ModuleName string `json:"module_name" enums:"global_dashboard"`
63+
HasRedDot bool `json:"has_red_dot"`
64+
}

sqle/api/controller/v1/task.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,14 @@ const (
7777
GitPassword = "git_user_password"
7878
)
7979

80-
func getSQLFromFile(c echo.Context) (getSQLFromFileResp, error) {
80+
func GetSQLFromFile(c echo.Context) (GetSQLFromFileResp, error) {
8181
// Read it from sql file.
8282
sqlsFromSQLFile, exist, err := controller.ReadFileContent(c, InputSQLFileName)
8383
if err != nil {
84-
return getSQLFromFileResp{}, err
84+
return GetSQLFromFileResp{}, err
8585
}
8686
if exist {
87-
return getSQLFromFileResp{
87+
return GetSQLFromFileResp{
8888
SourceType: model.TaskSQLSourceFromSQLFile,
8989
SQLs: []SQLsFromFile{{SQLs: sqlsFromSQLFile}},
9090
}, nil
@@ -93,14 +93,14 @@ func getSQLFromFile(c echo.Context) (getSQLFromFileResp, error) {
9393
// If sql_file is not exist, read it from mybatis xml file.
9494
data, exist, err := controller.ReadFileContent(c, InputMyBatisXMLFileName)
9595
if err != nil {
96-
return getSQLFromFileResp{}, err
96+
return GetSQLFromFileResp{}, err
9797
}
9898
if exist {
9999
sql, err := mybatis_parser.ParseXML(data)
100100
if err != nil {
101-
return getSQLFromFileResp{}, errors.New(errors.ParseMyBatisXMLFileError, err)
101+
return GetSQLFromFileResp{}, errors.New(errors.ParseMyBatisXMLFileError, err)
102102
}
103-
return getSQLFromFileResp{
103+
return GetSQLFromFileResp{
104104
SourceType: model.TaskSQLSourceFromMyBatisXMLFile,
105105
SQLs: []SQLsFromFile{{SQLs: sql}},
106106
}, nil
@@ -109,10 +109,10 @@ func getSQLFromFile(c echo.Context) (getSQLFromFileResp, error) {
109109
// If mybatis xml file is not exist, read it from zip file.
110110
sqlsFromZip, exist, err := getSqlsFromZip(c)
111111
if err != nil {
112-
return getSQLFromFileResp{}, err
112+
return GetSQLFromFileResp{}, err
113113
}
114114
if exist {
115-
return getSQLFromFileResp{
115+
return GetSQLFromFileResp{
116116
SourceType: model.TaskSQLSourceFromZipFile,
117117
SQLs: sqlsFromZip,
118118
}, nil
@@ -121,15 +121,15 @@ func getSQLFromFile(c echo.Context) (getSQLFromFileResp, error) {
121121
// If zip file is not exist, read it from git repository
122122
sqlsFromGit, exist, err := getSqlsFromGit(c)
123123
if err != nil {
124-
return getSQLFromFileResp{}, err
124+
return GetSQLFromFileResp{}, err
125125
}
126126
if exist {
127-
return getSQLFromFileResp{
127+
return GetSQLFromFileResp{
128128
SourceType: model.TaskSQLSourceFromGitRepository,
129129
SQLs: sqlsFromGit,
130130
}, nil
131131
}
132-
return getSQLFromFileResp{}, errors.New(errors.DataInvalid, fmt.Errorf("input sql is empty"))
132+
return GetSQLFromFileResp{}, errors.New(errors.DataInvalid, fmt.Errorf("input sql is empty"))
133133
}
134134

135135
// @Summary 创建Sql扫描任务并提交审核
@@ -156,11 +156,11 @@ func CreateAndAuditTask(c echo.Context) error {
156156
if err := controller.BindAndValidateReq(c, req); err != nil {
157157
return err
158158
}
159-
var sqls getSQLFromFileResp
159+
var sqls GetSQLFromFileResp
160160
var err error
161161

162162
if req.Sql != "" {
163-
sqls = getSQLFromFileResp{
163+
sqls = GetSQLFromFileResp{
164164
SourceType: model.TaskSQLSourceFromFormData,
165165
SQLs: []SQLsFromFile{
166166
{
@@ -170,7 +170,7 @@ func CreateAndAuditTask(c echo.Context) error {
170170
},
171171
}
172172
} else {
173-
sqls, err = getSQLFromFile(c)
173+
sqls, err = GetSQLFromFile(c)
174174
if err != nil {
175175
return controller.JSONBaseErrorReq(c, err)
176176
}
@@ -760,9 +760,9 @@ func AuditTaskGroupV1(c echo.Context) error {
760760
}
761761

762762
var err error
763-
var sqls getSQLFromFileResp
763+
var sqls GetSQLFromFileResp
764764
if req.Sql != "" {
765-
sqls = getSQLFromFileResp{
765+
sqls = GetSQLFromFileResp{
766766
SourceType: model.TaskSQLSourceFromFormData,
767767
SQLs: []SQLsFromFile{
768768
{
@@ -772,7 +772,7 @@ func AuditTaskGroupV1(c echo.Context) error {
772772
},
773773
}
774774
} else {
775-
sqls, err = getSQLFromFile(c)
775+
sqls, err = GetSQLFromFile(c)
776776
if err != nil {
777777
return controller.JSONBaseErrorReq(c, err)
778778
}

0 commit comments

Comments
 (0)