Skip to content

Commit

Permalink
Merge pull request #2873 from actiontech/fix_sql_analysis_chart
Browse files Browse the repository at this point in the history
fix: cost趋势图代码复审问题
  • Loading branch information
BugsGuru authored Jan 15, 2025
2 parents e88332f + dd1879d commit ec85a16
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 17 deletions.
2 changes: 2 additions & 0 deletions sqle/api/controller/v1/sql_manage.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,8 @@ type SqlAnalysis struct {

type SqlAnalysisChart struct {
Points *[]ChartPoint `json:"points"`
XInfo *string `json:"x_info"`
YInfo *string `json:"y_info"`
}

type ChartPoint struct {
Expand Down
6 changes: 6 additions & 0 deletions sqle/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17376,6 +17376,12 @@ var doc = `{
"items": {
"$ref": "#/definitions/v1.ChartPoint"
}
},
"x_info": {
"type": "string"
},
"y_info": {
"type": "string"
}
}
},
Expand Down
6 changes: 6 additions & 0 deletions sqle/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -17360,6 +17360,12 @@
"items": {
"$ref": "#/definitions/v1.ChartPoint"
}
},
"x_info": {
"type": "string"
},
"y_info": {
"type": "string"
}
}
},
Expand Down
4 changes: 4 additions & 0 deletions sqle/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4076,6 +4076,10 @@ definitions:
items:
$ref: '#/definitions/v1.ChartPoint'
type: array
x_info:
type: string
y_info:
type: string
type: object
v1.SqlAnalysisResDataV1:
properties:
Expand Down
2 changes: 2 additions & 0 deletions sqle/driver/mysql/plocale/active.en.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ AllCheckPrepareStatementPlaceholdersAnnotation = "Overusing bind variables can i
AllCheckPrepareStatementPlaceholdersDesc = "The number of bound variables should not exceed the threshold"
AllCheckPrepareStatementPlaceholdersMessage = "The number of bound variables is %v, which should not exceed the set threshold %v"
AllCheckPrepareStatementPlaceholdersParams1 = "Maximum number of bind variables"
AnalysisChartXTime = "Time"
AnalysisChartYTime = "Cost"
AnalysisDescCardinality = "Cardinality"
AnalysisDescCharacterSetName = "Character set"
AnalysisDescColumnComment = "Column comment"
Expand Down
2 changes: 2 additions & 0 deletions sqle/driver/mysql/plocale/active.zh.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ AllCheckPrepareStatementPlaceholdersAnnotation = "因为过度使用绑定变量
AllCheckPrepareStatementPlaceholdersDesc = "绑定的变量个数不建议超过阈值"
AllCheckPrepareStatementPlaceholdersMessage = "使用绑定变量数量为 %v,不建议超过设定阈值 %v"
AllCheckPrepareStatementPlaceholdersParams1 = "最大绑定变量数量"
AnalysisChartXTime = "时间"
AnalysisChartYTime = "花费"
AnalysisDescCardinality = "基数"
AnalysisDescCharacterSetName = "列字符集"
AnalysisDescColumnComment = "列说明"
Expand Down
2 changes: 2 additions & 0 deletions sqle/driver/mysql/plocale/message_zh.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ var (
AnalysisDescCardinality = &i18n.Message{ID: "AnalysisDescCardinality", Other: "基数"}
AnalysisDescIndexType = &i18n.Message{ID: "AnalysisDescIndexType", Other: "索引类型"}
AnalysisDescComment = &i18n.Message{ID: "AnalysisDescComment", Other: "备注"}
AnalysisChartXTime = &i18n.Message{ID: "AnalysisChartXTime", Other: "时间"}
AnalysisChartYCost = &i18n.Message{ID: "AnalysisChartYTime", Other: "花费"}
)

// audit
Expand Down
18 changes: 9 additions & 9 deletions sqle/model/instance_audit_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -526,15 +526,15 @@ func (SqlManageMetricValue) TableName() string {

type SqlManageMetricExecutePlanRecord struct {
Model
SqlManageMetricRecordID uint `gorm:"not null"`
SelectId int `gorm:"not null"`
SelectType string `gorm:"type:varchar(50);not null"`
Table string `gorm:"type:varchar(255);not null"`
Partitions string `gorm:"type:varchar(255)"`
Type string `gorm:"type:varchar(255);not null"`
PossibleKeys string `gorm:"type:varchar(255)"`
Key string `gorm:"type:varchar(255)"`
KeyLen int
SqlManageMetricRecordID uint `gorm:"not null"`
SelectId int `gorm:"not null"`
SelectType string `gorm:"type:varchar(50);not null"`
Table string `gorm:"type:varchar(255);not null"`
Partitions string `gorm:"type:varchar(255)"`
Type string `gorm:"type:varchar(255);not null"`
PossibleKeys string `gorm:"type:varchar(255)"`
Key string `gorm:"type:varchar(255)"`
KeyLen int `gorm:"type:int"`
Ref string `gorm:"type:varchar(50)"`
Rows int `gorm:"type:int"`
Filtered float64 `gorm:"type:decimal(20,4)"`
Expand Down
3 changes: 3 additions & 0 deletions sqle/model/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ var autoMigrateList = []interface{}{
&SQLManageRecord{},
&SQLManageRecordProcess{},
&SQLManageQueue{},
&SqlManageMetricRecord{},
&SqlManageMetricValue{},
&SqlManageMetricExecutePlanRecord{},
&ReportPushConfig{},
&ReportPushConfigRecord{},
&SqlVersion{},
Expand Down
16 changes: 8 additions & 8 deletions sqle/server/auditplan/task_wrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,9 @@ func (at *TaskWrapper) pushSQLToManagerSQLQueue(sqlList []*model.SQLManageQueue,
}

for _, sqlQueue := range SqlQueueList {
err = createSqlManageMetricRecord(sqlQueue, ap.Instance)
err = createSqlManageCostMetricRecord(sqlQueue, ap.Instance)
if err != nil {
log.Logger().Errorf("createSqlManageMetricRecord: %v", err)
log.Logger().Errorf("createSqlManageCostMetricRecord: %v", err)
}
}
err = at.persist.PushSQLToManagerSQLQueue(SqlQueueList)
Expand Down Expand Up @@ -522,8 +522,8 @@ func (f BlackFilter) HasEndpointInBlackList(checkIps []string) (uint, bool) {
return 0, false
}

// createSqlManageMetricRecord 针对SELECT语句的SQL下发执行计划,并且生成对应的实体类
func createSqlManageMetricRecord(sqlManageQueue *model.SQLManageQueue, instance *model.Instance) error {
// createSqlManageCostMetricRecord 针对SELECT语句的SQL下发执行计划,并且生成对应的实体类
func createSqlManageCostMetricRecord(sqlManageQueue *model.SQLManageQueue, instance *model.Instance) error {
// 建表语句直接忽略 mysql_schema_meta
if sqlManageQueue.Source == TypeMySQLSchemaMeta {
return nil
Expand Down Expand Up @@ -561,7 +561,7 @@ func createSqlManageMetricRecord(sqlManageQueue *model.SQLManageQueue, instance
}
cost, err := strconv.ParseFloat(explainJSONResult.QueryBlock.CostInfo.QueryCost, 64)
if err != nil {
log.Logger().Errorf("createSqlManageMetricRecord: parse explain cost to float64 failed %v", err)
log.Logger().Errorf("createSqlManageCostMetricRecord: parse explain cost to float64 failed %v", err)
return err
}
storage := model.GetStorage()
Expand All @@ -573,7 +573,7 @@ func createSqlManageMetricRecord(sqlManageQueue *model.SQLManageQueue, instance
RecordEndAt: nowTime,
}
if err = storage.Create(sqlManageMetricRecord); err != nil {
log.Logger().Errorf("createSqlManageMetricRecord: create SqlManageMetricRecord error sqlId: %v", sqlManageQueue.SQLID)
log.Logger().Errorf("createSqlManageCostMetricRecord: create SqlManageMetricRecord error sqlId: %v", sqlManageQueue.SQLID)
return err
}
sqlManageMetricValue := &model.SqlManageMetricValue{
Expand All @@ -582,12 +582,12 @@ func createSqlManageMetricRecord(sqlManageQueue *model.SQLManageQueue, instance
MetricValue: cost,
}
if err = storage.Create(sqlManageMetricValue); err != nil {
log.Logger().Errorf("createSqlManageMetricRecord: create sqlManageMetricValue error sqlId: %v", sqlManageQueue.SQLID)
log.Logger().Errorf("createSqlManageCostMetricRecord: create sqlManageMetricValue error sqlId: %v", sqlManageQueue.SQLID)
return err
}
sqlManageMetricExecutePlanRecords := buildSqlManageMetricExecutePlanRecord(explainResult.ClassicResult.Rows, sqlManageMetricRecord.ID)
if err = storage.Create(sqlManageMetricExecutePlanRecords); err != nil {
log.Logger().Errorf("createSqlManageMetricRecord: create sqlManageMetricExecutePlanRecord error sqlId: %v", sqlManageQueue.SQLID)
log.Logger().Errorf("createSqlManageCostMetricRecord: create sqlManageMetricExecutePlanRecord error sqlId: %v", sqlManageQueue.SQLID)
return err
}
return nil
Expand Down

0 comments on commit ec85a16

Please sign in to comment.