sunty
2020-08-28 4d59ac84878d58f098d1137ad215727e064ee337
add StatisticsEveryAreaPersonsNumber GetFaceDataByTimeAndId
2个文件已修改
177 ■■■■■ 已修改文件
EsApi.go 145 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
EsClient.go 32 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
EsApi.go
@@ -256,20 +256,109 @@
    if err != nil {
        return nil, err
    }
    fmt.Println(len(source))
    faceSource := make([]map[string]interface{}, 0)
    for index, info := range source {
        if int(info["stayTime"].(float64)) > thresholdStayTime {
            faceSource = append(faceSource, source[index])
        }
    }
    //fmt.Println(len(source))
    if len(faceSource) > total {
        mapsSort := MapsSort{}
        mapsSort.Key = "endTime"
        mapsSort.MapList = faceSource
        sort.Sort(&mapsSort)
        return mapsSort.MapList[:total], nil
    }
    return faceSource, nil
}
func GetFaceDataByTimeAndIds(startTime string, endTime string, id string, thresholdTime int, thresholdStayTime int, serverIp string, serverPort string, indexName string) (resData []map[string]interface{}, err error) {
    var requestUrl = "http://" + serverIp + ":" + serverPort + "/" + indexName + "/_search"
    var requestBody = `{
    "query": {
        "bool": {
            "filter": [
                {
                    "range": {
                        "picDate": {
                            "gte": "` + startTime + `",
                             "lte": "` + endTime + `"
                        }
                    }
                },
                {
                    "term":{
                        "targetInfo.targetType.raw": "FaceDetect"
                    }
                },
                {
                    "term":{
                        "baseInfo.targetId": "` + id + `"
                    }
                }
            ]
        }
    },
    "size": 0,
    "aggs": {
        "buckets_aggs": {
            "composite": {
                "sources": [
                    {
                        "faceId": {
                            "terms": {
                                "field": "baseInfo.targetId"
                            }
                        }
                    },
                    {
                        "areaId": {
                            "terms": {
                                "field": "targetInfo.areaId"
                            }
                        }
                    }
                ],
                "size": 10000000
            },
            "aggs": {
                "top_attention_hits": {
                    "top_hits": {
                        "size": 1000000,
                        "sort": [
                            {
                                "picDate": {
                                    "order": "asc"
                                }
                            }
                        ],
                        "_source": {
                            "includes": [
                                "baseInfo.targetId",
                                "targetInfo.picSmUrl",
                                "targetInfo.areaId",
                                "picDate"
                            ]
                        }
                    }
                }
            }
        }
    }
}`
    buf, err := EsReq("POST", requestUrl, []byte(requestBody))
    if err != nil {
        return nil, err
    }
    source, err := FaceSourceAggregations(buf, thresholdTime, thresholdStayTime)
    if err != nil {
        return nil, err
    }
    faceSource := make([]map[string]interface{}, 0)
    for index, info := range source {
        if int(info["stayTime"].(float64)) > thresholdStayTime {
            faceSource = append(faceSource, source[index])
        }
    }
    return faceSource, nil
}
@@ -327,6 +416,58 @@
    return ids, nil
}
//统计各个区域人数
func StatisticsEveryAreaPersonsNumber(startTime string, endTime string, serverIp string, serverPort string, indexName string) ([]map[string]interface{},error){
    var requestUrl = "http://" + serverIp + ":" + serverPort + "/" + indexName + "/_search"
    var requestBody = `{
    "query": {
        "bool": {
            "filter": [
                {
                    "range": {
                        "picDate": {
                            "gte": "` + startTime + `",
                            "lte": "` + endTime + `"
                        }
                    }
                },
                {
                    "term": {
                        "targetInfo.targetType.raw": "Yolo"
                    }
                }
            ]
        }
    },
    "size": 0,
    "aggs": {
        "buckets_aggs": {
            "composite": {
                "sources": [
                    {
                        "areaId": {
                            "terms": {
                                "field": "targetInfo.areaId"
                            }
                        }
                    }
                ],
                "size": 10000000
            }
        }
    }
}`
    buf,err := EsReq("POST",requestUrl,[]byte(requestBody))
    if err != nil{
        return nil,err
    }
    result,err := SourceStatistics(buf)
    if err != nil{
        return nil,err
    }
    return result,nil
}
/**************************************customer analysis util end**************************************/
//根据摄像机列表和时间查询人员浏览轨迹
func GetPersonDataByCameraIdAndTime(cameraId []string, startTime string, endTime string, serverIp string, serverPort string, indexName string) (map[string]interface{}, error) {
EsClient.go
@@ -476,6 +476,7 @@
    return tmpinfos
}
func FaceSourceAggregations(buf []byte, thresholdTime int,thresholdStayTime int) (sources []map[string]interface{}, err error) {
    loc, err := time.LoadLocation("Asia/Shanghai")
    if err != nil {
@@ -571,7 +572,7 @@
            }
            tmpHitSource["areaId"] = targetInfo["areaId"].(string)
            tmpHitSource["startTime"] = sTime
            tmpHitSource["startFacePicUrl"] = targetInfo["picSmUrl"].(string)
            tmpHitSource["faceImg"] = targetInfo["picSmUrl"].(string)
            tmpHitSource["endTime"] = eTime
            tmpHitSource["stayTime"] = stayTime
            hitsSources = append(hitsSources, tmpHitSource)
@@ -580,6 +581,7 @@
    }
    return allSource, nil
}
func SourceDeduplication(buf [] byte)  ([]string,error) {
    var info interface{}
    json.Unmarshal(buf, &info)
@@ -602,6 +604,34 @@
    }
    return faceId,nil
}
//解析聚合计数结构
func SourceStatistics(buf [] byte)  ([]map[string]interface{},error) {
    var info interface{}
    json.Unmarshal(buf, &info)
    out, ok := info.(map[string]interface{})
    if !ok {
        return nil, errors.New("http response interface can not change map[string]interface{}")
    }
    middle, ok := out["aggregations"].(map[string]interface{})
    if !ok {
        return nil, errors.New("first hits change error!")
    }
    bucketsAggs := middle["buckets_aggs"].(map[string]interface{})
    buckets := bucketsAggs["buckets"].([]interface{})
    if len(buckets) == 0 {
        return nil, nil
    }
    resultData := make([]map[string]interface{},0)
    for _, pick := range buckets {
        data := make(map[string]interface{},0)
        data["areaId"] = pick.(map[string]interface{})["key"].(map[string]interface{})["areaId"].(string)
        data["peopleNum"] = int(pick.(map[string]interface{})["doc_count"].(float64))
        resultData = append(resultData, data)
    }
    return    resultData,nil
}
func SourceAggregations(buf [] byte, thresholdTime float64, queryUseTime float64) (sources map[string]interface{}, err error) {
    s := make(map[string]interface{})
    loc, err := time.LoadLocation("Asia/Shanghai")