Skip to content

Commit 2326031

Browse files
committed
chore: implement system module status API with request/response structures and support checker logic
1 parent a09cf74 commit 2326031

File tree

4 files changed

+135
-13
lines changed

4 files changed

+135
-13
lines changed
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/cmd/sqled/sqled.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main
22

33
import (
44
"fmt"
5-
"io/ioutil"
65
"os"
76

87
dmsCommonConf "github.com/actiontech/dms/pkg/dms-common/conf"
@@ -11,7 +10,6 @@ import (
1110
"github.com/actiontech/sqle/sqle/log"
1211
"github.com/actiontech/sqle/sqle/utils"
1312
"github.com/spf13/cobra"
14-
yaml "gopkg.in/yaml.v2"
1513
)
1614

1715
var version string
@@ -81,14 +79,8 @@ func run(cmd *cobra.Command, _ []string) error {
8179

8280
// read config from file first, then read from cmd args.
8381
if configPath != "" {
84-
b, err := ioutil.ReadFile(configPath)
85-
if err != nil {
86-
return fmt.Errorf("load config path: %s failed error :%v", configPath, err)
87-
}
88-
err = yaml.Unmarshal(b, cfg)
89-
if err != nil {
90-
return fmt.Errorf("unmarshal config file error %v", err)
91-
}
82+
config.ParseConfigFile(configPath)
83+
cfg = config.GetOptions()
9284
} else {
9385
mysqlPass, err := utils.DecodeString(mysqlPass)
9486
if err != nil {

sqle/config/config.go

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,45 @@
11
package config
22

3-
import dmsCommonConf "github.com/actiontech/dms/pkg/dms-common/conf"
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
"sync"
7+
8+
dmsCommonConf "github.com/actiontech/dms/pkg/dms-common/conf"
9+
"gopkg.in/yaml.v2"
10+
)
11+
12+
var (
13+
options *Options = &Options{}
14+
once sync.Once
15+
)
16+
17+
// GetOptions 获取配置选项
18+
func GetOptions() *Options {
19+
return options
20+
}
21+
22+
func ParseConfigFile(configPath string) {
23+
once.Do(func() {
24+
b, err := ioutil.ReadFile(configPath)
25+
if err != nil {
26+
panic(fmt.Errorf("load config path: %s failed error :%v", configPath, err))
27+
}
28+
err = yaml.Unmarshal(b, options)
29+
if err != nil {
30+
panic(fmt.Errorf("unmarshal config file error %v", err))
31+
}
32+
})
33+
}
434

535
type Options struct {
636
SqleOptions SqleOptions `yaml:"sqle"`
737
}
838
type SqleOptions struct {
939
dmsCommonConf.BaseOptions `yaml:",inline"`
10-
DMSServerAddress string `yaml:"dms_server_address"`
11-
Service SeviceOpts `yaml:"service"`
40+
DMSServerAddress string `yaml:"dms_server_address"`
41+
Service SeviceOpts `yaml:"service"`
42+
OptimizationConfig OptimizationConfig `yaml:"optimization_config"`
1243
}
1344

1445
type SeviceOpts struct {
@@ -36,3 +67,8 @@ type PluginConfig struct {
3667
PluginName string `yaml:"plugin_name"`
3768
CMD string `yaml:"cmd"`
3869
}
70+
71+
type OptimizationConfig struct {
72+
OptimizationKey string `yaml:"optimization_key"`
73+
OptimizationURL string `yaml:"optimization_url"`
74+
}

sqle/server/support_checker.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package server
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/actiontech/sqle/sqle/config"
7+
)
8+
9+
const (
10+
sqlOptimization string = "sql_optimization"
11+
)
12+
13+
type StatusChecker interface {
14+
CheckIsSupport() bool
15+
}
16+
17+
func NewModuleStatusChecker(driverType string, moduleName string) (StatusChecker, error) {
18+
switch moduleName {
19+
case sqlOptimization:
20+
return sqlOptimizationChecker{}, nil
21+
}
22+
return nil, fmt.Errorf("no checker matched")
23+
}
24+
25+
type sqlOptimizationChecker struct{}
26+
27+
func (s sqlOptimizationChecker) CheckIsSupport() bool {
28+
return config.GetOptions().SqleOptions.OptimizationConfig.OptimizationKey != "" &&
29+
config.GetOptions().SqleOptions.OptimizationConfig.OptimizationURL != ""
30+
}

0 commit comments

Comments
 (0)