Skip to content

Commit 0cda63f

Browse files
author
Cairry
committed
feat(datasource) supported preview for loki datasource and remove ruleTemplate description field
1 parent 0d109d8 commit 0cda63f

7 files changed

Lines changed: 86 additions & 25 deletions

File tree

‎api/datasource.go‎

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ func (datasourceController datasourceController) API(gin *gin.RouterGroup) {
5858
middleware.ParseTenant(),
5959
)
6060
{
61-
c.GET("promQuery", datasourceController.PromQuery)
62-
c.GET("promQueryRange", datasourceController.PromQueryRange)
63-
c.POST("dataSourcePing", datasourceController.Ping)
64-
c.POST("searchViewLogsContent", datasourceController.SearchViewLogsContent)
61+
c.POST("ping", datasourceController.Ping)
62+
c.POST("searchLogs", datasourceController.SearchLogs)
63+
c.GET("query", datasourceController.PromQuery)
64+
c.GET("queryRange", datasourceController.PromQueryRange)
6565
}
6666

6767
}
@@ -254,7 +254,7 @@ func (datasourceController datasourceController) Ping(ctx *gin.Context) {
254254
}
255255

256256
// SearchViewLogsContent Logs 数据预览
257-
func (datasourceController datasourceController) SearchViewLogsContent(ctx *gin.Context) {
257+
func (datasourceController datasourceController) SearchLogs(ctx *gin.Context) {
258258
r := new(types.RequestSearchLogsContent)
259259
BindJson(ctx, r)
260260

@@ -280,6 +280,22 @@ func (datasourceController datasourceController) SearchViewLogsContent(ctx *gin.
280280
QueryStr := string(decodedBytes)
281281

282282
switch r.Type {
283+
case provider.LokiDsProviderName:
284+
client, err = provider.NewLokiClient(datasource)
285+
if err != nil {
286+
return nil, err
287+
}
288+
289+
curAt := time.Now()
290+
startAt := curAt.Add(-r.GetLogScopeDuration())
291+
292+
options = provider.LogQueryOptions{
293+
Loki: provider.Loki{
294+
Query: QueryStr,
295+
},
296+
StartAt: startAt.Unix(),
297+
EndAt: curAt.Unix(),
298+
}
283299
case provider.VictoriaLogsDsProviderName:
284300
client, err = provider.NewVictoriaLogsClient(ctx, datasource)
285301
if err != nil {

‎internal/models/rule_template.go‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package models
22

33
type RuleTemplateGroup struct {
4-
Name string `json:"name" gorm:"type:varchar(255);not null"`
5-
Number int `json:"number"`
6-
Type string `json:"type"`
7-
Description string `json:"description"`
4+
Name string `json:"name" gorm:"type:varchar(255);not null"`
5+
Number int `json:"number"`
6+
Type string `json:"type"`
87
}
98

109
type RuleTemplate struct {
@@ -15,7 +14,6 @@ type RuleTemplate struct {
1514
EvalInterval int64 `json:"evalInterval"`
1615
ForDuration int64 `json:"forDuration"`
1716
RepeatNoticeInterval int64 `json:"repeatNoticeInterval"`
18-
Description string `json:"description"`
1917
PrometheusConfig PrometheusConfig `json:"prometheusConfig" gorm:"prometheusConfig;serializer:json"`
2018
AliCloudSLSConfig AliCloudSLSConfig `json:"alicloudSLSConfig" gorm:"alicloudSLSConfig;serializer:json"`
2119
LokiConfig LokiConfig `json:"lokiConfig" gorm:"lokiConfig;serializer:json"`

‎internal/services/rule_tmpl.go‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ func (rt ruleTmplService) Create(req interface{}) (interface{}, interface{}) {
5050
EvalInterval: r.EvalInterval,
5151
ForDuration: r.ForDuration,
5252
RepeatNoticeInterval: r.RepeatNoticeInterval,
53-
Description: r.Description,
5453
PrometheusConfig: r.PrometheusConfig,
5554
AliCloudSLSConfig: r.AliCloudSLSConfig,
5655
LokiConfig: r.LokiConfig,
@@ -77,7 +76,6 @@ func (rt ruleTmplService) Update(req interface{}) (interface{}, interface{}) {
7776
EvalInterval: r.EvalInterval,
7877
ForDuration: r.ForDuration,
7978
RepeatNoticeInterval: r.RepeatNoticeInterval,
80-
Description: r.Description,
8179
PrometheusConfig: r.PrometheusConfig,
8280
AliCloudSLSConfig: r.AliCloudSLSConfig,
8381
LokiConfig: r.LokiConfig,

‎internal/services/rule_tmpl_group.go‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,8 @@ func (rtg ruleTmplGroupService) List(req interface{}) (interface{}, interface{})
4343
func (rtg ruleTmplGroupService) Create(req interface{}) (interface{}, interface{}) {
4444
r := req.(*types.RequestRuleTemplateGroupCreate)
4545
err := rtg.ctx.DB.RuleTmplGroup().Create(models.RuleTemplateGroup{
46-
Name: r.Name,
47-
Type: r.Type,
48-
Description: r.Description,
46+
Name: r.Name,
47+
Type: r.Type,
4948
})
5049
if err != nil {
5150
return nil, err
@@ -57,9 +56,8 @@ func (rtg ruleTmplGroupService) Create(req interface{}) (interface{}, interface{
5756
func (rtg ruleTmplGroupService) Update(req interface{}) (interface{}, interface{}) {
5857
r := req.(*types.RequestRuleTemplateGroupUpdate)
5958
err := rtg.ctx.DB.RuleTmplGroup().Update(models.RuleTemplateGroup{
60-
Name: r.Name,
61-
Type: r.Type,
62-
Description: r.Description,
59+
Name: r.Name,
60+
Type: r.Type,
6361
})
6462
if err != nil {
6563
return nil, err

‎internal/types/datasource.go‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,15 @@ type RequestSearchLogsContent struct {
9797
DatasourceId string `json:"datasourceId"`
9898
Index string `json:"index"`
9999
Query string `json:"query"`
100+
LogScope int `json:"logScope"` // 查询时间范围(分钟),用于 Loki/AliCloudSLS 等需要时间区间的数据源
101+
}
102+
103+
// GetLogScopeDuration 获取查询时间范围,如果未传或非法则默认为 15 分钟
104+
func (requestSearchLogsContent RequestSearchLogsContent) GetLogScopeDuration() time.Duration {
105+
if requestSearchLogsContent.LogScope <= 0 {
106+
return 15 * time.Minute
107+
}
108+
return time.Duration(requestSearchLogsContent.LogScope) * time.Minute
100109
}
101110

102111
func (requestSearchLogsContent RequestSearchLogsContent) GetElasticSearchIndexName() string {

‎internal/types/ruleTmpl.go‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ type RequestRuleTemplateCreate struct {
1010
EvalInterval int64 `json:"evalInterval"`
1111
ForDuration int64 `json:"forDuration"`
1212
RepeatNoticeInterval int64 `json:"repeatNoticeInterval"`
13-
Description string `json:"description"`
1413
PrometheusConfig models.PrometheusConfig `json:"prometheusConfig"`
1514
AliCloudSLSConfig models.AliCloudSLSConfig `json:"alicloudSLSConfig"`
1615
LokiConfig models.LokiConfig `json:"lokiConfig"`
@@ -29,7 +28,6 @@ type RequestRuleTemplateUpdate struct {
2928
EvalInterval int64 `json:"evalInterval"`
3029
ForDuration int64 `json:"forDuration"`
3130
RepeatNoticeInterval int64 `json:"repeatNoticeInterval"`
32-
Description string `json:"description"`
3331
PrometheusConfig models.PrometheusConfig `json:"prometheusConfig"`
3432
AliCloudSLSConfig models.AliCloudSLSConfig `json:"alicloudSLSConfig"`
3533
LokiConfig models.LokiConfig `json:"lokiConfig"`

‎pkg/provider/logs_loki.go‎

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,48 @@ type Result struct {
4545
Values []interface{} `json:"values"`
4646
}
4747

48+
// parseLokiTimeParam 将 LogQueryOptions.StartAt/EndAt 转换为 Unix 时间戳(秒)。
49+
// 支持 int64/int32/int(Unix 秒)以及字符串(Unix 秒数字符串或 RFC3339Nano 格式)。
50+
// 当传入值为空或无法解析时,返回 (0, false)。
51+
func parseLokiTimeParam(v interface{}) (int64, bool) {
52+
switch val := v.(type) {
53+
case int64:
54+
if val == 0 {
55+
return 0, false
56+
}
57+
return val, true
58+
case int32:
59+
if val == 0 {
60+
return 0, false
61+
}
62+
return int64(val), true
63+
case int:
64+
if val == 0 {
65+
return 0, false
66+
}
67+
return int64(val), true
68+
case string:
69+
if val == "" {
70+
return 0, false
71+
}
72+
// 尝试解析为 Unix 时间戳字符串
73+
if unix, err := strconv.ParseInt(val, 10, 64); err == nil {
74+
return unix, true
75+
}
76+
// 尝试解析为 RFC3339Nano 格式
77+
if t, err := time.Parse(time.RFC3339Nano, val); err == nil {
78+
return t.Unix(), true
79+
}
80+
// 尝试解析为 RFC3339 格式
81+
if t, err := time.Parse(time.RFC3339, val); err == nil {
82+
return t.Unix(), true
83+
}
84+
return 0, false
85+
default:
86+
return 0, false
87+
}
88+
}
89+
4890
func (l LokiProvider) Query(options LogQueryOptions) (Logs, int, error) {
4991
curTime := time.Now()
5092

@@ -60,16 +102,18 @@ func (l LokiProvider) Query(options LogQueryOptions) (Logs, int, error) {
60102
options.Loki.Limit = 100
61103
}
62104

63-
if options.StartAt == "" {
105+
startAt, ok := parseLokiTimeParam(options.StartAt)
106+
if !ok {
64107
duration, _ := time.ParseDuration(strconv.Itoa(1) + "h")
65-
options.StartAt = curTime.Add(-duration).Format(time.RFC3339Nano)
108+
startAt = curTime.Add(-duration).Unix()
66109
}
67110

68-
if options.EndAt == "" {
69-
options.EndAt = curTime.Format(time.RFC3339Nano)
111+
endAt, ok := parseLokiTimeParam(options.EndAt)
112+
if !ok {
113+
endAt = curTime.Unix()
70114
}
71115

72-
args := fmt.Sprintf("/loki/api/v1/query_range?query=%s&direction=%s&limit=%d&start=%d&end=%d", url.QueryEscape(options.Loki.Query), options.Loki.Direction, options.Loki.Limit, options.StartAt.(int64), options.EndAt.(int64))
116+
args := fmt.Sprintf("/loki/api/v1/query_range?query=%s&direction=%s&limit=%d&start=%d&end=%d", url.QueryEscape(options.Loki.Query), options.Loki.Direction, options.Loki.Limit, startAt, endAt)
73117
requestURL := l.Url + args
74118

75119
var headers = make(map[string]string)

0 commit comments

Comments
 (0)