From 89a3b406aabf07ba6faf0b6eb2a0c147d62b75d5 Mon Sep 17 00:00:00 2001 From: liuxiaolong <liuxiaolong@aiotlink.com> Date: 星期二, 28 七月 2020 19:08:22 +0800 Subject: [PATCH] add log --- esutil/EsClient.go | 80 ++++++++++++++++++++++++++++++++++++---- 1 files changed, 72 insertions(+), 8 deletions(-) diff --git a/esutil/EsClient.go b/esutil/EsClient.go index 88de0f4..ddadef7 100644 --- a/esutil/EsClient.go +++ b/esutil/EsClient.go @@ -1,6 +1,7 @@ package esutil import ( + "bytes" "encoding/json" "errors" "fmt" @@ -13,6 +14,35 @@ log "andriodServer/log" ) + +func EsReq(method string, url string, parama []byte) (buf []byte, err error) { + //defer elapsed("page")() + timeout := time.Duration(100 * time.Second) + client := http.Client{ + Timeout: timeout, + } + request, err := http.NewRequest(method, url, bytes.NewBuffer(parama)) + request.Header.Set("Content-type", "application/json") + + if err != nil { + fmt.Println("build request fail !") + return nil, err + } + + resp, err := client.Do(request) + if err != nil { + fmt.Println("request error: ", err) + return nil, err + } + + defer resp.Body.Close() + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + fmt.Println(err) + return nil, err + } + return body, nil +} func GetEsDataReq(url string, param string, isSource bool) (error, map[string]interface{}) { req, err := http.NewRequest("POST", url, strings.NewReader(param)) @@ -68,10 +98,29 @@ if !exist { continue } + alarmRules, exist := source["alarmRules"] + if exist { + fmt.Println("exist alarmRules") + if alarmB,ae := json.Marshal(alarmRules);ae ==nil { + var alarmRArr []AlarmRule + if ae = json.Unmarshal(alarmB, &alarmRArr);ae ==nil && len(alarmRArr) >0 { + d["alarmLevel"] = alarmRArr[0].AlarmLevel + } else { + fmt.Println("unmarshal ae:", ae) + } + } else { + fmt.Println("ae:", ae) + } + } else { + d["alarmLevel"] = "" + } pmArr := pmax.([]interface{}) if len(pmArr) > 0 { d["picMaxUrl"] = pmArr[0] + } else { + d["picMaxUrl"] = "" } + d["picAddress"] = source["cameraAddr"] tB, err := json.Marshal(source["targetInfo"]) if err != nil { @@ -106,12 +155,19 @@ } d["sdkType"] = "浜鸿劯" } else { - d["sdkType"] = source["sdkName"] + d["sdkType"] = source["taskName"] + d["picSmUrl"] = d["picMaxUrl"] } + } else { + d["picSmUrl"] = d["picMaxUrl"] } - - d["videoNum"] = source["videoUrl"] + vUri := source["videoUrl"] + if vUri != nil && vUri.(string) != "" { + d["videoNum"] = "http://"+vUri.(string) + } else { + d["videoNum"] = "" + } sources = append(sources, d) } @@ -120,6 +176,11 @@ } else { return nil, dat } +} + +type AlarmRule struct { + GroupId string `json:"groupId"` + AlarmLevel string `json:"alarmLevel"` } type TargetInfo struct { @@ -223,19 +284,24 @@ preSec = strconv.Itoa(sec) } var filterArr []string + var mustNotArr []string //鏄惁鏌ユ姤璀︽暟鎹� if ishub == "hub" { - filterArr = append(filterArr,"{\"term\":{\"alarmRules.alarmLevel.raw\":\"浜岀骇\"}}") + mustNotArr = append(mustNotArr,"{\"term\":{\"alarmRules.alarmLevel.raw\":\"浜旂骇\"}}") } filterArr = append(filterArr, "{\"range\":{\"picDate\":{\"gte\":\"now+8h-"+preSec+"s\",\"lt\":\"now+8h\"}}}") filterStr := "" + mustNotStr := "" if len(filterArr) >0 { filterStr = strings.Join(filterArr, ",") } - param := "{\"query\":{\"bool\":{\"filter\":["+filterStr+"]}},\"size\":\""+sizeStr+"\",\"sort\":[{\"picDate\":{\"order\":\"desc\"}}]," + - "\"_source\":{\"includes\":[\"baseInfo\",\"targetInfo\",\"content\",\"id\",\"picMaxUrl\",\"picDate\",\"showLabels\",\"taskName\",\"sdkName\"],\"excludes\":[\"*.feature\",\"*.attachTarget\",\"*.targetLocation\",\"alarmRules\"]}" + + if len(mustNotArr) > 0 { + mustNotStr = strings.Join(mustNotArr, ",") + } + param := "{\"query\":{\"bool\":{\"filter\":["+filterStr+"],\"must_not\":["+mustNotStr+"]}},\"size\":\""+sizeStr+"\",\"sort\":[{\"picDate\":{\"order\":\"desc\"}}]," + + "\"_source\":{\"includes\":[\"cameraAddr\",\"baseInfo\",\"targetInfo\",\"content\",\"id\",\"picMaxUrl\",\"picDate\",\"showLabels\",\"taskName\",\"sdkName\",\"videoUrl\",\"alarmRules\"],\"excludes\":[\"*.feature\",\"*.attachTarget\",\"*.targetLocation\",\"alarmRules\"]}" + "}" err, tokenRes := GetEsDataReq(url, param, true) @@ -243,9 +309,7 @@ log.Log.Errorln("GetEsDataReq err:", err) return nil } - log.Log.Infoln("tokenRes:", tokenRes) jsonstring, _ := json.Marshal(tokenRes) - log.Log.Infoln("len(result):", len(jsonstring)) if len(jsonstring) <= 26 { return nil } -- Gitblit v1.8.0