package esutil import ( "bytes" "encoding/json" "errors" "fmt" "io" "io/ioutil" "net/http" "strconv" "strings" "time" 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)) if err != nil { return err, nil } req.Header.Add("Content-Type", "application/json") timeout := time.Duration(10 * time.Second) //超时时间50ms client := &http.Client{Timeout: timeout} resp, err := client.Do(req) if err != nil { return err, nil } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { return err, nil } jsonStr := string(body) var dat map[string]interface{} dec := json.NewDecoder(strings.NewReader(jsonStr)) if err := dec.Decode(&dat); err == io.EOF { return err, nil } else if err != nil { return err, nil } // 是否需要 解析 es 返回的 source if isSource { dat, ok := dat["hits"].(map[string]interface{}) if !ok { return errors.New("data is not type of map[string]interface{}"), nil } var data = make(map[string]interface{}, 2) data["total"] = dat["total"] sources := []interface{}{} for _, value := range dat["hits"].([]interface{}) { d := make(map[string]interface{}) source, ok := value.(map[string]interface{})["_source"].(map[string]interface{}) if !ok { return errors.New("value is not type of map[string]interface{}"), nil } 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 { fmt.Println("no alarmRules") 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 { 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 { ti := tiArr[0] d["picSmUrl"] = ti.PicSmUrl if ti.TargetType == "face" { sLabelStr, ok := source["showLabels"] if ok { labelArr := strings.Split(sLabelStr.(string), "/") if len(labelArr) == 3 { d["gender"] = labelArr[0] d["ageDescription"] = labelArr[1] d["race"] = labelArr[2] } } if bInfos,ok := source["baseInfo"]; ok && bInfos != nil { d["baseInfo"] = getSourceBaseInfo(bInfos) } else { d["baseInfo"] = []interface{}{} } d["sdkType"] = "人脸" } else { d["sdkType"] = source["taskName"] d["picSmUrl"] = d["picMaxUrl"] } } else { d["picSmUrl"] = d["picMaxUrl"] } vUri := source["videoUrl"] if vUri != nil && vUri.(string) != "" { d["videoNum"] = "http://"+vUri.(string) } else { d["videoNum"] = "" } sources = append(sources, d) } data["datalist"] = sources return nil, data } else { return nil, dat } } type AlarmRule struct { GroupId string `json:"groupId"` AlarmLevel string `json:"alarmLevel"` } type TargetInfo struct { TargetId string `json:"targetId"` TargetType string `json:"targetType"` PicSmUrl string `json:"picSmUrl"` TargetScore float32 `json:"targetScore"` } type BaseInfo struct { TaskId string `json:"taskId"` TaskName string `json:"taskName"` LikePer string `json:"likePer"` TableId string `json:"tableId"` TableName string `json:"tableName"` PersonId string `json:"personId"` PersonPicUrl string `json:"personPicUrl"` PersonName string `json:"personName"` Gender string `json:"gender"` PhoneNum string `json:"phoneNum"` IDCard string `json:"IDCard"` MonitorLevel string `json:"monitorLevel"` Content string `json:"content"` } type TI struct { BwType string `json:"bwType"` TargetPicUrl string `json:"targetPicUrl"` TargetName string `json:"targetName"` TargetId string `json:"targetId"` TableId string `json:"tableId"` CompareScore string `json:"compareScore"` MonitorLevel string `json:"monitorLevel"` Content string `json:"content"` TableName string `json:"tableName"` Labels string `json:"labels"` } func getSourceBaseInfo(bInfos interface{}) []BaseInfo { baseInfoArr := make([]BaseInfo,0) b, err := json.Marshal(bInfos) if err == nil { var targetArr []TI if err = json.Unmarshal(b, &targetArr); err == nil && len(targetArr) >0 { for _,t := range targetArr { idCard,sex := "","" if t.Labels != "" { arr := strings.Split(t.Labels, "/") if len(arr) > 0 { for _,str := range arr { if str == "男" || str == "女" { sex = str break } } for _,str := range arr { if len(str) == 18 { idCard = str break } } } } baseInfoArr = append(baseInfoArr, BaseInfo{ TaskId: "",//2.0新字段 TaskName: "",//2.0新字段 LikePer: t.CompareScore, TableId: "",//2.0新字段 TableName: t.TableName, PersonId: t.TargetId, PersonName: t.TargetName,//人员姓名,从管理平台获取 PersonPicUrl: t.TargetPicUrl, Gender: sex, PhoneNum: "",//手机号,从管理平台获取 IDCard: idCard, MonitorLevel: t.MonitorLevel,//2.0新字段 Content: t.Content, }) } } } return baseInfoArr } func PostAction(sec int, Eurl string, ishub string, size int, lastT time.Time, curTime time.Time) []byte { index := "ai_ocean" url := fmt.Sprintf("%s%s%s", Eurl, index, "/_search") sizeStr :="" if size <=0 { sizeStr = "1000" } else { sizeStr = strconv.Itoa(size) } preSec := "10" if sec <=0 { preSec = "10" } else { preSec = strconv.Itoa(sec) } var filterArr []string var mustNotArr []string //是否查报警数据 if ishub == "hub" { 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, ",") } 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\"]}" + "}" err, tokenRes := GetEsDataReq(url, param, true) if err != nil { log.Log.Errorln("GetEsDataReq err:", err) return nil } jsonstring, _ := json.Marshal(tokenRes) if len(jsonstring) <= 26 { return nil } return jsonstring }