Browse Source

fix elk bug

tags/v1.21.12.1
Gitea 4 years ago
parent
commit
e5a714fb77
1 changed files with 3 additions and 25 deletions
  1. +3
    -25
      modules/repository/elk_pagedata.go

+ 3
- 25
modules/repository/elk_pagedata.go View File

@@ -99,29 +99,8 @@ type ResultInfo struct {

//elk输出的json结构end

//发送post请求到elk
func SendReqToElk(jsonStr []byte) (content string, err error) {
ElkBase64Init := setting.ElkUser + ":" + setting.ElkPassword
ElkBase64 := base64.StdEncoding.EncodeToString([]byte(ElkBase64Init))
BasicElkBase64 := "Basic" + " " + ElkBase64
url := setting.ElkUrl
req, _ := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("kbn-version", "7.13.2")
req.Header.Set("Authorization", BasicElkBase64)

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
return string(body), err
}

//处理返回的elk数据,只保留totalView,即访问量;loaded是分片载入次数,用来判断返回的数据是否准确
func GetResultFromElk(resultinfo ResultInfo, jsonStr []byte) (loaded int, totalView int, err error) {
func GetResultFromElk(resultInfo ResultInfo, jsonStr []byte) (loaded int, totalView int, err error) {
ElkBase64Init := setting.ElkUser + ":" + setting.ElkPassword
ElkBase64 := base64.StdEncoding.EncodeToString([]byte(ElkBase64Init))
BasicElkBase64 := "Basic" + " " + ElkBase64
@@ -139,11 +118,10 @@ func GetResultFromElk(resultinfo ResultInfo, jsonStr []byte) (loaded int, totalV
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)

var resultTest ResultInfo
errs := json.Unmarshal([]byte(string(body)), &resultTest)
errs := json.Unmarshal([]byte(string(body)), &resultInfo)
fmt.Println(errs)

return resultTest.Result.Loaded, resultTest.Result.RawResponse.Hits.Total, err
return resultInfo.Result.Loaded, resultInfo.Result.RawResponse.Hits.Total, err
}

//初始化传给elk的数据结构,给定用户名和项目名,查询的起止时间,返回初始化后的结构


Loading…
Cancel
Save