From 6f10f72b074455ba473f82a20b76fa20452a4114 Mon Sep 17 00:00:00 2001
From: sunty <1172534965@qq.com>
Date: 星期四, 09 五月 2024 09:53:07 +0800
Subject: [PATCH] add AnalyzeCoordinatedMovements 同行目标按照档案编号分组

---
 EsApi.go | 1735 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 1,678 insertions(+), 57 deletions(-)

diff --git a/EsApi.go b/EsApi.go
index 6f19930..c33e153 100644
--- a/EsApi.go
+++ b/EsApi.go
@@ -1,15 +1,15 @@
 package esutil
 
 import (
+	"basic.com/pubsub/protomsg.git"
 	"encoding/json"
 	"errors"
 	"fmt"
+	"sort"
 	"strconv"
 	"strings"
 	"sync"
 	"time"
-
-	"basic.com/pubsub/protomsg.git"
 )
 
 var logPrint = func(i ...interface{}) {
@@ -22,6 +22,774 @@
 	}
 }
 
+//***********************閲嶅簡Start**********************************//
+
+type activeHourFormat struct {
+	startTime string
+	endTime   string
+	startHour int
+	endHour   int
+}
+
+//鎸夐渶姹�(activeHourFormat缁撴瀯浣�)鏍煎紡鍖栨椂闂存暟鎹�
+func formatActiveHour(activeHour string) (activeHourFormat, error) {
+	hours := strings.Split(activeHour, "-")
+
+	if len(hours) == 2 {
+		startHour := hours[0]
+		endHour := hours[1]
+
+		// 瑙f瀽寮�濮嬫椂闂寸殑灏忔椂鍜屽垎閽�
+		startParts := strings.Split(startHour, ":")
+		startHourInt, _ := strconv.Atoi(startParts[0])
+
+		// 瑙f瀽缁撴潫鏃堕棿鐨勫皬鏃跺拰鍒嗛挓
+		endParts := strings.Split(endHour, ":")
+		endHourInt, _ := strconv.Atoi(endParts[0])
+
+		// 杈撳嚭寮�濮嬫椂闂寸殑灏忔椂
+		//fmt.Println("寮�濮嬫椂闂寸殑灏忔椂:", startHourInt)
+
+		// 杈撳嚭缁撴潫鏃堕棿鐨勫皬鏃� + 1
+		endHourPlusOne := (endHourInt + 1) % 24 // 鍙栦綑纭繚涓嶈秴杩�24灏忔椂
+		//fmt.Println("缁撴潫鏃堕棿鐨勫皬鏃� + 1:", endHourPlusOne)
+		activeHourFormat := activeHourFormat{startTime: startHour, endTime: endHour, startHour: startHourInt, endHour: endHourPlusOne}
+		return activeHourFormat, nil
+	}
+	return activeHourFormat{}, errors.New("閿欒锛氭棤娉曡В鏋愬紑濮嬫椂闂村拰缁撴潫鏃堕棿")
+
+}
+
+//鍒ゆ柇鏃堕棿鏄惁鍐嶈寖鍥翠箣鍐�
+func isTimeInRange(timeStr, startStr, endStr string) bool {
+	layout := "15:04:05"
+
+	timeStamp, err := time.Parse(layout, timeStr)
+	if err != nil {
+		fmt.Println("Error parsing timestamp:", err)
+		return false
+	}
+
+	startTime, err := time.Parse(layout, startStr)
+	if err != nil {
+		fmt.Println("Error parsing start time:", err)
+		return false
+	}
+
+	endTime, err := time.Parse(layout, endStr)
+	if err != nil {
+		fmt.Println("Error parsing end time:", err)
+		return false
+	}
+
+	if startTime.After(endTime) {
+		// 璺ㄨ秺鏃ユ湡鐨勬儏鍐�
+		//fmt.Println("璺ㄦ棩鏈�",timeStamp, timeStamp.After(startTime), timeStamp.Before(endTime))
+		return timeStamp.After(startTime) || timeStamp.Before(endTime)
+	} else {
+		// 涓嶈法瓒婃棩鏈熺殑鎯呭喌
+		//fmt.Println("涓嶈法鏃ユ湡",timeStamp, timeStamp.After(startTime), timeStamp.Before(endTime))
+		return timeStamp.After(startTime) && timeStamp.Before(endTime)
+	}
+}
+
+//鍒ゆ柇涓や釜鏃堕棿鍏堝悗
+func compareTimes(time1Str, time2Str string) int {
+	layout := "15:04:05"
+
+	time1, err := time.Parse(layout, time1Str)
+	if err != nil {
+		fmt.Println("Error parsing time 1:", err)
+		return 0
+	}
+
+	time2, err := time.Parse(layout, time2Str)
+	if err != nil {
+		fmt.Println("Error parsing time 2:", err)
+		return 0
+	}
+
+	if time1.Before(time2) {
+		return -1 // time1 鍦� time2 涔嬪墠
+	} else if time1.After(time2) {
+		return 1 // time1 鍦� time2 涔嬪悗
+	} else {
+		return 0 // time1 鍜� time2 鐩哥瓑
+	}
+}
+
+//鍒ゆ柇鏃ユ湡鐩稿樊鍑犲ぉ
+func daysBetweenDates(date1Str, date2Str string) int {
+	layout := "2006-01-02"
+
+	date1, err := time.Parse(layout, date1Str)
+	if err != nil {
+		fmt.Println("Error parsing date 1:", err)
+		return 0
+	}
+
+	date2, err := time.Parse(layout, date2Str)
+	if err != nil {
+		fmt.Println("Error parsing date 2:", err)
+		return 0
+	}
+
+	duration := date2.Sub(date1)
+	days := int(duration.Hours() / 24)
+
+	return days
+}
+
+//璁$畻鏃堕棿闃堝��
+func checkTimeDifference(timestampStr1 string, timestampStr2 string, intervalInMinutes int) bool {
+	layout := "2006-01-02 15:04:05"
+	timestampStr1 = timestampStr1[:19]
+	timestampStr2 = timestampStr2[:19]
+	// 灏嗗瓧绗︿覆瑙f瀽涓烘椂闂�
+	time1, err := time.Parse(layout, timestampStr1)
+	if err != nil {
+		fmt.Println("鏃堕棿瑙f瀽澶辫触:", err)
+		return false
+	}
+	time2, err := time.Parse(layout, timestampStr2)
+	if err != nil {
+		fmt.Println("鏃堕棿瑙f瀽澶辫触:", err)
+		return false
+	}
+
+	// 璁$畻鏃堕棿宸�
+	diff := time2.Sub(time1)
+
+	// 妫�鏌ユ椂闂村樊鏄惁灏忎簬绛変簬鎸囧畾鐨勯棿闅�
+	if diff.Minutes() <= float64(intervalInMinutes) {
+		return true
+	} else {
+		return false
+	}
+}
+
+////鏍煎紡鍖栨椂闂磆h:mm:ss:zzz to hh:mm:ss
+//func formatTime(inputTime string) (string, error) {
+//	parsedTime, err := time.Parse("15:04:05:000", inputTime)
+//	if err != nil {
+//		return "", err
+//	}
+//
+//	formattedTime := parsedTime.Format("15:04:05")
+//	return formattedTime, nil
+//}
+func resetDataId(dataId []string, id, dDate, dTime string, sDate *string, sTime *string) []string {
+	dataId = make([]string, 0)
+	*sDate = dDate
+	*sTime = dTime
+	dataId = append(dataId, id)
+	return dataId
+}
+
+func decodeActivityId(aHFormat activeHourFormat, frequency int, intervalInMinutes int, source []map[string]interface{}) ([]map[string]interface{}, error) {
+	docInfo := make([]map[string]interface{}, 0)
+	for _, info := range source {
+		tmpInfo := make(map[string]interface{})
+		activeId := make([]string, 0)
+		sDate := ""
+		sTime := ""
+		documentNumber := info["key"].(string)
+		tmpInfo["documentNumber"] = documentNumber
+		//fmt.Println("documentNumber: ", documentNumber)
+		topHits := info["top_hits"].(map[string]interface{})
+		hits := topHits["hits"].(map[string]interface{})
+		hitsResult := hits["hits"].([]interface{})
+		dataId := make([]string, 0)
+		picUrl := ""
+		if hitsResult[0].(map[string]interface{})["_source"].(map[string]interface{})["baseInfo"] != nil {
+			baseInfo := hitsResult[0].(map[string]interface{})["_source"].(map[string]interface{})["baseInfo"]
+			if v, ok := baseInfo.([]interface{}); ok {
+				picUrl = v[0].(map[string]interface{})["targetPicUrl"].(string)
+			} else if v, ok := baseInfo.(map[string]interface{}); ok {
+				picUrl = v["targetPicUrl"].(string)
+			}
+		} else {
+			if hitsResult[0].(map[string]interface{})["_source"].(map[string]interface{})["targetInfo"] != nil {
+				picUrl = hitsResult[0].(map[string]interface{})["_source"].(map[string]interface{})["targetInfo"].([]interface{})[0].(map[string]interface{})["picSmUrl"].(string)
+			}
+		}
+
+		//if hitsResult[0].(map[string]interface{})["baseInfo"] != nil {
+		//	fmt.Println("picUrl1: ", picUrl)
+		//	picUrl = hitsResult[0].(map[string]interface{})["baseInfo"].([]interface{})[0].(map[string]interface{})["targetPicUrl"].(string)
+		//} else {
+		//	if hitsResult[0].(map[string]interface{})["targetInfo"] != nil {
+		//		fmt.Println("picUrl2: ", picUrl)
+		//		picUrl = hitsResult[0].(map[string]interface{})["targetInfo"].([]interface{})[0].(map[string]interface{})["picSmUrl"].(string)
+		//	}
+		//}
+		tmpInfo["picUrl"] = picUrl
+		for sIndex, sourceInfo := range hitsResult {
+			rSourceInfo := sourceInfo.(map[string]interface{})
+			source := rSourceInfo["_source"].(map[string]interface{})
+			captureTime := source["picDate"].(string)
+			dDate := strings.Split(captureTime, " ")[0]
+			dTime := strings.Split(captureTime[:19], " ")[1]
+			id := source["id"].(string)
+			//fmt.Println("sindex: ", sIndex, "documentNumber", tmpInfo["documentNumber"], "id: ", id, "captureTime: ", captureTime)
+			if !isTimeInRange(dTime, aHFormat.startTime, aHFormat.endTime) {
+				//if sDate != "" && len(dataId) >= frequency {
+				//	activeId = append(activeId, dataId...)
+				//	dataId = resetDataId(dataId, id, dDate, dTime, &sDate, &sTime)
+				//}
+				continue
+			}
+			if sDate == "" {
+				sDate = dDate
+				sTime = dTime
+				dataId = append(dataId, id)
+				if len(dataId) >= frequency {
+					activeId = append(activeId, dataId...)
+					dataId = resetDataId(dataId, id, dDate, dTime, &sDate, &sTime)
+				}
+				continue
+			}
+			if checkTimeDifference(sDate+" "+sTime, captureTime[:19], intervalInMinutes) {
+				if len(dataId) >= frequency {
+					activeId = append(activeId, dataId...)
+					dataId = resetDataId(dataId, id, dDate, dTime, &sDate, &sTime)
+				}
+				continue
+			}
+			//fmt.Println(daysBetweenDates(sDate, dDate))
+			if aHFormat.startHour < aHFormat.endHour && daysBetweenDates(sDate, dDate) == 0 {
+				dataId = append(dataId, id)
+			} else if aHFormat.startHour > aHFormat.endHour {
+				if daysBetweenDates(sDate, dDate) == 0 {
+					if compareTimes(dTime, aHFormat.startTime) == compareTimes(sTime, aHFormat.startTime) {
+						//	||compareTimes(dTime,aHFormat.endTime) == compareTimes(sTime, aHFormat.endTime){
+						dataId = append(dataId, id)
+					}
+				} else if daysBetweenDates(sDate, dDate) == 1 {
+					//鍒濆鏃堕棿鎴冲湪缁撴潫鑼冨洿涔嬪墠
+					if compareTimes(sTime, aHFormat.endTime) == -1 {
+						if len(dataId) >= frequency {
+							activeId = append(activeId, dataId...)
+							dataId = resetDataId(dataId, id, dDate, dTime, &sDate, &sTime)
+						}
+						//鍒濆鏃堕棿鎴冲湪寮�濮嬭寖鍥翠箣鍚�
+					} else if compareTimes(sTime, aHFormat.endTime) == 1 {
+						//next鏃堕棿鎴冲湪缁撴潫鑼冨洿涔嬪墠
+						if compareTimes(dTime, aHFormat.endTime) == -1 {
+							dataId = append(dataId, id)
+							//next鏃堕棿鎴冲湪寮�濮嬭寖鍥翠箣鍚�
+						} else if compareTimes(dTime, aHFormat.startTime) == 1 {
+							if len(dataId) >= frequency {
+								activeId = append(activeId, dataId...)
+								dataId = resetDataId(dataId, id, dDate, dTime, &sDate, &sTime)
+							}
+						}
+					}
+				} else if daysBetweenDates(sDate, dDate) >= 1 {
+					//fmt.Println(len(dataId))
+					if len(dataId) >= frequency {
+						activeId = append(activeId, dataId...)
+						dataId = resetDataId(dataId, id, dDate, dTime, &sDate, &sTime)
+					}
+				}
+			}
+			if sIndex == len(hitsResult)-1 {
+				if len(dataId) >= frequency {
+					activeId = append(activeId, dataId...)
+				}
+			}
+		}
+		if len(activeId) > 0 {
+			tmpInfo["id"] = activeId
+			docInfo = append(docInfo, tmpInfo)
+		}
+	}
+	return docInfo, nil
+}
+
+func DayNightActivityQuery(comIds []string, docNumber string, startTime string, endTime string, activeHour string, frequency int,
+	intervalInMinutes int, indexName string, serverIp string, serverPort string) (map[string]interface{}, error) {
+	esURL := "http://" + serverIp + ":" + serverPort + "/" + indexName + "/_search"
+
+	aHFormat, err := formatActiveHour(activeHour)
+	if err != nil {
+		return nil, err
+	}
+	filterDocIdAttr := ""
+	if docNumber != "" {
+		filterDocIdAttr = "{\"term\": {\"documentNumber\": \"" + docNumber + "\"}},"
+	}
+	comIdsStr := ""
+	if comIds == nil || len(comIds) > 0 {
+		esComIds := strings.Replace(strings.Trim(fmt.Sprint(comIds), "[]"), " ", "\",\"", -1)
+		comIdsStr = "{\"terms\":{\"communityId\":[\"" + esComIds + "\"]}},"
+	}
+	queryDSL := `
+	{
+	    "size": 0,
+	    "query": {
+	        "bool": {
+	            "filter": [
+	                {
+	                    "range": {
+	                        "picDate": {
+	                            "gte": "` + startTime + `",
+	                            "lt": "` + endTime + `"
+	                        }
+	                    }
+	                },
+					` + filterDocIdAttr + `
+	    			` + comIdsStr + `
+	                {
+	                    "script": {
+	                        "script": {
+	                            "source": "doc['picDate'].value.hourOfDay >= ` + strconv.Itoa(aHFormat.startHour) + ` || doc['picDate'].value.hourOfDay < ` + strconv.Itoa(aHFormat.endHour) + `",
+	                            "lang": "painless"
+	                        }
+	                    }
+	                }
+	            ],
+	            "must_not": [
+	                {
+	                    "term": {
+	                        "documentNumber": ""
+	                    }
+	                }
+	            ]
+	        }
+	    },
+	    "aggs": {
+	        "group_by_documentnumber": {
+	            "terms": {
+	                "field": "documentNumber",
+	                "size": 100000
+	            },
+				"aggs": {
+					"top_hits": {
+	                	"top_hits": {
+	                    	"_source": [
+								"id",
+	                        	"picDate",
+								"baseInfo.targetPicUrl",
+								"targetInfo.picSmUrl"
+							],
+	                        "size": 100000,
+	                        "sort": [
+	                            {
+	                                "picDate": {
+	                                    "order": "asc"
+	                                }
+	                            }
+	                        ]
+						}
+					}
+	            }
+	        }
+	    }
+	}`
+	//fmt.Println(esURL)
+	//fmt.Println(queryDSL)
+	var result = make(map[string]interface{})
+	buf, err := EsReq("POST", esURL, []byte(queryDSL))
+	if err != nil {
+		return nil, err
+	}
+	source, err := SourceAggregationList(buf)
+	if err != nil {
+		return nil, err
+	}
+	//fmt.Println(source)
+	docResult, err := decodeActivityId(aHFormat, frequency, intervalInMinutes, source)
+	if err != nil {
+		return nil, err
+	}
+	//result, _ := decodeDocumentInfos(source)
+	//return result, nil
+	if len(docResult) == 0 {
+		return result, nil
+	}
+	DataInfos, err := GetInfosByIds(docResult[0]["id"].([]string), indexName, serverIp, serverPort)
+	result["documentNumbers"] = docResult
+	result["datalist"] = DataInfos
+	return result, nil
+}
+
+type acmInfo struct {
+	documentNumber string
+	camerasInfos   []camerasInfo
+}
+
+type camerasInfo struct {
+	cameraId     string
+	captureInfos []captureInfo
+}
+
+type captureInfo struct {
+	id      string
+	picDate string
+}
+
+func addSecondsToTimestamp(timestamp string, seconds int) (string, error) {
+	parsedTime, err := time.Parse("2006-01-02 15:04:05", timestamp)
+	if err != nil {
+		return "", err
+	}
+	newTime := parsedTime.Add(time.Second * time.Duration(seconds))
+	newTimestamp := newTime.Format("2006-01-02 15:04:05")
+	return newTimestamp, nil
+}
+
+func decodeInfo(intervalInMinutes int, source []map[string]interface{}) ([]acmInfo, error) {
+	acmInfos := make([]acmInfo, 0)
+	for _, info := range source {
+		var aInfo acmInfo
+		documentNumber := info["key"].(string)
+		aInfo.documentNumber = documentNumber
+		groupByCameraId := info["group_by_cameraId"].(map[string]interface{})
+		cameraBuckets := groupByCameraId["buckets"].([]interface{})
+		for _, cameraInfo := range cameraBuckets {
+			var camsInfo camerasInfo
+			cInfo := cameraInfo.(map[string]interface{})
+			cameraId := cInfo["key"].(string)
+			camsInfo.cameraId = cameraId
+			dataBuckets := cInfo["top_hits"].(map[string]interface{})["hits"].(map[string]interface{})["hits"].([]interface{})
+			markTime := ""
+			for _, dataInfo := range dataBuckets {
+				var capInfo captureInfo
+				dInfo := dataInfo.(map[string]interface{})
+				dSource := dInfo["_source"].(map[string]interface{})
+				id := dSource["id"].(string)
+				picDate := dSource["picDate"].(string)
+				//addFlag := false
+				if markTime == "" {
+					//addFlag = true
+					markTime = picDate
+				} else {
+					if checkTimeDifference(markTime, picDate, intervalInMinutes) {
+						//fmt.Println(markTime, picDate)
+						markTime = picDate
+						continue
+					}
+					markTime = picDate
+				}
+				capInfo.id = id
+				capInfo.picDate = picDate
+				camsInfo.captureInfos = append(camsInfo.captureInfos, capInfo)
+			}
+			aInfo.camerasInfos = append(aInfo.camerasInfos, camsInfo)
+		}
+		acmInfos = append(acmInfos, aInfo)
+	}
+	return acmInfos, nil
+}
+
+type addResultIds struct {
+	documentNumber string
+	unionIds       []unionId
+}
+
+type unionId struct {
+	baseId   string
+	targetId string
+}
+
+func addResultInfo(source []map[string]interface{}, targetAddResultIds *[]addResultIds, bId string) {
+	found := false
+	for _, info := range source {
+		documentNumber := info["key"].(string)
+		dataBuckets := info["top_hits"].(map[string]interface{})["hits"].(map[string]interface{})["hits"].([]interface{})
+		id := dataBuckets[0].(map[string]interface{})["_source"].(map[string]interface{})["id"].(string)
+		//fmt.Println("documentNumber: ", documentNumber, "\tid: ", id)
+		for i, docInfo := range *targetAddResultIds {
+			if documentNumber == docInfo.documentNumber {
+				//fmt.Println("鏂版洿鏂�")
+				(*targetAddResultIds)[i].unionIds = append((*targetAddResultIds)[i].unionIds, unionId{baseId: bId, targetId: id})
+				found = true
+				break
+			}
+		}
+		if !found {
+			//fmt.Println("鏂版坊鍔�")
+			var targetAddResultId addResultIds
+			targetAddResultId.documentNumber = documentNumber
+			targetAddResultId.unionIds = append(targetAddResultId.unionIds, unionId{baseId: bId, targetId: id})
+			*targetAddResultIds = append(*targetAddResultIds, targetAddResultId)
+		}
+
+	}
+}
+
+func removeDuplicates(nums []string) []string {
+	result := make([]string, 0)
+	seen := make(map[string]bool)
+
+	for _, num := range nums {
+		if !seen[num] {
+			result = append(result, num)
+			seen[num] = true
+		}
+	}
+
+	return result
+}
+
+func findAnalyzeCoordinatedMovementsInfos(infos []acmInfo, docNumber string, beforeTime int, afterTime int, frequency int,
+	indexName string, serverIp string, serverPort string) (map[string]interface{}, error) {
+	//baseAddResultIds := make([]addResultIds, 0)
+	targetAddResultIds := make([]addResultIds, 0)
+	esURL := "http://" + serverIp + ":" + serverPort + "/" + indexName + "/_search"
+	for _, info := range infos {
+		for _, cInfo := range info.camerasInfos {
+			for _, pInfo := range cInfo.captureInfos {
+				gteDate, err := addSecondsToTimestamp(pInfo.picDate[:19], beforeTime)
+				if err != nil {
+					fmt.Println(err)
+				}
+				lteDate, err := addSecondsToTimestamp(pInfo.picDate[:19], afterTime)
+				if err != nil {
+					fmt.Println(err)
+				}
+				queryDSL := `
+					{
+					    "size": 0,
+					    "query": {
+					        "bool": {
+					            "filter": [
+					                {
+					                    "range": {
+					                        "picDate": {
+					                            "gte": "` + gteDate + `",
+					                            "lte": "` + lteDate + `"
+					                        }
+					                    }
+					                },
+					                {
+					                    "term": {
+					                        "cameraId": "` + cInfo.cameraId + `"
+					                    }
+					                }
+					            ],            
+								"must_not": [
+               						 {
+                    					"term": {
+                       						 "documentNumber": "` + docNumber + `"
+                    					}
+                					 }
+								]
+					        }
+					    },
+					    "aggs": {
+					        "group_by_documentnumber": {
+					            "terms": {
+					                "field": "documentNumber",
+					                "size": 100000
+					            },
+					            "aggs": {
+					                "top_hits": {
+					                    "top_hits": {
+					                        "_source": [
+					                            "id",
+					                            "cameraId",
+					                            "picDate"
+					                        ],
+					                        "size": 10000,
+					                        "sort": [
+					                            {
+					                                "picDate": {
+					                                    "order": "asc"
+					                                }
+					                            }
+					                        ]
+					                    }
+					                }
+					            }
+					        }
+					    }
+					}`
+				//fmt.Println(esURL, queryDSL)
+				buf, err := EsReq("POST", esURL, []byte(queryDSL))
+				if err != nil {
+					return nil, err
+				}
+				source, err := SourceAggregationList(buf)
+				if err != nil {
+					return nil, err
+				}
+				//fmt.Println("pInfo.id: ", pInfo.id)
+				addResultInfo(source, &targetAddResultIds, pInfo.id)
+				//fmt.Println("targetAddResultIds: ", targetAddResultIds)
+				if err != nil {
+					return nil, err
+				}
+				//fmt.Println(source)
+			}
+		}
+	}
+	//fmt.Println("targetAddResultIds: ", targetAddResultIds)
+	baseIds := make([]string, 0)
+	targetIds := make([]string, 0)
+	for _, tAIdInfo := range targetAddResultIds {
+		if len(tAIdInfo.unionIds) >= frequency {
+			for _, unionId := range tAIdInfo.unionIds {
+				baseIds = append(baseIds, unionId.baseId)
+				targetIds = append(targetIds, unionId.targetId)
+			}
+		}
+	}
+
+	rdbaseIds := removeDuplicates(baseIds)
+	rdtargetIds := removeDuplicates(targetIds)
+	baseInfos, err := GetInfosByIds(rdbaseIds, indexName, serverIp, serverPort)
+	if err != nil {
+		return nil, err
+	}
+	targetInfos, err := GetInfosByIds(rdtargetIds, indexName, serverIp, serverPort)
+	if err != nil {
+		return nil, err
+	}
+	docNumberMap := make(map[string][]interface{})
+	for _, tinfo := range targetInfos {
+		docNumber := tinfo["documentNumber"].(string)
+		docNumberMap[docNumber] = append(docNumberMap[docNumber], tinfo)
+	}
+	targetRecordInfos := make([]map[string]interface{}, 0)
+	for docNumber, infos := range docNumberMap {
+		ifs := make(map[string]interface{})
+		ifs["documentNumber"] = docNumber
+		ifs["recordInfos"] = infos
+		targetRecordInfos = append(targetRecordInfos, ifs)
+	}
+	result := make(map[string]interface{})
+	result["baseRecordInfo"] = baseInfos
+	result["targetRecordInfo"] = targetRecordInfos
+	return result, nil
+}
+
+func AnalyzeCoordinatedMovements(comIds []string, docNumber string, startDate string, endDate string, beforeTime int, afterTime int, frequency int,
+	intervalInMinutes int, indexName string, serverIp string, serverPort string) (map[string]interface{}, error) {
+	esURL := "http://" + serverIp + ":" + serverPort + "/" + indexName + "/_search"
+	//鍒ゆ柇绀惧尯IDs
+	comIdsStr := ""
+	if comIds == nil || len(comIds) > 0 {
+		esComIds := strings.Replace(strings.Trim(fmt.Sprint(comIds), "[]"), " ", "\",\"", -1)
+		comIdsStr = "{\"terms\":{\"communityId\":[\"" + esComIds + "\"]}},"
+	}
+	queryDSL := `
+	{
+	    "size": 0,
+	    "query": {
+	        "bool": {
+	            "filter": [
+	                {
+	                    "range": {
+	                        "picDate": {
+	                            "gte": "` + startDate + `",
+	                            "lte": "` + endDate + `"
+	                        }
+	                    }
+	                },
+					` + comIdsStr + `
+	                {
+	                    "term": {
+	                        "documentNumber": "` + docNumber + `"
+	                    }
+	                }
+	            ]
+	        }
+	    },
+	    "aggs": {
+	        "group_by_documentnumber": {
+	            "terms": {
+	                "field": "documentNumber",
+	                "size": 100000
+	            },
+	            "aggs": {
+	                "group_by_cameraId": {
+	                    "terms": {
+	                        "field": "cameraId",
+	                        "size": 10000
+	                    },
+	                    "aggs": {
+	                        "top_hits": {
+	                            "top_hits": {
+	                                "_source": [
+	                                    "id",
+	                                    "cameraId",
+	                                    "picDate"
+	                                ],
+	                                "size": 10000,
+	                                "sort": [
+	                                    {
+	                                        "picDate": {
+	                                            "order": "asc"
+	                                        }
+	                                    }
+	                                ]
+	                            }
+	                        }
+	                    }
+	                }
+	            }
+	        }
+	    }
+	}`
+	//fmt.Println(esURL)
+	//fmt.Println(queryDSL)
+	buf, err := EsReq("POST", esURL, []byte(queryDSL))
+	if err != nil {
+		return nil, err
+	}
+	source, err := SourceAggregationList(buf)
+	if err != nil {
+		return nil, err
+	}
+	docResult, err := decodeInfo(intervalInMinutes, source)
+	if err != nil {
+		return nil, err
+	}
+	//fmt.Println(docResult)
+	result, err := findAnalyzeCoordinatedMovementsInfos(docResult, docNumber, beforeTime, afterTime, frequency, indexName, serverIp, serverPort)
+	if err != nil {
+		return nil, err
+	}
+	//fmt.Println(result)
+	return result, nil
+}
+
+func GetInfosByIds(ids []string, indexName string, serverIp string, serverPort string) ([]map[string]interface{}, error) {
+	captureIds := strings.Replace(strings.Trim(fmt.Sprint(ids), "[]"), " ", "\",\"", -1)
+	esURL := "http://" + serverIp + ":" + serverPort + "/" + indexName + "/_search"
+	queryDSL := `
+            {
+            	"query": {
+            		"bool": {
+            			"filter": [{
+            				"terms": {
+            					"id": [
+            						"` + captureIds + `"
+            					]
+            				}
+            			}]
+            		}
+            	},
+                "size":1000000,
+				"sort":[{"picDate":{"order":"desc"}}],
+				"_source": {"includes":[],"excludes":["*.feature"]}
+            }
+`
+	buf, err := EsReq("POST", esURL, []byte(queryDSL))
+	if err != nil {
+		return nil, err
+	}
+	sources, err := Sourcelist(buf)
+	if err != nil {
+		return nil, err
+	}
+
+	return sources, nil
+}
+
+// ***********************閲嶅簡End************************************//
 // 鏍规嵁鎶撴媿浜哄憳id鏌ヨ鎶撴媿浜哄憳淇℃伅
 func AIOceaninfosbyid(id []string, indexName string, serverIp string, serverPort string) ([]protomsg.AIOcean, error) {
 	var aIOceanInfo []protomsg.AIOcean
@@ -56,7 +824,43 @@
 	return aIOcean, nil
 }
 
-//鏍规嵁鎶撴媿搴撲汉鍛榠d鏌ヨ鐗瑰緛鍊�
+// 鏍规嵁鎶撴媿浜哄憳id鏌ヨ瑙嗛鍦板潃
+func AIOceanVideoUrlbyid(id string, indexName string, serverIp string, serverPort string) (string, error) {
+	//var aIOceanInfo []protomsg.AIOcean
+	//videopersonsPersonId := strings.Replace(strings.Trim(fmt.Sprint(id), "[]"), " ", "\",\"", -1)
+	var dbinfoRequest = `
+		{
+    		"query": {
+        		"bool": {
+            		"filter": [
+                		{
+                    		"term": {
+                       			"id": "` + id + `"
+                    		}
+                		}
+            		]
+        		}
+    		},
+    		"_source": [
+        		"videoUrl"
+    		]
+		}
+        `
+	buf, err := EsReq("POST", "http://"+serverIp+":"+serverPort+"/"+indexName+"/_search", []byte(dbinfoRequest))
+	if err != nil {
+		return "", err
+	}
+
+	sources, err := Sourcelist(buf)
+	if err != nil {
+		return "", err
+	}
+	videoUrl := sources[0]["videoUrl"].(string)
+	//aIOcean := AIOceanAnalysis(sources)
+	return videoUrl, nil
+}
+
+// 鏍规嵁鎶撴媿搴撲汉鍛榠d鏌ヨ鐗瑰緛鍊�
 func GetVideoPersonFaceFeatureById(id string, indexName string, serverIp string, serverPort string) (string, error) {
 	var jsonDSL = `
             {
@@ -86,7 +890,7 @@
 	return feature, nil
 }
 
-//鏍规嵁鐩爣id鏌ヨ宸茶拷鍔犳潯鏁�
+// 鏍规嵁鐩爣id鏌ヨ宸茶拷鍔犳潯鏁�
 func GetLinkTagInfoSize(id string, indexName string, serverIp string, serverPort string) (size int, err error) {
 	url := "http://" + serverIp + ":" + serverPort + "/" + indexName + "/_search"
 	queryDSL := `{
@@ -112,7 +916,7 @@
 	return size, nil
 }
 
-//鏍规嵁鐩爣id杩藉姞璺熻釜淇℃伅
+// 鏍规嵁鐩爣id杩藉姞璺熻釜淇℃伅
 func AppendTargetInfo(id string, targetInfo string, indexName string, serverIp string, serverPort string, updateTime string) (string, error) {
 	if targetInfo == "" {
 		return "", errors.New("append data is nil")
@@ -159,7 +963,699 @@
 
 }
 
-//鏍规嵁鎶撴媿浜哄憳id鏇存柊锛坴ideourl锛夋憚鍍忔満鍦板潃
+/**************************************customer analysis util start**************************************/
+/*******************sort []map util*******************/
+type MapsSort struct {
+	Key     string
+	MapList []map[string]interface{}
+}
+
+func (m *MapsSort) Len() int {
+	return len(m.MapList)
+}
+
+func (m *MapsSort) Less(i, j int) bool {
+	return m.MapList[i][m.Key].(string) > m.MapList[j][m.Key].(string)
+}
+
+func (m *MapsSort) Swap(i, j int) {
+	m.MapList[i], m.MapList[j] = m.MapList[j], m.MapList[i]
+}
+
+/*******************sort []map util*******************/
+//鏍规嵁鏃堕棿鑼冨洿鑱氬悎鎵�鏈夊尯鍩熶汉淇℃伅锛岃繑鍥炲浐瀹氭潯鏁�
+func GetFaceDataByTimeAndTotal(startTime string, endTime string, total int, 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"
+                    }
+                }
+            ]
+        }
+    },
+    "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
+	}
+	if len(source) == 0 {
+		return source, nil
+	}
+	faceSource := make([]map[string]interface{}, 0)
+	for index, info := range source {
+		if int(info["stayTime"].(float64)) > thresholdStayTime {
+			faceSource = append(faceSource, source[index])
+		}
+	}
+	mapsSort := MapsSort{}
+	mapsSort.Key = "endTime"
+	mapsSort.MapList = faceSource
+	sort.Sort(&mapsSort)
+	if len(faceSource) > total {
+		return mapsSort.MapList[:total], nil
+	}
+	return mapsSort.MapList, nil
+}
+
+func GetFaceDataByTimeAndId(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
+	}
+	if len(source) == 0 {
+		return source, nil
+	}
+	faceSource := make([]map[string]interface{}, 0)
+	for index, info := range source {
+		if int(info["stayTime"].(float64)) > thresholdStayTime {
+			faceSource = append(faceSource, source[index])
+		}
+	}
+	mapsSort := MapsSort{}
+	mapsSort.Key = "startTime"
+	mapsSort.MapList = faceSource
+	sort.Sort(&mapsSort)
+	return mapsSort.MapList, nil
+}
+
+func GetFaceIdDeduplication(startTime string, endTime string, serverIp string, serverPort string, indexName string) (ids []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"
+                    }
+                }
+            ]
+        }
+    },
+    "size": 0,
+    "aggs": {
+        "buckets_aggs": {
+            "composite": {
+                "sources": [
+                    {
+                        "faceId": {
+                            "terms": {
+                                "field": "baseInfo.targetId"
+                            }
+                        }
+                    }
+                ],
+                "size": 10000000
+            },
+            	"aggs": {
+                	"top_attention_hits": {
+                    	"top_hits": {
+                        	"size": 1,
+                        	"sort": [
+                            	{
+                                	"picDate": {
+                                    	"order": "desc"
+                                	}
+                            	}
+                        	],
+                        	"_source": {
+                            	"includes": [
+                                	"picDate"
+                            	]
+                        	}
+                    	}
+             	   	}
+          	  	}
+        	}
+        }
+    }
+}`
+	//fmt.Println(requestUrl)
+	//fmt.Println(requestBody)
+	buf, err := EsReq("POST", requestUrl, []byte(requestBody))
+	if err != nil {
+		return nil, err
+	}
+	ids, err1 := SourceDeduplication(buf)
+	if err1 != nil {
+		return nil, err1
+	}
+	if len(ids) > 1 {
+		mapsSort := MapsSort{}
+		mapsSort.Key = "lastTime"
+		mapsSort.MapList = ids
+		sort.Sort(&mapsSort)
+		return mapsSort.MapList, nil
+	}
+	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) {
+
+	var filterArr []string
+	if cameraId != nil && len(cameraId) > 0 {
+		esCameraId := strings.Replace(strings.Trim(fmt.Sprint(cameraId), "[]"), " ", "\",\"", -1)
+		filterArr = append(filterArr, `{
+							"terms": {
+                        	"cameraId": ["`+esCameraId+`"]
+						}
+				}`)
+	}
+	filterArr = append(filterArr, `{
+                    "range": {
+                        "picDate": {
+                            "gte": "`+startTime+`",
+                            "lte": "`+endTime+`"
+                        }
+                    }
+                }`)
+	filterArr = append(filterArr, `                {
+                    "term": {
+                        "targetInfo.targetType.raw": "Yolo"
+                    }
+                }`)
+	queryStr := strings.Join(filterArr, ",")
+
+	personUrl := "http://" + serverIp + ":" + serverPort + "/" + indexName + "/_search"
+	personBody := `{
+    "query": {
+        "bool": {
+            "filter": [
+				` + queryStr + `
+            ]
+        }
+    },
+    "size": 2147483647,
+    "_source": {
+        "includes": [
+            "cameraId",
+            "cameraName",
+            "cameraAddr",
+            "targetInfo.targetScore",
+            "picDate",
+            "updateTime",
+            "picMaxUrl",
+            "targetInfo.belongsTargetId",
+            "targetInfo.targetLocation",
+			"picWH"
+        ]
+    }
+}`
+	//fmt.Println(personUrl)
+	//fmt.Println(personBody)
+	source := make(map[string]interface{})
+	queryStartTime := time.Now()
+	buf, err := EsReq("POST", personUrl, []byte(personBody))
+	if err != nil {
+		return nil, err
+	}
+	queryUseTime := time.Now().Sub(queryStartTime).Seconds() * 1000
+	sources, err := Sourcelist(buf)
+	if err != nil {
+		return nil, err
+	}
+	resData, err := PerSonAnalysis(sources)
+	source["result"] = resData
+	source["total"] = len(resData)
+	source["queryUseTime"] = queryUseTime
+	//println(sources)
+	return source, nil
+
+}
+
+// 鏍规嵁鏃堕棿鑼冨洿锛屾憚鍍忔満鍒楄〃锛屽垎缁勮仛鍚堜汉鑴稿垪琛�,杩斿洖鍒嗙粍鏁版嵁
+func GetFaceDataBucketsByCameraIdAndTimeReturnByGrouped(cameraId []string, personId []string, startTime string, endTime string, thresholdTime float64, serverIp string, ServerPort string, indexName string) (buckersDate map[string]interface{}, err error) {
+	var filterArr []string
+	if cameraId != nil && len(cameraId) > 0 {
+		esCameraId := strings.Replace(strings.Trim(fmt.Sprint(cameraId), "[]"), " ", "\",\"", -1)
+		filterArr = append(filterArr, `{
+							"terms": {
+                        	"cameraId": ["`+esCameraId+`"]
+						}
+				}`)
+	}
+	if personId != nil && len(personId) > 0 {
+		esPersonId := strings.Replace(strings.Trim(fmt.Sprint(personId), "[]"), " ", "\",\"", -1)
+		filterArr = append(filterArr, `{
+			"terms": {
+				"baseInfo.targetId": ["`+esPersonId+`"]
+				}
+			}`)
+	}
+	filterArr = append(filterArr, `{
+                    "range": {
+                        "picDate": {
+                            "gte": "`+startTime+`",
+                            "lte": "`+endTime+`"
+                        }
+                    }
+                }`)
+	filterArr = append(filterArr, `                {
+                    "term": {
+                        "targetInfo.targetType.raw": "FaceDetect"
+                    }
+                }`)
+	queryStr := strings.Join(filterArr, ",")
+
+	var buckersUrl = "http://" + serverIp + ":" + ServerPort + "/" + indexName + "/_search"
+	var buckersBody = `{
+    "query": {
+        "bool": {
+            "filter": [
+				` + queryStr + `
+            ]
+        }
+    },
+    "size": 0,
+    "aggs": {
+        "buckets_aggs": {
+            "composite": {
+                "sources": [
+                    {
+                        "baseInfo.targetId": {
+                            "terms": {
+                                "field": "baseInfo.targetId"
+                            }
+                        }
+                    },
+                    {
+                        "cameraId": {
+                            "terms": {
+                                "field": "cameraId"
+                            }
+                        }
+                    }
+                ],
+                "size": 10000000
+            },
+            "aggs":{
+                "top_attention_hits":{
+                    "top_hits":{
+                        "size": 1000000,
+                        "sort": [
+                            {
+                                "picDate": {
+                                    "order": "asc"
+                                }
+                            }
+                        ],
+                        "_source":{
+                            "includes":["baseInfo.targetId","cameraId","cameraName","cameraAddr","targetInfo.targetScore","targetInfo.picSmUrl","showLabels","baseInfo.tableId","baseInfo.tableName","baseInfo.bwType","baseInfo.targetName","baseInfo.compareScore","picDate","picMaxUrl","picWH"]
+                        }
+                    }
+                }
+            }
+        }
+    }
+}`
+	//fmt.Println(buckersUrl)
+	//fmt.Println(buckersBody)
+	sources := make(map[string]interface{})
+	queryStartTime := time.Now()
+	buf, err := EsReq("POST", buckersUrl, []byte(buckersBody))
+	if err != nil {
+		return nil, err
+	}
+	queryUseTime := time.Now().Sub(queryStartTime).Seconds() * 1000
+	//fmt.Println(queryUseTime)
+	tmpSources, err := SourceAggregationsReturnByGrouped(buf, thresholdTime)
+	if err != nil {
+		return nil, err
+	}
+	sources["result"] = tmpSources
+	sources["total"] = len(tmpSources)
+	sources["queryUseTime"] = queryUseTime
+	//println(sources)
+	return sources, nil
+}
+
+// 鏍规嵁鏃堕棿鑼冨洿锛屾憚鍍忔満鍒楄〃锛屽垎缁勮仛鍚堜汉鑴稿垪琛�
+func GetFaceDataBucketsByCameraIdAndTime(cameraId []string, personId []string, startTime string, endTime string, thresholdTime float64, serverIp string, ServerPort string, indexName string) (buckersDate map[string]interface{}, err error) {
+	var filterArr []string
+	if cameraId != nil && len(cameraId) > 0 {
+		esCameraId := strings.Replace(strings.Trim(fmt.Sprint(cameraId), "[]"), " ", "\",\"", -1)
+		filterArr = append(filterArr, `{
+							"terms": {
+                        	"cameraId": ["`+esCameraId+`"]
+						}
+				}`)
+	}
+	if personId != nil && len(personId) > 0 {
+		esPersonId := strings.Replace(strings.Trim(fmt.Sprint(personId), "[]"), " ", "\",\"", -1)
+		filterArr = append(filterArr, `{
+			"terms": {
+				"baseInfo.targetId": ["`+esPersonId+`"]
+				}
+			}`)
+	}
+	filterArr = append(filterArr, `{
+                    "range": {
+                        "picDate": {
+                            "gte": "`+startTime+`",
+                            "lte": "`+endTime+`"
+                        }
+                    }
+                }`)
+	filterArr = append(filterArr, `                {
+                    "term": {
+                        "targetInfo.targetType.raw": "FaceDetect"
+                    }
+                }`)
+	queryStr := strings.Join(filterArr, ",")
+
+	var buckersUrl = "http://" + serverIp + ":" + ServerPort + "/" + indexName + "/_search"
+	var buckersBody = `{
+    "query": {
+        "bool": {
+            "filter": [
+				` + queryStr + `
+            ]
+        }
+    },
+    "size": 0,
+    "aggs": {
+        "buckets_aggs": {
+            "composite": {
+                "sources": [
+                    {
+                        "baseInfo.targetId": {
+                            "terms": {
+                                "field": "baseInfo.targetId"
+                            }
+                        }
+                    },
+                    {
+                        "cameraId": {
+                            "terms": {
+                                "field": "cameraId"
+                            }
+                        }
+                    }
+                ],
+                "size": 10000000
+            },
+            "aggs":{
+                "top_attention_hits":{
+                    "top_hits":{
+                        "size": 1000000,
+                        "sort": [
+                            {
+                                "picDate": {
+                                    "order": "asc"
+                                }
+                            }
+                        ],
+                        "_source":{
+                            "includes":["baseInfo.targetId","cameraId","cameraName","cameraAddr","targetInfo.targetScore","targetInfo.picSmUrl","showLabels","baseInfo.tableId","baseInfo.tableName","baseInfo.bwType","baseInfo.targetName","baseInfo.compareScore","picDate","picMaxUrl","picWH"]
+                        }
+                    }
+                }
+            }
+        }
+    }
+}`
+	//fmt.Println(buckersUrl)
+	//fmt.Println(buckersBody)
+	queryStartTime := time.Now()
+	buf, err := EsReq("POST", buckersUrl, []byte(buckersBody))
+	if err != nil {
+		return nil, err
+	}
+	queryUseTime := time.Now().Sub(queryStartTime).Seconds() * 1000
+
+	sources, err := SourceAggregations(buf, thresholdTime, queryUseTime)
+	if err != nil {
+		return nil, err
+	}
+	return sources, nil
+}
+
+// 鏍规嵁鎶撴媿浜哄憳id鏇存柊锛坧icurl锛夊浘鐗囧湴鍧�
+func UpdatePicUrlById(id string, picUrl string, indexName string, serverIp string, serverPort string) (err error) {
+	updateTime := time.Now().Format("2006-01-02 15:04:05")
+	tRes, err := AIOceaninfosbyid([]string{id}, indexName, serverIp, serverPort)
+	if err != nil || len(tRes) == 0 {
+		return err
+	}
+	picMaxUrls := tRes[0].PicMaxUrl
+	sourceStr := `
+        "source": "ctx._source.picMaxUrl.add('` + picUrl + `');ctx._source.updateTime='` + updateTime + `'"
+`
+	if len(picMaxUrls) >= 2 {
+		sourceStr = `"source": "ctx._source.picMaxUrl[1]='` + picUrl + `';ctx._source.updateTime='` + updateTime + `'"`
+	}
+	var info interface{}
+	url := "http://" + serverIp + ":" + serverPort + "/" + indexName + "/_update_by_query?refresh=true"
+
+	var picUrlInfo = `
+        {
+          "script": {
+           ` + sourceStr + `
+          },
+              "query": {
+    		    "bool": {
+    		        "filter": [
+    		            {
+    		                "term": {
+    		                    "id": "` + id + `"
+    		                }
+    		            }
+    		        ]
+    		    }
+    		}
+        }
+        `
+	//logPrint("url: ", url, videoUrlInfo)
+	//fmt.Println(url, picUrlInfo)
+	buf, err := EsReq("POST", url, []byte(picUrlInfo))
+	if err != nil {
+		logPrint("http request videoUrlInfo info is err!")
+		return err
+	}
+	json.Unmarshal(buf, &info)
+	//logPrint(info)
+	out, ok := info.(map[string]interface{})
+	if !ok {
+		logPrint("http response interface can not change map[string]interface{}")
+		return errors.New("http response interface can not change map[string]interface{}")
+	}
+	middle, ok := out["updated"].(float64)
+	if !ok {
+		logPrint("first updated change error!", out)
+		return errors.New("first updated change error!")
+	}
+	if middle == 1 {
+		return nil
+	}
+	if middle == 0 {
+		return errors.New("宸茬粡淇敼")
+	}
+	return nil
+}
+
+// 鏍规嵁鎶撴媿浜哄憳id鏇存柊锛坴ideourl锛夋憚鍍忔満鍦板潃
 func UpdateVideourlById(id string, videoUrl string, indexName string, serverIp string, serverPort string, command int) (statu int, err error) {
 
 	var info interface{}
@@ -196,23 +1692,30 @@
 		return statu, errors.New("http response interface can not change map[string]interface{}")
 	}
 	middle, ok := out["updated"].(float64)
-	if !ok {
+	batches, ok1 := out["batches"].(float64)
+	if !ok || !ok1 {
 		logPrint("first updated change error!")
 		statu = 500
 		return statu, errors.New("first updated change error!")
 	}
-	if middle == 1 {
-		statu = 200
-		return statu, nil
-	}
-	if middle == 0 {
-		statu = 201
-		return statu, errors.New("宸茬粡淇敼")
+	if batches == 0 {
+		logPrint("no such doc in database")
+		statu = 400
+		return statu, errors.New("鐩爣鏁版嵁涓嶅瓨鍦�")
+	} else {
+		if middle == 1 {
+			statu = 200
+			return statu, nil
+		}
+		if middle == 0 {
+			statu = 201
+			return statu, errors.New("宸茬粡淇敼")
+		}
 	}
 	return statu, nil
 }
 
-//鑾峰彇褰撳墠鑺傜偣鎶撴媿搴撴墍鏈変汉鍛業D*缂撳瓨*
+// 鑾峰彇褰撳墠鑺傜偣鎶撴媿搴撴墍鏈変汉鍛業D*缂撳瓨*
 func GetAllLocalVideopersonsId(compareArgs protomsg.CompareArgs, indexName string, serverIp string, serverPort string, alarmLevelTypes string) (capturetable []string) {
 	queryStr := ""
 	queryBody := compareArgs.InputValue
@@ -252,7 +1755,12 @@
 	isCollectStr := ""
 	isCollect := compareArgs.Collection
 	if isCollect != "" {
-		isCollectStr = "{\"term\":{\"isCollect\":\"" + isCollect + "\"}},"
+		//isCollectStr = "{\"term\":{\"isCollect\":\"" + isCollect + "\"}},"
+		if isCollect == "1" {
+			isCollectStr = "{\"term\":{\"isCollect\":true}},"
+		} else if isCollect == "0" {
+			isCollectStr = "{\"term\":{\"isCollect\":false}},"
+		}
 	}
 
 	//鍒ゆ柇甯冮槻绛夌骇
@@ -284,7 +1792,7 @@
 			"\"size\":\"1000\"," +
 			"\"query\":{\"bool\":{" + queryStr +
 			"\"filter\":[" +
-			"{\"term\":{\"targetInfo.targetType.raw\":\"face\"}}," +
+			"{\"term\":{\"targetInfo.targetType.raw\":\"FaceDetect\"}}," +
 			cameraIdStr +
 			alarmLevelStr +
 			taskIdStr +
@@ -376,7 +1884,7 @@
 	return capturetable
 }
 
-//鍒濆鍖栧疄鏃舵姄鎷�
+// 鍒濆鍖栧疄鏃舵姄鎷�
 func InitRealTimeCapture(serverIp string, serverPort string, indexName string, isAlarm string, category string, quantity int) ([]protomsg.AIOcean, error) {
 	var aIOceanInfo []protomsg.AIOcean
 	url := "http://" + serverIp + ":" + serverPort +
@@ -393,7 +1901,7 @@
 	if category != "all" {
 		filterArr = append(filterArr, `				{
 					"term":{
-						"targetInfo.targetType":"`+category+`"
+						"targetInfo.targetType.raw":"`+category+`"
 					}
 				}`)
 
@@ -413,7 +1921,7 @@
 	"sort":[{"picDate":{"order":"desc"}}],
 	"_source": {"includes":[],"excludes":["*.feature"]}
 	}`
-	logPrint(DSLJson)
+	//logPrint(DSLJson)
 	buf, err := EsReq("POST", url, []byte(DSLJson))
 	if err != nil {
 		return aIOceanInfo, err
@@ -429,7 +1937,7 @@
 	return aIOcean, nil
 }
 
-//瀹炴椂鎶撴媿
+// 瀹炴椂鎶撴媿
 func RealTimeCapture(serverIp string, serverPort string, indexName string, isAlarm bool) ([]protomsg.AIOcean, error) {
 	var aIOceanInfo []protomsg.AIOcean
 	url := "http://" + serverIp + ":" + serverPort +
@@ -473,7 +1981,7 @@
 	return aIOcean, nil
 }
 
-//缁煎悎缁熻
+// 缁煎悎缁熻
 func StatisticsComprehensive(serverIp string, serverPort string, indexName string, isAlarm string) (total int, err error) {
 	url := "http://" + serverIp + ":" + serverPort +
 		"/" + indexName + "/_search"
@@ -503,7 +2011,6 @@
 			}
 		}
 	}`
-	//logPrint(DSLJson)
 	buf, err := EsReq("POST", url, []byte(DSLJson))
 	if err != nil {
 		return total, err
@@ -523,7 +2030,7 @@
 	return total, nil
 }
 
-//瀹炴椂鎶ヨ浠诲姟姣旂巼
+// 瀹炴椂鎶ヨ浠诲姟姣旂巼
 func RealTimeAlarmTaskRate(serverIp string, serverPort string, indexName string) (sources []map[string]interface{}, err error) {
 	url := "http://" + serverIp + ":" + serverPort +
 		"/" + indexName + "/_search"
@@ -541,9 +2048,9 @@
 		}
 	},
 	"aggs":{
-		"sdkName_status":{
+		"taskName_status":{
 			"terms":{
-				"field":"sdkName.raw"
+				"field":"taskName.raw"
 			}
 		}
 	}
@@ -562,11 +2069,11 @@
 	if !ok {
 		return nil, errors.New("first hits change error!")
 	}
-	sdkName_status, ok := middle["sdkName_status"].(map[string]interface{})
+	sdkName_status, ok := middle["taskName_status"].(map[string]interface{})
 	if !ok {
 		return nil, errors.New("first hits change error!")
 	}
-
+	//fmt.Println(sdkName_status)
 	for _, in := range sdkName_status["buckets"].([]interface{}) {
 		var source = make(map[string]interface{}, 0)
 		tmpbuf, ok := in.(map[string]interface{})
@@ -584,11 +2091,20 @@
 	return sources, nil
 }
 
-//鑱氬悎浠诲姟鍒楄〃锛宼askId+taskName
-func AggregateTaskList(serverIp string, serverPort string, indexName string, analyServerId string) (sources []map[string]interface{}, err error) {
+// 鑱氬悎浠诲姟鍒楄〃锛宼askId+taskName
+func AggregateTaskList(serverIp string, serverPort string, indexName string, analyServerId string, cameraIds []string) (sources []map[string]interface{}, err error) {
 	url := "http://" + serverIp + ":" + serverPort +
 		"/" + indexName + "/_search"
 	serverFilterStr := ""
+	cameIdFilterStr := ""
+	if cameraIds != nil && len(cameraIds) > 0 {
+		cameIdsStr := strings.Replace(strings.Trim(fmt.Sprint(cameraIds), "[]"), " ", "\",\"", -1)
+		cameIdFilterStr = `,{
+				"term": {
+				"cameraId": "` + cameIdsStr + `"
+					}
+                }`
+	}
 	if analyServerId != "" {
 		serverFilterStr = `,
 			"query": {
@@ -597,8 +2113,9 @@
 				{
 				"term": {
 				"analyServerId": "` + analyServerId + `"
+					}
 				}
-				}
+ 				` + cameIdFilterStr + `
 			]
 			}
 		}`
@@ -670,7 +2187,7 @@
 
 }
 
-//娣诲姞鍗冲皢鍒犻櫎淇″彿
+// 娣诲姞鍗冲皢鍒犻櫎淇″彿
 func AddDeleteSignal() {
 
 }
@@ -711,13 +2228,13 @@
 
 }
 
-//鏌ヨ鏃堕棿娈垫暟鎹� *缂撳瓨*
+// 鏌ヨ鏃堕棿娈垫暟鎹� *缂撳瓨*
 func GetPeriodInfos(serverIp string, serverPort string, startTime string, endTime string, indexName string, shards string, targetType string) ([]*protomsg.MultiFeaCache, error) {
 	var capdbinfo []*protomsg.MultiFeaCache
 	url := "http://" + serverIp + ":" + serverPort + "/" + indexName + "/_search?preference=_shards:" + shards + "|_only_local"
 	var source []string
 	switch targetType {
-	case "face":
+	case "face", "FaceDetect":
 		source = []string{"id", "targetInfo.feature", "analyServerId", "cameraId"}
 	case "track":
 		source = []string{"id", "targetInfo.feature", "analyServerId", "cameraId", "targetInfo.attachTarget.feature", "targetInfo.targetLocation", "linkTagInfo.targetInfo.feature", "linkTagInfo.targetInfo.attachTarget.feature", "linkTagInfo.cameraId", "linkTagInfo.targetInfo.targetLocation"}
@@ -770,14 +2287,14 @@
 func GetOceanFeatures(serverIp string, serverPort string, queryNums int, indexName string, shards string, targetType string) ([]*protomsg.MultiFeaCache, error) {
 	//queryIndexNum int
 	//var dbinfos []*protomsg.MultiFeaCache
-	dbinfos := make([]*protomsg.MultiFeaCache,0)
+	dbinfos := make([]*protomsg.MultiFeaCache, 0)
 	//dbinfosss := make([]*protomsg.MultiFeaCache,0)
 	//dbinfoss = append(dbinfoss, dbinfosss...)
 
 	JsonDSL := ""
 	var source []string
 	switch targetType {
-	case "face":
+	case "face", "FaceDetect":
 		source = []string{"id", "targetInfo.feature", "analyServerId"}
 	case "track":
 		source = []string{"id", "targetInfo.feature", "analyServerId", "targetInfo.attachTarget.feature", "targetInfo.targetLocation", "linkTagInfo.targetInfo.feature", "linkTagInfo.targetInfo.attachTarget.feature", "linkTagInfo.targetInfo.targetLocation"}
@@ -819,20 +2336,20 @@
 			//logPrint("url: ",reqJsonDSL)
 			buf, err := EsReq("POST", url, []byte(reqJsonDSL))
 			if err != nil {
-				logPrint("EsReq: ",err)
+				logPrint("EsReq: ", err)
 				return
 			}
 
 			// 杩斿洖 _source 鏁扮粍
 			sources, err := Sourcelistforscroll(buf)
 			if err != nil {
-				logPrint("EsReq: ",err)
+				logPrint("EsReq: ", err)
 				return
 			}
 			// 杩斿洖鎵�鏈夋煡璇㈢殑鏁版嵁
-			ftmpDatas :=  Parsesources(sources["sourcelist"].([]map[string]interface{}))
+			ftmpDatas := Parsesources(sources["sourcelist"].([]map[string]interface{}))
 			lock.Lock()
-			dbinfos = append(dbinfos,ftmpDatas...)
+			dbinfos = append(dbinfos, ftmpDatas...)
 			//logPrint("prsLen: ", len(Parsesources(sources["sourcelist"].([]map[string]interface{}))))
 			//logPrint("dbinfosLen: ", len(dbinfos))
 			lock.Unlock()
@@ -884,13 +2401,13 @@
 	}
 	wg.Wait()
 
-	fmt.Println("lenth_all: ", len(dbinfos))
+	//fmt.Println("lenth_all: ", len(dbinfos))
 
 	return dbinfos, nil
 }
 
-//************************CORN TASK*******************************
-//鏌ヨ鏃ユ湡鑼冨洿鍐呮槸鍚﹁繕瀛樺湪鏁版嵁
+// ************************CORN TASK*******************************
+// 鏌ヨ鏃ユ湡鑼冨洿鍐呮槸鍚﹁繕瀛樺湪鏁版嵁
 func QueryAnalyServerData(serverIp string, serverPort string, indexName string, startTime string, endTime string, analyServerId string) (result bool, err error) {
 	url := "http://" + serverIp + ":" + serverPort + "/" + indexName + "/_search"
 	deleteJson := `{
@@ -921,7 +2438,7 @@
 	if err != nil {
 		return false, errors.New("瑙g爜澶辫触")
 	}
-	if resTotal == -1 || resTotal == 0{
+	if resTotal == -1 || resTotal == 0 {
 		result = false
 	} else {
 		result = true
@@ -929,9 +2446,45 @@
 	return result, nil
 }
 
+// 鎸夋棩鏈熻寖鍥达紝鏈嶅姟鍣↖d鍒犻櫎鏁版嵁
+func DeleteByDocumentNumber(docNumber []string, serverIp string, serverPort string, indexName string) (total int, err error) {
 
-//鎸夋棩鏈熻寖鍥达紝鏈嶅姟鍣↖d鍒犻櫎鏁版嵁
-func DeleteAnalyServerData(serverIp string, serverPort string, indexName string, startTime string, endTime string, analyServerId string) (result bool, err error) {
+	url := "http://" + serverIp + ":" + serverPort + "/" + indexName + "/_delete_by_query"
+	docNumbers := strings.Replace(strings.Trim(fmt.Sprint(docNumber), "[]"), " ", "\",\"", -1)
+	deleteJson := `{
+	"query":{
+		"bool":{
+			"filter":[
+			{
+				"terms":{
+					"documentNumber":["` + docNumbers + `"]
+				}
+			}	
+			]
+		}
+	}
+}	`
+	//fmt.Println(url)
+	//fmt.Println(deleteJson)
+	//return
+	buf, err := EsReq("POST", url, []byte(deleteJson))
+	if err != nil {
+		return -1, errors.New("璇锋眰澶辫触")
+	}
+	deleteRes, err := SourceDeleted(buf)
+	if err != nil {
+		return -1, errors.New("瑙g爜澶辫触")
+	}
+	return deleteRes, nil
+}
+
+//func GetCaptureDaysByDocumentNumber(docNumber string, comId string, indexName string, serverIp string, serverPort string){
+//	url := "http://" + serverIp + ":" + serverPort + "/" + indexName + "/_search"
+//	queryDSL := ``
+//}
+
+// 鎸夋棩鏈熻寖鍥达紝鏈嶅姟鍣↖d鍒犻櫎鏁版嵁
+func DeleteAnalyServerData(serverIp string, serverPort string, indexName string, startTime string, endTime string, analyServerId string) (total int, err error) {
 	url := "http://" + serverIp + ":" + serverPort + "/" + indexName + "/_delete_by_query"
 	deleteJson := `{
 	"query":{
@@ -953,23 +2506,20 @@
 		}
 	}
 }	`
+	fmt.Println(url)
+	fmt.Println(deleteJson)
 	buf, err := EsReq("POST", url, []byte(deleteJson))
 	if err != nil {
-		return false, errors.New("璇锋眰澶辫触")
+		return -1, errors.New("璇锋眰澶辫触")
 	}
 	deleteRes, err := SourceDeleted(buf)
 	if err != nil {
-		return false, errors.New("瑙g爜澶辫触")
+		return -1, errors.New("瑙g爜澶辫触")
 	}
-	if deleteRes == -1 {
-		result = false
-	} else {
-		result = true
-	}
-	return result, nil
+	return deleteRes, nil
 }
 
-//缁欐墍鏈夎妭鐐硅拷鍔犲垹闄や换鍔′俊鎭�
+// 缁欐墍鏈夎妭鐐硅拷鍔犲垹闄や换鍔′俊鎭�
 func AddDelTask(serverIp string, serverPort string, indexName string, startTime string, endTime string, analyServerId string) (result bool, err error) {
 	url := "http://" + serverIp + ":" + serverPort + "/" + indexName + "/_update_by_query"
 	addJson := `{
@@ -985,7 +2535,15 @@
         }
     },
     "query": {
-        "match_all": {}
+        "bool": {
+            "filter": [
+                {
+                    "term": {
+                        "application": "loopCoverage"
+                    }
+                }
+            ]
+        }
     }
 }`
 	buf, err := EsReq("POST", url, []byte(addJson))
@@ -1004,7 +2562,7 @@
 	return result, nil
 }
 
-//绉婚櫎宸叉墽琛屽畬鐨勫垹闄や换鍔�
+// 绉婚櫎宸叉墽琛屽畬鐨勫垹闄や换鍔�
 func DeleteDelTask(serverIp string, serverPort string, indexName string, analyServerId string) (result bool, err error) {
 	url := "http://" + serverIp + ":" + serverPort + "/" + indexName + "/_update_by_query"
 	deleteJson := `{
@@ -1037,3 +2595,66 @@
 	}
 	return result, nil
 }
+
+type ShardInfo struct {
+	ShardIndex string `json:"shardIndex"` //鍒嗙墖鎵�灞炵储寮曞悕绉�
+	ShardNum   int    `json:"shardNum"`   //鍒嗙墖鍙�
+	ShardRole  string `json:"shardRole"`  //鍒嗙墖瑙掕壊(涓诲垎鐗囷細primary 鍓湰鍒嗙墖锛歳eplica)
+	ShardState string `json:"shardState"` //鍒嗙墖鐘舵��(鍚敤锛歋TARTED 鏈惎鐢細UNASSIGNED)
+	ShardDocs  int    `json:"shardDocs"`  //鍒嗙墖宸蹭繚瀛樻枃妗f暟
+	ShardStore string `json:"shardStore"` //鍒嗙墖褰撳墠瀛樺偍鏁版嵁澶у皬
+	ShardIp    string `json:"shardIp"`    //鍒嗙墖鎵�鍦ㄨ妭鐐筰p
+	ShardNode  string `json:"shardNode"`  //鍒嗙墖鎵�鍦ㄨ妭鐐瑰悕绉�
+}
+
+// 鑾峰彇绱㈠紩鍒嗙墖淇℃伅
+func GetShardsByIndex(serverIp string, serverPort string, indexName string) ([]ShardInfo, error) {
+	url := "http://" + serverIp + ":" + serverPort + "/_cat/shards?v"
+	buf, err := EsReq("GET", url, []byte(""))
+	if err != nil {
+		return nil, err
+	}
+	var inf = []ShardInfo{}
+	res := strings.Split(string(buf), "\n")[1:]
+	for _, r := range res {
+		if r != "" {
+
+			inx := strings.Fields(r)
+			index := inx[0]
+			shard, _ := strconv.Atoi(inx[1])
+			prired := inx[2]
+			if prired == "r" {
+				prired = "replica"
+			}
+			if prired == "p" {
+				prired = "primary"
+			}
+			state := inx[3]
+			docs := 0
+			store := ""
+			ip := ""
+			node := ""
+			if state == "STARTED" {
+				docs, _ = strconv.Atoi(inx[4])
+				store = inx[5]
+				ip = inx[6]
+				node = inx[7]
+			}
+			if index == indexName {
+				inf = append(inf, ShardInfo{
+					ShardIndex: index,
+					ShardNum:   shard,
+					ShardRole:  prired,
+					ShardState: state,
+					ShardDocs:  docs,
+					ShardStore: store,
+					ShardIp:    ip,
+					ShardNode:  node,
+				})
+
+			}
+		}
+
+	}
+	return inf, nil
+}

--
Gitblit v1.8.0