From ebb9bb29798f4da68d6afb9edb40ab20789d5498 Mon Sep 17 00:00:00 2001 From: sunty <1172534965@qq.com> Date: 星期五, 28 八月 2020 21:36:22 +0800 Subject: [PATCH] add ), done. --- EsClient.go | 32 ++++++++++ EsApi.go | 145 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 174 insertions(+), 3 deletions(-) diff --git a/EsApi.go b/EsApi.go index 49bd181..487198a 100644 --- a/EsApi.go +++ b/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) { diff --git a/EsClient.go b/EsClient.go index a24b0e2..70c3073 100644 --- a/EsClient.go +++ b/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 } + +//瑙f瀽鑱氬悎璁℃暟缁撴瀯 +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") -- Gitblit v1.8.0