Skip to content

Commit 0cdc4ea

Browse files
committed
feat(api): add SQL performance insights functionality
1 parent 505a220 commit 0cdc4ea

File tree

4 files changed

+252
-3
lines changed

4 files changed

+252
-3
lines changed

sqle/api/app.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,10 @@ func StartApi(net *gracenet.Net, exitChan chan struct{}, config *config.SqleOpti
363363
v1ProjectViewRouter.POST("/:project_name/sql_manages/send", v1.SendSqlManage)
364364
v1ProjectViewRouter.GET("/:project_name/sql_manages/abnormal_audit_plan_instance", v1.GetAbnormalInstanceAuditPlans)
365365

366+
v1ProjectViewRouter.GET("/:project_name/sql_performance_insights", v1.GetSqlPerformanceInsights)
367+
v1ProjectViewRouter.GET("/:project_name/sql_performance_insights/related_sql", v1.GetSqlPerformanceInsightsRelatedSQL)
368+
v1ProjectViewRouter.GET("/:project_name/sql_performance_insights/related_sql/related_transaction", v1.GetSqlPerformanceInsightsRelatedTransaction)
369+
366370
// sql dev records
367371
v1ProjectViewRouter.GET("/:project_name/sql_dev_records", v1.GetSqlDEVRecordList)
368372

sqle/api/controller/v1/sql_manage.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,9 +275,10 @@ type SqlAnalysisChart struct {
275275
}
276276

277277
type ChartPoint struct {
278-
X *string `json:"x"`
279-
Y *float64 `json:"y"`
280-
Infos []map[string]string `json:"info"`
278+
X *string `json:"x"`
279+
Y *float64 `json:"y"`
280+
Infos []map[string]string `json:"info"`
281+
Status *int `json:"status"`
281282
}
282283

283284
type GetSqlManageSqlAnalysisResp struct {
Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
package v1
2+
3+
import (
4+
"github.com/actiontech/sqle/sqle/api/controller"
5+
"github.com/labstack/echo/v4"
6+
)
7+
8+
type GetSqlPerformanceInsightsResp struct {
9+
controller.BaseRes
10+
Data *SqlPerformanceInsights `json:"data"`
11+
}
12+
13+
type SqlPerformanceInsights struct {
14+
TaskSupport bool `json:"task_support"`
15+
TaskEnable bool `json:"task_enable"`
16+
XInfo string `json:"x_info"`
17+
YInfo string `json:"y_info"`
18+
Message string `json:"message"`
19+
Lines []Line `json:"lines"`
20+
}
21+
22+
type Line struct {
23+
LineName string `json:"line_name"`
24+
Points *[]ChartPoint `json:"points"`
25+
}
26+
27+
// 定义SQL性能洞察指标类型
28+
type MetricName string
29+
30+
const (
31+
MetricNameComprehensiveTrend MetricName = "comprehensive_trend" // 数据源综合趋势
32+
MetricNameSlowSQLTrend MetricName = "slow_sql_trend" // 慢SQL趋势
33+
MetricNameTopSQLTrend MetricName = "top_sql_trend" // TopSQL趋势
34+
MetricNameActiveSessionTrend MetricName = "active_session_trend" // 活跃会话数趋势
35+
)
36+
37+
type GetSqlPerformanceInsightsReq struct {
38+
InstanceName string `query:"instance_name" json:"instance_name" valid:"required"`
39+
StartTime string `query:"start_time" json:"start_time" valid:"required"`
40+
EndTime string `query:"end_time" json:"end_time" valid:"required"`
41+
MetricName MetricName `query:"metric_name" json:"metric_name" enums:"comprehensive_trend,slow_sql_trend,active_session_trend" valid:"required"`
42+
}
43+
44+
// GetSqlPerformanceInsights
45+
// @Summary 获取SQL管控SQL性能洞察图表数据
46+
// @Description get sql manage sql performance insights
47+
// @Id GetSqlPerformanceInsights
48+
// @Tags SqlInsight
49+
// @Param project_name path string true "project name"
50+
// @Param metric_name query string true "metric name" Enums(comprehensive_trend,slow_sql_trend,top_sql_trend,active_session_trend)
51+
// @Param start_time query string true "start time"
52+
// @Param end_time query string true "end time"
53+
// @Param instance_name query string true "instance name"
54+
// @Security ApiKeyAuth
55+
// @Success 200 {object} GetSqlPerformanceInsightsResp
56+
// @Router /v1/projects/{project_name}/sql_performance_insights [get]
57+
func GetSqlPerformanceInsights(c echo.Context) error {
58+
return getSqlPerformanceInsights(c)
59+
}
60+
61+
type GetSqlPerformanceInsightsRelatedSQLResp struct {
62+
controller.BaseRes
63+
Data []*RelatedSQLInfo `json:"data"`
64+
TotalNums uint32 `json:"total_nums"`
65+
}
66+
67+
type SqlSourceTypeEnum string
68+
69+
const (
70+
SqlSourceTypeWorkflow SqlSourceTypeEnum = "workflow"
71+
SqlSourceTypeSqlManage SqlSourceTypeEnum = "sql_manage"
72+
SqlSourceTypeWorkBench SqlSourceTypeEnum = "workbench"
73+
)
74+
75+
type RelatedSQLInfo struct {
76+
SqlFingerprint string `json:"sql_fingerprint"`
77+
Source SqlSourceTypeEnum `json:"source" enums:"workflow,sql_manage"`
78+
ExecuteTimeAvg float64 `json:"execute_time_avg"` // 平均执行时间(s)
79+
ExecuteTimeMax float64 `json:"execute_time_max"` // 最大执行时间(s)
80+
ExecuteTimeMin float64 `json:"execute_time_min"` // 最小执行时间(s)
81+
ExecuteTimeSum float64 `json:"execute_time_sum"` // 总执行时间(s)
82+
LockWaitTime float64 `json:"lock_wait_time"` // 锁等待时间(s)
83+
ExecutionTimeTrend *SqlAnalysisScatterChart `json:"execution_time_trend,omitempty"` // SQL 趋势图表
84+
}
85+
86+
// 散点图结构体,专用于SQL执行代价的散点图表示
87+
type SqlAnalysisScatterChart struct {
88+
Points *[]ScatterPoint `json:"points"`
89+
XInfo *string `json:"x_info"`
90+
YInfo *string `json:"y_info"`
91+
Message string `json:"message"`
92+
}
93+
94+
type ScatterPoint struct {
95+
Time *string `json:"time"`
96+
ExecuteTime *float64 `json:"execute_time"`
97+
SQL *string `json:"sql"`
98+
Id uint64 `json:"id"`
99+
IsInTransaction bool `json:"is_in_transaction"`
100+
Infos []map[string]string `json:"info"`
101+
}
102+
103+
type GetSqlPerformanceInsightsRelatedSQLReq struct {
104+
ProjectName string `param:"project_name" json:"project_name" valid:"required"`
105+
InstanceName string `query:"instance_name" json:"instance_name" valid:"required"`
106+
StartTime string `query:"start_time" json:"start_time" valid:"required"`
107+
EndTime string `query:"end_time" json:"end_time" valid:"required"`
108+
FilterSource SqlSourceTypeEnum `query:"filter_source" json:"filter_source,omitempty" enums:"workflow,sql_manage,workbench" valid:"required"`
109+
OrderBy *string `query:"order_by" json:"order_by,omitempty" enums:"execute_time_avg,execute_time_max,execute_time_min,execute_time_sum,lock_wait_time"`
110+
IsAsc *bool `query:"is_asc" json:"is_asc,omitempty"`
111+
PageIndex uint32 `query:"page_index" valid:"required" json:"page_index"`
112+
PageSize uint32 `query:"page_size" valid:"required" json:"page_size"`
113+
}
114+
115+
// fixme: 这个接口的设计上由于产品对于SQL指纹浮动的图表的展示情况还有疑问。所以对应的设计应该是缺失了部分。
116+
// 1、由于后续还需要查询具体sql的管理事务等。所以可能需要设计一个 id 之类的字段以便后续查询。但是现在list里的内容是sql指纹,不是具体的sql,所以这个ID要放在哪还需要等产品的结论。
117+
// 2、同样的,对于关联事物功能。目前产品的设计里,关联事物打开的是具体的SQL,而不是SQL指纹。但是又不是所有SQL都在事务里。
118+
// 2.1、所以问题一为,点击按钮的时候要打开的是哪一条具体的SQL。
119+
// 2.2、需要设计字段显示的告诉前端这条SQL是否在一个事务当中。以便前端展示 "关联事务" 按钮是否可用。
120+
// 3、这个table还有一个跳转到SQL分析的功能。但是现有的SQL分析也是针对单条SQL。而这个地方也是SQL指纹。
121+
// 3.1、而且看上去现有的SQL是基于生成了分析结果。然后这个结果有个类似于 sql_manage_id 的玩意来获取的。但是从这里跳过去就没这个id了。需要前后端讨论一下具体实现。
122+
// GetSqlPerformanceInsightsRelatedSQL
123+
// @Summary 获取sql洞察 时间选区 的关联SQL
124+
// @Description Get related SQL for the selected time range in SQL performance insights
125+
// @Id GetSqlPerformanceInsightsRelatedSQL
126+
// @Tags SqlInsight
127+
// @Param project_name path string true "project name"
128+
// @Param instance_name query string true "instance name"
129+
// @Param start_time query string true "start time"
130+
// @Param end_time query string true "end time"
131+
// @Param filter_source query string true "filter by SQL source" Enums(workflow,sql_manage,workbench)
132+
// @Param order_by query string false "order by field"
133+
// @Param is_asc query bool false "is ascending order"
134+
// @Param page_index query uint32 true "page index"
135+
// @Param page_size query uint32 true "size of per page"
136+
// @Security ApiKeyAuth
137+
// @Success 200 {object} GetSqlPerformanceInsightsRelatedSQLResp
138+
// @Router /v1/projects/{project_name}/sql_performance_insights/related_sql [get]
139+
func GetSqlPerformanceInsightsRelatedSQL(c echo.Context) error {
140+
return getSqlPerformanceInsightsRelatedSQL(c)
141+
}
142+
143+
type LockType string
144+
145+
const (
146+
LockTypeShared LockType = "SHARED" // 共享锁
147+
LockTypeExclusive LockType = "EXCLUSIVE" // 排他锁
148+
LockTypeIntentionShared LockType = "INTENTION_SHARED" // 意向共享锁
149+
LockTypeIntentionExclusive LockType = "INTENTION_EXCLUSIVE" // 意向排他锁
150+
LockTypeSharedIntentionExclusive LockType = "SHARED_INTENTION_EXCLUSIVE" // 共享意向排他锁
151+
LockTypeRowLock LockType = "ROW_LOCK" // 行锁
152+
LockTypeTableLock LockType = "TABLE_LOCK" // 表锁
153+
LockTypeMetadataLock LockType = "METADATA_LOCK" // 元数据锁
154+
)
155+
156+
type TransactionState string
157+
158+
const (
159+
TransactionStateRunning TransactionState = "RUNNING" // 执行中
160+
TransactionStateCompleted TransactionState = "COMPLETED" // 已完成
161+
)
162+
163+
type TransactionInfo struct {
164+
TransactionId string `json:"transaction_id"`
165+
LockType LockType `json:"lock_type" enums:"SHARED,EXCLUSIVE,INTENTION_SHARED,INTENTION_EXCLUSIVE,SHARED_INTENTION_EXCLUSIVE,ROW_LOCK,TABLE_LOCK,METADATA_LOCK"`
166+
TransactionStartTime string `json:"transaction_start_time"`
167+
TransactionEndTime string `json:"transaction_end_time"`
168+
TransactionDuration float64 `json:"transaction_duration"`
169+
TransactionState TransactionState `json:"transaction_state" enums:"RUNNING,COMPLETED"`
170+
}
171+
172+
type TransactionTimelineItem struct {
173+
StartTime string `json:"start_time"`
174+
Description string `json:"description"`
175+
}
176+
177+
type TransactionTimeline struct {
178+
Timeline []*TransactionTimelineItem `json:"timeline"`
179+
CurrentStepIndex int `json:"current_step_index"`
180+
}
181+
182+
// fixme: 这里同样有SQL分析功能。所以有跟上面3.1相同的问题。
183+
type TransactionSQL struct {
184+
SQL string `json:"sql"`
185+
ExecuteDuration float64 `json:"execute_duration"`
186+
LockType LockType `json:"lock_type" enums:"SHARED,EXCLUSIVE,INTENTION_SHARED,INTENTION_EXCLUSIVE,SHARED_INTENTION_EXCLUSIVE,ROW_LOCK,TABLE_LOCK,METADATA_LOCK"`
187+
}
188+
189+
type TransactionLockInfo struct {
190+
LockType LockType `json:"lock_type" enums:"SHARED,EXCLUSIVE,INTENTION_SHARED,INTENTION_EXCLUSIVE,SHARED_INTENTION_EXCLUSIVE,ROW_LOCK,TABLE_LOCK,METADATA_LOCK"`
191+
TableName string `json:"table_name"`
192+
CreateLockSQL string `json:"create_lock_sql"`
193+
}
194+
195+
type RelatedTransactionInfo struct {
196+
TransactionInfo *TransactionInfo `json:"transaction_info"`
197+
TransactionTimeline *TransactionTimeline `json:"transaction_timeline"`
198+
TransactionSQLList []*TransactionSQL `json:"related_sql_info"`
199+
TransactionLockInfo []*TransactionLockInfo `json:"transaction_lock_info"`
200+
}
201+
202+
type GetSqlPerformanceInsightsRelatedTransactionResp struct {
203+
controller.BaseRes
204+
Data *RelatedTransactionInfo `json:"data"`
205+
}
206+
207+
// GetSqlPerformanceInsightsRelatedTransaction
208+
// @Summary 获取sql洞察 相关SQL中具体一条SQL 的关联事务
209+
// @Description Get related transaction for the selected SQL in SQL performance insights
210+
// @Id GetSqlPerformanceInsightsRelatedTransaction
211+
// @Tags SqlInsight
212+
// @Param project_name path string true "project name"
213+
// @Param instance_name query string true "instance name"
214+
// @Param sql_id query string true "sql id"
215+
// @Security ApiKeyAuth
216+
// @Success 200 {object} GetSqlPerformanceInsightsRelatedTransactionResp
217+
// @Router /v1/projects/{project_name}/sql_performance_insights/related_sql/related_transaction [get]
218+
func GetSqlPerformanceInsightsRelatedTransaction(c echo.Context) error {
219+
return getSqlPerformanceInsightsRelatedTransaction(c)
220+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//go:build !enterprise
2+
// +build !enterprise
3+
4+
package v1
5+
6+
import (
7+
e "errors"
8+
"github.com/actiontech/sqle/sqle/errors"
9+
"github.com/labstack/echo/v4"
10+
)
11+
12+
var ErrCommunityEditionNotSupportSqlInsight = errors.New(errors.EnterpriseEditionFeatures, e.New("sql insight is enterprise version feature"))
13+
14+
func getSqlPerformanceInsights(c echo.Context) error {
15+
return ErrCommunityEditionNotSupportSqlInsight
16+
}
17+
18+
func getSqlPerformanceInsightsRelatedSQL(c echo.Context) error {
19+
return ErrCommunityEditionNotSupportSqlInsight
20+
}
21+
22+
func getSqlPerformanceInsightsRelatedTransaction(c echo.Context) error {
23+
return ErrCommunityEditionNotSupportSqlInsight
24+
}

0 commit comments

Comments
 (0)