Skip to content

Commit

Permalink
Merge pull request #2883 from actiontech/feat_opt_sql_analysis_chart
Browse files Browse the repository at this point in the history
fix: api: sql_analysis add cost property
  • Loading branch information
BugsGuru authored Jan 23, 2025
2 parents c596082 + 0563ea8 commit 6de3a7a
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 5 deletions.
28 changes: 28 additions & 0 deletions sqle/api/controller/v1/analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package v1

import (
"context"
"github.com/pkg/errors"
"strconv"

"github.com/actiontech/sqle/sqle/common"
"github.com/actiontech/sqle/sqle/driver"
Expand All @@ -11,6 +13,7 @@ import (
)

type AnalysisResult struct {
Cost *float64
ExplainResult *driverV2.ExplainResult
ExplainResultErr error

Expand All @@ -35,13 +38,38 @@ func GetSQLAnalysisResult(l *logrus.Entry, instance *model.Instance, schema, sql
defer plugin.Close(context.TODO())

res = &AnalysisResult{}

res.Cost, err = GetQueryCost(plugin, sql)
res.ExplainResult, res.ExplainResultErr = Explain(instance.DbType, plugin, sql)
res.TableMetaResult, res.TableMetaResultErr = GetTableMetas(instance.DbType, plugin, sql)
res.AffectRowsResult, res.AffectRowsResultErr = GetRowsAffected(instance.DbType, plugin, sql)

return res, nil
}

func GetQueryCost(plugin driver.Plugin, sql string) (cost *float64, err error) {
explainJSONResult, err := plugin.ExplainJSONFormat(context.TODO(), &driverV2.ExplainConf{Sql: sql})
if err != nil {
return nil, err
}
sqlNodes, err := plugin.Parse(context.TODO(), sql)
if err != nil {
return nil, err
}
if len(sqlNodes) == 0 {
return nil, errors.Errorf("failed to get query cost invalid sql: %v", sql)
}
// 不是SELECT语句直接忽略
if sqlNodes[0].Type != driverV2.SQLTypeDQL {
return nil, errors.Errorf("failed to get query cost because it is not DQL: %v", sql)
}
costFloat, err := strconv.ParseFloat(explainJSONResult.QueryBlock.CostInfo.QueryCost, 64)
if err != nil {
return nil, err
}
return &costFloat, nil
}

func Explain(dbType string, plugin driver.Plugin, sql string) (res *driverV2.ExplainResult, err error) {
if !driver.GetPluginManager().
IsOptionalModuleEnabled(dbType, driverV2.OptionalModuleExplain) {
Expand Down
9 changes: 6 additions & 3 deletions sqle/api/controller/v1/sql_manage.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,9 +305,10 @@ func GetSqlManageSqlAnalysisV1(c echo.Context) error {
}

type SqlManageAnalysisChartReq struct {
StartTime *string `query:"start_time" json:"start_time"`
EndTime *string `query:"end_time" json:"end_time"`
MetricName *string `query:"metric_name" json:"metric_name"`
LatestPointEnabled bool `query:"latest_point_enabled" json:"latest_point_enabled"`
StartTime *string `query:"start_time" json:"start_time"`
EndTime *string `query:"end_time" json:"end_time"`
MetricName *string `query:"metric_name" json:"metric_name"`
}

type SqlManageAnalysisChartResp struct {
Expand All @@ -322,6 +323,7 @@ type SqlManageAnalysisChartResp struct {
// @Tags SqlManage
// @Param project_name path string true "project name"
// @Param sql_manage_id path string true "sql manage id"
// @Param latest_point_enabled query bool true "latest_point_enabled"
// @Param start_time query string true "start time"
// @Param end_time query string true "end time"
// @Param metric_name query string true "metric_name"
Expand Down Expand Up @@ -352,6 +354,7 @@ func convertSQLAnalysisResultToRes(ctx context.Context, res *AnalysisResult, raw
// explain
{
data.SQLExplain = &SQLExplain{SQL: rawSQL}
data.SQLExplain.Cost = *res.Cost
if res.ExplainResultErr != nil {
data.SQLExplain.Message = res.ExplainResultErr.Error()
} else {
Expand Down
5 changes: 3 additions & 2 deletions sqle/api/controller/v1/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -794,8 +794,9 @@ func CheckCurrentUserCanOpTask(c echo.Context, task *model.Task) (err error) {
}

type SQLExplain struct {
SQL string `json:"sql"`
Message string `json:"message"`
SQL string `json:"sql"`
Cost float64 `json:"cost"`
Message string `json:"message"`
// explain result in table format
ClassicResult ExplainClassicResult `json:"classic_result"`
}
Expand Down
10 changes: 10 additions & 0 deletions sqle/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5553,6 +5553,13 @@ var doc = `{
"in": "path",
"required": true
},
{
"type": "boolean",
"description": "latest_point_enabled",
"name": "latest_point_enabled",
"in": "query",
"required": true
},
{
"type": "string",
"description": "start time",
Expand Down Expand Up @@ -17319,6 +17326,9 @@ var doc = `{
"type": "object",
"$ref": "#/definitions/v1.ExplainClassicResult"
},
"cost": {
"type": "number"
},
"message": {
"type": "string"
},
Expand Down
10 changes: 10 additions & 0 deletions sqle/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -5537,6 +5537,13 @@
"in": "path",
"required": true
},
{
"type": "boolean",
"description": "latest_point_enabled",
"name": "latest_point_enabled",
"in": "query",
"required": true
},
{
"type": "string",
"description": "start time",
Expand Down Expand Up @@ -17303,6 +17310,9 @@
"type": "object",
"$ref": "#/definitions/v1.ExplainClassicResult"
},
"cost": {
"type": "number"
},
"message": {
"type": "string"
},
Expand Down
7 changes: 7 additions & 0 deletions sqle/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4016,6 +4016,8 @@ definitions:
$ref: '#/definitions/v1.ExplainClassicResult'
description: explain result in table format
type: object
cost:
type: number
message:
type: string
sql:
Expand Down Expand Up @@ -9763,6 +9765,11 @@ paths:
name: sql_manage_id
required: true
type: string
- description: latest_point_enabled
in: query
name: latest_point_enabled
required: true
type: boolean
- description: start time
in: query
name: start_time
Expand Down

0 comments on commit 6de3a7a

Please sign in to comment.