diff --git a/modules/repository/elk_pagedata.go b/modules/repository/elk_pagedata.go index 03a61be1f..1454f0364 100644 --- a/modules/repository/elk_pagedata.go +++ b/modules/repository/elk_pagedata.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/base64" "encoding/json" + "fmt" "io/ioutil" "net/http" @@ -113,13 +114,23 @@ func GetResultFromElk(resultInfo ResultInfo, jsonStr []byte) (loaded int, totalV client := &http.Client{} resp, err := client.Do(req) if err != nil { - panic(err) + // panic(err) + return 0, 0, err } defer resp.Body.Close() - body, _ := ioutil.ReadAll(resp.Body) - - errs := json.Unmarshal([]byte(string(body)), &resultInfo) - log.Info("Get resultJson failed", errs) + if resp.StatusCode != 200 { + log.Error("ConnectToElk failed:%s", resp.Status) + return 0, 0, fmt.Errorf("ConnectToElk failed:%s", resp.Status) + } + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return 0, 0, err + } + err = json.Unmarshal([]byte(string(body)), &resultInfo) + if err != nil { + log.Error("Get resultJson failed", err) + return 0, 0, err + } return resultInfo.Result.Loaded, resultInfo.Result.RawResponse.Hits.Total, err } @@ -138,6 +149,7 @@ func ProjectViewInit(User string, Project string, Gte string, Lte string) (proje var timeRange Range timeRange.Timestamptest.Gte = Gte timeRange.Timestamptest.Lte = Lte + timeRange.Timestamptest.Format = "strict_date_optional_time" inputStruct.Batch[0].Request.Params.Body.Query.BoolIn.Filter[0].Range = &timeRange //限定用户 var userName FilterMatchPhrase @@ -208,9 +220,10 @@ func TagNameInit(MessageInfo string, Tagname string, Gte string, Lte string) (pr //向elk发送请求,将获取的结果只保留访问量,输入是初始化后的数据结构,返回访问量 func ViewInfo(viewInfo InputInfo) (totalView int, err error) { - jsons, errs := json.Marshal(viewInfo) - if errs != nil { - log.Info("errs:", errs) + jsons, err := json.Marshal(viewInfo) + if err != nil { + log.Error("jsons failed", err) + return 0, err } var jsonStr = []byte(jsons) var resultInfo ResultInfo @@ -221,7 +234,6 @@ func ViewInfo(viewInfo InputInfo) (totalView int, err error) { if loaded == 0 { loaded_next, totalView, err := GetResultFromElk(resultInfo, jsonStr) time++ - log.Info("time:", time) if loaded_next != 0 && time < 100 { return totalView, err }