From 8944afb953cad14e6157381e7e35e9640acc0c3f Mon Sep 17 00:00:00 2001 From: crimson <1291463831@qq.com> Date: Wed, 25 Dec 2024 20:19:40 +0800 Subject: [PATCH] feat: getHistogram v3 use post --- client_interface.go | 1 + client_store.go | 5 ++++ log_store.go | 47 +++++++++++++++++++++++++++++++++++++ model.go | 4 ++++ token_auto_update_client.go | 10 ++++++++ 5 files changed, 67 insertions(+) diff --git a/client_interface.go b/client_interface.go index 9ed97047..326b2c78 100644 --- a/client_interface.go +++ b/client_interface.go @@ -303,6 +303,7 @@ type ClientInterface interface { // GetHistograms query logs with [from, to) time range GetHistograms(project, logstore string, topic string, from int64, to int64, queryExp string) (*GetHistogramsResponse, error) GetHistogramsV2(project, logstore string, ghr *GetHistogramRequest) (*GetHistogramsResponse, error) + GetHistogramsV3(project, logstore string, ghr *GetHistogramRequest) (*GetHistogramsResponse, error) // GetLogs query logs with [from, to) time range GetLogs(project, logstore string, topic string, from int64, to int64, queryExp string, maxLineNum int64, offset int64, reverse bool) (*GetLogsResponse, error) diff --git a/client_store.go b/client_store.go index 9627c294..dbe94cdd 100644 --- a/client_store.go +++ b/client_store.go @@ -253,6 +253,11 @@ func (c *Client) GetHistogramsV2(project, logstore string, ghr *GetHistogramRequ return ls.GetHistogramsV2(ghr) } +func (c *Client) GetHistogramsV3(project, logstore string, ghr *GetHistogramRequest) (*GetHistogramsResponse, error) { + ls := convertLogstore(c, project, logstore) + return ls.GetHistogramsV3(ghr) +} + // GetHistogramsToCompleted query logs with [from, to) time range to completed func (c *Client) GetHistogramsToCompleted(project, logstore string, topic string, from int64, to int64, queryExp string) (*GetHistogramsResponse, error) { ls := convertLogstore(c, project, logstore) diff --git a/log_store.go b/log_store.go index fc9632d2..4edd2049 100644 --- a/log_store.go +++ b/log_store.go @@ -716,6 +716,53 @@ func (s *LogStore) GetHistogramsV2(ghr *GetHistogramRequest) (*GetHistogramsResp return &getHistogramsResponse, nil } +func (s *LogStore) GetHistogramsV3(ghr *GetHistogramRequest) (*GetHistogramsResponse, error) { + + h := map[string]string{ + "x-log-bodyrawsize": "0", + "Accept": "application/json", + "Content-Type": "application/json", + } + + reqBody, err := ghr.ToJsonBody() + if err != nil { + return nil, err + } + + uri := fmt.Sprintf("/logstores/%s/histograms", s.Name) + r, err := request(s.project, "POST", uri, h, reqBody) + if err != nil { + return 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, NewBadResponseError(string(body), r.Header, r.StatusCode) + } + return nil, err + } + + histograms := []SingleHistogram{} + err = json.Unmarshal(body, &histograms) + if err != nil { + return nil, NewBadResponseError(string(body), r.Header, r.StatusCode) + } + + count, err := strconv.ParseInt(r.Header.Get(GetLogsCountHeader), 10, 64) + if err != nil { + return nil, err + } + getHistogramsResponse := GetHistogramsResponse{ + Progress: r.Header[ProgressHeader][0], + Count: count, + Histograms: histograms, + } + + return &getHistogramsResponse, nil +} + // 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) { diff --git a/model.go b/model.go index 34081fd4..778b4c9e 100644 --- a/model.go +++ b/model.go @@ -63,6 +63,10 @@ func (ghr *GetHistogramRequest) ToURLParams() url.Values { return urlVal } +func (ghr *GetHistogramRequest) ToJsonBody() ([]byte, error) { + return json.Marshal(ghr) +} + type PullLogRequest struct { Project string Logstore string diff --git a/token_auto_update_client.go b/token_auto_update_client.go index b70be12b..78d53686 100644 --- a/token_auto_update_client.go +++ b/token_auto_update_client.go @@ -901,6 +901,16 @@ func (c *TokenAutoUpdateClient) GetHistogramsV2(project, logstore string, ghr *G return } +func (c *TokenAutoUpdateClient) GetHistogramsV3(project, logstore string, ghr *GetHistogramRequest) (h *GetHistogramsResponse, err error) { + for i := 0; i < c.maxTryTimes; i++ { + h, err = c.logClient.GetHistogramsV3(project, logstore, ghr) + if !c.processError(err) { + return + } + } + return +} + func (c *TokenAutoUpdateClient) GetHistogramsToCompleted(project, logstore string, topic string, from int64, to int64, queryExp string) (h *GetHistogramsResponse, err error) { for i := 0; i < c.maxTryTimes; i++ { h, err = c.logClient.GetHistogramsToCompleted(project, logstore, topic, from, to, queryExp)