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 | 89 ++++++++++++++++++++++++++++++++++++++++---- 1 files changed, 80 insertions(+), 9 deletions(-) diff --git a/esutil/EsClient.go b/esutil/EsClient.go index bcf3c83..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)) @@ -27,7 +57,7 @@ resp, err := client.Do(req) if err != nil { - return err, nil + return err, nil } defer resp.Body.Close() @@ -47,7 +77,7 @@ } // 鏄惁闇�瑕� 瑙f瀽 es 杩斿洖鐨� source if isSource { - dat, ok := dat["hits"].(map[string]interface{}) + dat, ok := dat["hits"].(map[string]interface{}) if !ok { return errors.New("data is not type of map[string]interface{}"), nil } @@ -63,24 +93,45 @@ } d["id"] = value.(map[string]interface{})["_id"] - + d["picDate"] = source["picDate"] pmax, exist := source["picMaxUrl"] 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 { - continue + log.Log.Infoln("err:", err) + continue } tiArr := []TargetInfo{} err = json.Unmarshal(tB, &tiArr) if err !=nil { + log.Log.Infoln("err:", err) continue } if len(tiArr) > 0 { @@ -102,11 +153,21 @@ } else { d["baseInfo"] = []interface{}{} } + d["sdkType"] = "浜鸿劯" + } else { + 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) } @@ -115,6 +176,11 @@ } else { return nil, dat } +} + +type AlarmRule struct { + GroupId string `json:"groupId"` + AlarmLevel string `json:"alarmLevel"` } type TargetInfo struct { @@ -218,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\"],\"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) -- Gitblit v1.8.0