Skip to content

Commit

Permalink
[add] power sql for get logs
Browse files Browse the repository at this point in the history
  • Loading branch information
shabicheng committed Nov 5, 2021
1 parent b66efad commit 9b54256
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 16 deletions.
3 changes: 3 additions & 0 deletions client_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ type ClientInterface interface {
GetLogLines(project, logstore string, topic string, from int64, to int64, queryExp string,
maxLineNum int64, offset int64, reverse bool) (*GetLogLinesResponse, error)

GetLogsV2(project, logstore string, req *GetLogRequest) (*GetLogsResponse, error)
GetLogLinesV2(project, logstore string, req *GetLogRequest) (*GetLogLinesResponse, error)

// #################### Index Operations #####################
// CreateIndex ...
CreateIndex(project, logstore string, index Index) error
Expand Down
12 changes: 12 additions & 0 deletions client_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,18 @@ func (c *Client) GetLogLines(project, logstore string, topic string, from int64,
return ls.GetLogLines(topic, from, to, queryExp, maxLineNum, offset, reverse)
}

// GetLogsV2 ...
func (c *Client) GetLogsV2(project, logstore string, req *GetLogRequest) (*GetLogsResponse, error) {
ls := convertLogstore(c, project, logstore)
return ls.GetLogsV2(req)
}

// GetLogLinesV2 ...
func (c *Client) GetLogLinesV2(project, logstore string, req *GetLogRequest) (*GetLogLinesResponse, error) {
ls := convertLogstore(c, project, logstore)
return ls.GetLogLinesV2(req)
}

// CreateIndex ...
func (c *Client) CreateIndex(project, logstore string, index Index) error {
ls := convertLogstore(c, project, logstore)
Expand Down
45 changes: 30 additions & 15 deletions log_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,23 +534,14 @@ func (s *LogStore) GetHistograms(topic string, from int64, to int64, queryExp st
}

// getLogs query logs with [from, to) time range
func (s *LogStore) getLogs(topic string, from int64, to int64, queryExp string,
maxLineNum int64, offset int64, reverse bool) (*http.Response, []byte, *GetLogsResponse, error) {
func (s *LogStore) getLogs(req *GetLogRequest) (*http.Response, []byte, *GetLogsResponse, error) {

h := map[string]string{
"x-log-bodyrawsize": "0",
"Accept": "application/json",
}

urlVal := url.Values{}
urlVal.Add("type", "log")
urlVal.Add("from", strconv.Itoa(int(from)))
urlVal.Add("to", strconv.Itoa(int(to)))
urlVal.Add("topic", topic)
urlVal.Add("line", strconv.Itoa(int(maxLineNum)))
urlVal.Add("offset", strconv.Itoa(int(offset)))
urlVal.Add("reverse", strconv.FormatBool(reverse))
urlVal.Add("query", queryExp)
urlVal := req.ToURLParams()

uri := fmt.Sprintf("/logstores/%s?%s", s.Name, urlVal.Encode())
r, err := request(s.project, "GET", uri, h, nil)
Expand Down Expand Up @@ -591,11 +582,24 @@ func (s *LogStore) getLogs(topic string, from int64, to int64, queryExp string,
}, nil
}

// GetJsonLogs query logs with [from, to) time range
// GetLogLines query logs with [from, to) time range
func (s *LogStore) GetLogLines(topic string, from int64, to int64, queryExp string,
maxLineNum int64, offset int64, reverse bool) (*GetLogLinesResponse, error) {

rsp, b, logRsp, err := s.getLogs(topic, from, to, queryExp, maxLineNum, offset, reverse)
var req GetLogRequest
req.Topic = topic
req.From = from
req.To = to
req.Query = queryExp
req.Lines = maxLineNum
req.Offset = offset
req.Reverse = reverse
return s.GetLogLinesV2(&req)
}

// GetLogLinesV2 query logs with [from, to) time range
func (s *LogStore) GetLogLinesV2(req *GetLogRequest) (*GetLogLinesResponse, error) {
rsp, b, logRsp, err := s.getLogs(req)
if err != nil {
return nil, err
}
Expand All @@ -616,8 +620,20 @@ func (s *LogStore) GetLogLines(topic string, from int64, to int64, queryExp stri
// GetLogs query logs with [from, to) time range
func (s *LogStore) GetLogs(topic string, from int64, to int64, queryExp string,
maxLineNum int64, offset int64, reverse bool) (*GetLogsResponse, error) {
var req GetLogRequest
req.Topic = topic
req.From = from
req.To = to
req.Query = queryExp
req.Lines = maxLineNum
req.Offset = offset
req.Reverse = reverse
return s.GetLogsV2(&req)
}

rsp, b, logRsp, err := s.getLogs(topic, from, to, queryExp, maxLineNum, offset, reverse)
// GetLogsV2 query logs with [from, to) time range
func (s *LogStore) GetLogsV2(req *GetLogRequest) (*GetLogsResponse, error) {
rsp, b, logRsp, err := s.getLogs(req)
if err == nil && len(b) != 0 {
logs := []map[string]string{}
err = json.Unmarshal(b, &logs)
Expand All @@ -626,7 +642,6 @@ func (s *LogStore) GetLogs(topic string, from int64, to int64, queryExp string,
}
logRsp.Logs = logs
}

return logRsp, err
}

Expand Down
1 change: 1 addition & 0 deletions logstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ func (s *LogstoreTestSuite) TestGetLogs() {
lResp, lErr := s.Logstore.GetLogs("", int64(beginTime), int64(endTime), "InternalServerError", 100, 0, false)
s.Nil(lErr)
s.Equal(lResp.Count, int64(logCount))
fmt.Println(*lResp)
}

func (s *LogstoreTestSuite) TestLogstore() {
Expand Down
30 changes: 29 additions & 1 deletion model.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,37 @@ package sls

import (
"encoding/json"
"net/url"
"strconv"
"strings"
)

// GetLogRequest for GetLogsV2
type GetLogRequest struct {
From int64 // unix time, eg time.Now().Unix() - 900
To int64 // unix time, eg time.Now().Unix()
Topic string // @note topic is not used anymore, use __topic__ : xxx in query instead
Lines int64 // max 100; offset, lines and reverse is ignored when use SQL in query
Offset int64
Reverse bool
Query string
PowerSQL bool
}

func (glr *GetLogRequest) ToURLParams() url.Values {
urlVal := url.Values{}
urlVal.Add("type", "log")
urlVal.Add("from", strconv.Itoa(int(glr.From)))
urlVal.Add("to", strconv.Itoa(int(glr.To)))
urlVal.Add("topic", glr.Topic)
urlVal.Add("line", strconv.Itoa(int(glr.Lines)))
urlVal.Add("offset", strconv.Itoa(int(glr.Offset)))
urlVal.Add("reverse", strconv.FormatBool(glr.Reverse))
urlVal.Add("powerSql", strconv.FormatBool(glr.PowerSQL))
urlVal.Add("query", glr.Query)
return urlVal
}

// GetHistogramsResponse defines response from GetHistograms call
type SingleHistogram struct {
Progress string `json:"progress"`
Expand Down Expand Up @@ -36,7 +64,7 @@ type GetLogsResponse struct {
// note: GetLogLinesResponse.Logs is nil when use GetLogLinesResponse
type GetLogLinesResponse struct {
GetLogsResponse
Lines []json.RawMessage
Lines []json.RawMessage
}

func (resp *GetLogsResponse) IsComplete() bool {
Expand Down
20 changes: 20 additions & 0 deletions token_auto_update_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,26 @@ func (c *TokenAutoUpdateClient) GetHistograms(project, logstore string, topic st
return
}

func (c *TokenAutoUpdateClient) GetLogsV2(project, logstore string, req *GetLogRequest) (r *GetLogsResponse, err error) {
for i := 0; i < c.maxTryTimes; i++ {
r, err = c.logClient.GetLogsV2(project, logstore, req)
if !c.processError(err) {
return
}
}
return
}

func (c *TokenAutoUpdateClient) GetLogLinesV2(project, logstore string, req *GetLogRequest) (r *GetLogLinesResponse, err error) {
for i := 0; i < c.maxTryTimes; i++ {
r, err = c.logClient.GetLogLinesV2(project, logstore, req)
if !c.processError(err) {
return
}
}
return
}

func (c *TokenAutoUpdateClient) GetLogs(project, logstore string, topic string, from int64, to int64, queryExp string,
maxLineNum int64, offset int64, reverse bool) (r *GetLogsResponse, err error) {
for i := 0; i < c.maxTryTimes; i++ {
Expand Down

0 comments on commit 9b54256

Please sign in to comment.