Skip to content

Commit

Permalink
modify rsp by review suggestions
Browse files Browse the repository at this point in the history
	modified:   log_store.go
       modified:   model.go
  • Loading branch information
AlexStocks committed Nov 10, 2020
1 parent d5a515b commit f65be42
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
23 changes: 12 additions & 11 deletions log_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,9 +518,9 @@ func (s *LogStore) GetHistograms(topic string, from int64, to int64, queryExp st
return &getHistogramsResponse, nil
}

// GetJsonLogs query logs with [from, to) time range
// 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) ([]byte, *GetLogsResponse, error) {
maxLineNum int64, offset int64, reverse bool) (*http.Response, []byte, *GetLogsResponse, error) {

h := map[string]string{
"x-log-bodyrawsize": "0",
Expand All @@ -540,21 +540,22 @@ func (s *LogStore) getLogs(topic string, from int64, to int64, queryExp string,
uri := fmt.Sprintf("/logstores/%s?%s", s.Name, urlVal.Encode())
r, err := request(s.project, "GET", uri, h, nil)
if err != nil {
return nil, nil, NewClientError(err)
return nil, nil, nil, NewClientError(err)
}
defer r.Body.Close()

body, _ := ioutil.ReadAll(r.Body)
if r.StatusCode != http.StatusOK {
err := new(Error)
if jErr := json.Unmarshal(body, err); jErr != nil {
return nil, nil, NewBadResponseError(string(body), r.Header, r.StatusCode)
return nil, nil, nil, NewBadResponseError(string(body), r.Header, r.StatusCode)
}
return nil, nil, err
return nil, nil, nil, err
}

count, err := strconv.ParseInt(r.Header[GetLogsCountHeader][0], 10, 32)
if err != nil {
return nil, nil, err
return nil, nil, nil, err
}
var contents string
if _, ok := r.Header[GetLogsQueryInfo]; ok {
Expand All @@ -567,7 +568,7 @@ func (s *LogStore) getLogs(topic string, from int64, to int64, queryExp string,
hasSQL = true
}

return body, &GetLogsResponse{
return r, body, &GetLogsResponse{
Progress: r.Header[ProgressHeader][0],
Count: count,
Contents: contents,
Expand All @@ -579,14 +580,14 @@ func (s *LogStore) getLogs(topic string, from int64, to int64, queryExp string,
func (s *LogStore) GetLogLines(topic string, from int64, to int64, queryExp string,
maxLineNum int64, offset int64, reverse bool) (*GetLogLinesResponse, error) {

b, logRsp, err := s.getLogs(topic, from, to, queryExp, maxLineNum, offset, reverse)
rsp, b, logRsp, err := s.getLogs(topic, from, to, queryExp, maxLineNum, offset, reverse)
if err != nil {
return nil, err
}
var logs []string
err = json.Unmarshal(b, &logs)
if err != nil {
return nil, err
return nil, NewBadResponseError(string(b), rsp.Header, rsp.StatusCode)
}

lineRsp := GetLogLinesResponse{
Expand All @@ -601,12 +602,12 @@ func (s *LogStore) GetLogLines(topic string, from int64, to int64, queryExp stri
func (s *LogStore) GetLogs(topic string, from int64, to int64, queryExp string,
maxLineNum int64, offset int64, reverse bool) (*GetLogsResponse, error) {

b, logRsp, err := s.getLogs(topic, from, to, queryExp, maxLineNum, offset, reverse)
rsp, b, logRsp, err := s.getLogs(topic, from, to, queryExp, maxLineNum, offset, reverse)
if err == nil && len(b) != 0 {
logs := []map[string]string{}
err = json.Unmarshal(b, &logs)
if err != nil {
return nil, err
return nil, NewBadResponseError(string(b), rsp.Header, rsp.StatusCode)
}
logRsp.Logs = logs
}
Expand Down
3 changes: 2 additions & 1 deletion model.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ type GetLogsResponse struct {
HasSQL bool `json:"hasSQL"`
}

// GetLogLinesResponse defines response from GetJsonLogs call
// GetLogLinesResponse defines response from GetLogLines call
// note: GetLogLinesResponse.Logs is nil when use GetLogLinesResponse
type GetLogLinesResponse struct {
GetLogsResponse
Lines []string `json:"lines"`
Expand Down

0 comments on commit f65be42

Please sign in to comment.