From 86655db5ef2cca9d7cf4eedae879c3a7c4464b9f Mon Sep 17 00:00:00 2001
From: liuxiaolong <736321739@qq.com>
Date: 星期六, 26 十月 2019 16:10:08 +0800
Subject: [PATCH] func GetAllLocalVideopersonsId get all when analyServerId is null

---
 EsApi.go |  498 ++++++++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 420 insertions(+), 78 deletions(-)

diff --git a/EsApi.go b/EsApi.go
index c8e9541..391f9e6 100644
--- a/EsApi.go
+++ b/EsApi.go
@@ -6,8 +6,9 @@
     "fmt"
     "strconv"
     "strings"
+	"sync"
 
-    "basic.com/pubsub/protomsg.git"
+	"basic.com/pubsub/protomsg.git"
         )
 
 // 鏌ヨ搴曞簱浜哄憳淇℃伅
@@ -285,87 +286,157 @@
 }
 
 //鑾峰彇褰撳墠鑺傜偣鎶撴媿搴撴墍鏈変汉鍛業D
-func GetAllLocalVideopersonsId(compareArgs  protomsg.CompareArgs,indexName string, serverIp string, serverPort string) (capturetable []string) {
-    queryStr := ""
-    queryBody := compareArgs.InputValue
-    //妫�绱㈡
-    if queryBody != "" {
-        queryStr = "\"must\":[{\"multi_match\":{\"query\":\"" + queryBody + "\",\"fields\":[\"alarmRules.alarmLevel^1.5\",\"ageDescription^1.5\",\"taskName^1.5\",\"baseInfo.tableName^1.5\",\"sex^2.0\",\"race^2.0\",\"content^1.0\",\"baseInfo.idCard^1.8\",\"cameraAddr^1.0\"]," +
-            "\"type\":\"cross_fields\",\"operator\":\"OR\",\"slop\":0,\"prefix_length\":0,\"max_expansions\":50,\"zero_terms_query\":\"NONE\",\"auto_generate_synonyms_phrase_query\":true,\"fuzzy_transpositions\":true,\"boost\":1}}],"
-    }
-    gteDate := compareArgs.SearchTime[0]
-    lteDate := compareArgs.SearchTime[1]
-    //鍒ゆ柇浠诲姟ID
-    taskIdStr := ""
-    taskId := compareArgs.Tasks
-    if taskId != nil && len(taskId) > 0 {
-        esTaskId := strings.Replace(strings.Trim(fmt.Sprint(taskId), "[]"), " ", "\",\"", -1)
-        taskIdStr = "{\"terms\":{\"taskId\":[\"" + esTaskId + "\"]}},"
-    }
-    //鍒ゆ柇鎽勫儚鏈篒D
-    cameraIdStr := ""
-    cameraId := compareArgs.TreeNodes
-    if cameraId != nil && len(cameraId) > 0 {
-        esCameraId := strings.Replace(strings.Trim(fmt.Sprint(cameraId), "[]"), " ", "\",\"", -1)
-        cameraIdStr = "{\"terms\":{\"cameraId\":[\"" + esCameraId + "\"]}},"
-    }
+func GetAllLocalVideopersonsId(compareArgs protomsg.CompareArgs, indexName string, serverIp string, serverPort string) (capturetable []string) {
+	queryStr := ""
+	queryBody := compareArgs.InputValue
+	//妫�绱㈡
+	if queryBody != "" {
+		queryStr = "\"must\":[{\"multi_match\":{\"query\":\"" + queryBody + "\",\"fields\":[\"alarmRules.alarmLevel^1.5\",\"ageDescription^1.5\",\"taskName^1.5\",\"baseInfo.tableName^1.5\",\"sex^2.0\",\"race^2.0\",\"content^1.0\",\"baseInfo.idCard^1.8\",\"cameraAddr^1.0\"]," +
+			"\"type\":\"cross_fields\",\"operator\":\"OR\",\"slop\":0,\"prefix_length\":0,\"max_expansions\":50,\"zero_terms_query\":\"NONE\",\"auto_generate_synonyms_phrase_query\":true,\"fuzzy_transpositions\":true,\"boost\":1}}],"
+	}
+	gteDate := compareArgs.SearchTime[0]
+	lteDate := compareArgs.SearchTime[1]
+	//鍒ゆ柇浠诲姟ID
+	taskIdStr := ""
+	taskId := compareArgs.Tasks
+	if taskId != nil && len(taskId) > 0 {
+		esTaskId := strings.Replace(strings.Trim(fmt.Sprint(taskId), "[]"), " ", "\",\"", -1)
+		taskIdStr = "{\"terms\":{\"taskId\":[\"" + esTaskId + "\"]}},"
+	}
+	//鍒ゆ柇鎽勫儚鏈篒D
+	cameraIdStr := ""
+	cameraId := compareArgs.TreeNodes
+	if cameraId != nil && len(cameraId) > 0 {
+		esCameraId := strings.Replace(strings.Trim(fmt.Sprint(cameraId), "[]"), " ", "\",\"", -1)
+		cameraIdStr = "{\"terms\":{\"cameraId\":[\"" + esCameraId + "\"]}},"
+	}
 
-    //鍒ゆ柇搴撹〃ID
-    tableId := compareArgs.Tabs
-    esTableId := ""
-    esTableIdStr := ""
-    if tableId != nil && len(tableId) > 0 {
-        esTableId = strings.Replace(strings.Trim(fmt.Sprint(tableId), "[]"), " ", "\",\"", -1)
-        esTableIdStr = "{\"terms\":{\"baseInfo.tableId\":[\"" + esTableId + "\"]}},"
-    }
-    isCollectStr := ""
-    isCollect := compareArgs.Collection
-    if isCollect != "" {
-        isCollectStr = "{\"term\":{\"isCollect\":\"" + isCollect + "\"}},"
-    }
+	//鍒ゆ柇搴撹〃ID
+	tableId := compareArgs.Tabs
+	esTableId := ""
+	esTableIdStr := ""
+	if tableId != nil && len(tableId) > 0 {
+		esTableId = strings.Replace(strings.Trim(fmt.Sprint(tableId), "[]"), " ", "\",\"", -1)
+		esTableIdStr = "{\"terms\":{\"baseInfo.tableId\":[\"" + esTableId + "\"]}},"
+	}
+	isCollectStr := ""
+	isCollect := compareArgs.Collection
+	if isCollect != "" {
+		isCollectStr = "{\"term\":{\"isCollect\":\"" + isCollect + "\"}},"
+	}
 
-    //浣跨敤es搴曞眰鏈哄埗澶勭悊鍒嗛〉
+	//浣跨敤es搴曞眰鏈哄埗澶勭悊鍒嗛〉
+
+	analyServerFilterStr := ""
+	analyServerId := compareArgs.AnalyServerId
+	if analyServerId != "" {
+		analyServerFilterStr = "{\"term\":{\"analyServerId\":\"" + analyServerId + "\"}},"
+	}
 
 
-    analyServerId := compareArgs.AnalyServerId
-    if analyServerId == "" {
-        fmt.Println("no analyServerId")
-        return
-    }
-    analyServerFilterStr := "{\"term\":{\"analyServerId\":\"" + analyServerId + "\"}},"
+	//棣栨璇锋眰澶�
+	url := "http://" + serverIp + ":" + serverPort +
+		"/" + indexName + "/_search?search_type=dfs_query_then_fetch;scroll=1m"
 
-    //璇锋眰澶�
-    url := "http://" + serverIp + ":" + serverPort +
-        "/" + indexName + "/_search?search_type=dfs_query_then_fetch"
+	var lock sync.RWMutex
+	var wg sync.WaitGroup
 
-    //璇锋眰浣�
-    prama := "{" +
-        "\"size\":\"100000000\"," +
-        "\"query\":{\"bool\":{" + queryStr +
-        "\"filter\":[" +
-        cameraIdStr +
-        taskIdStr +
-        isCollectStr +
-        esTableIdStr +
-        analyServerFilterStr +
-        "{\"range\":{\"picDate\":{\"from\":\"" + gteDate + "\",\"to\":\"" + lteDate + "\",\"include_lower\":true,\"include_upper\":true,\"boost\":1}}}]}}," +
-        "\"_source\":[\"id\"]" +
-        "}"
-    fmt.Println(url)
-    fmt.Println(prama)
-    buf, err := EsReq("POST", url,[]byte(prama))
-    if err != nil {
-        fmt.Println("http request videoUrlInfo info is err!")
-        return
-    }
-    sources, err := Sourcelist(buf)
-    if err != nil {
-        return
-    }
-    for _, source := range  sources{
-        capturetable = append(capturetable, source["id"].(string))
-    }
-    return capturetable
+	for i := 0; i < 32; i++ {
+		//璇锋眰浣�
+		prama := "{" +
+			"\"slice\":{\"id\":" + strconv.Itoa(i) + ",\"max\":48}," +
+			"\"size\":\"1000\"," +
+			"\"query\":{\"bool\":{" + queryStr +
+			"\"filter\":[" +
+			cameraIdStr +
+			taskIdStr +
+			isCollectStr +
+			esTableIdStr +
+			analyServerFilterStr +
+			"{\"range\":{\"picDate\":{\"from\":\"" + gteDate + "\",\"to\":\"" + lteDate + "\",\"include_lower\":true,\"include_upper\":true,\"boost\":1}}}]}}," +
+			"\"_source\":[\"id\"]" +
+			"}"
+		wg.Add(1)
+		go func() {
+			defer wg.Done()
+
+			//fmt.Println(url)
+			//fmt.Println(prama)
+			buf, err := EsReq("POST", url, []byte(prama))
+
+			if err != nil {
+				fmt.Println("http request videoUrlInfo info is err!")
+				fmt.Println(len(capturetable))
+				return
+			}
+
+			sources, err := Sourcelistforscroll(buf)
+
+			if err != nil {
+				fmt.Println(len(capturetable))
+				return
+			}
+			for _, source := range sources["sourcelist"].([]map[string]interface{}) {
+				capturetable = append(capturetable, source["id"].(string))
+			}
+
+			scroll_id := sources["scroll_id"].(string)
+
+			//scroll璇锋眰澶�
+			scroll_url := "http://" + serverIp + ":" + serverPort + "/_search/scroll"
+
+			for {
+				var tmpList []string
+				next_scroll_id := ""
+				if next_scroll_id != "" {
+					scroll_id = next_scroll_id
+				}
+				jsonDSL := `{
+            "scroll": "1m", 
+            "scroll_id" : "` + scroll_id + `"
+        }`
+				//fmt.Println(scroll_url)
+				//fmt.Println(jsonDSL)
+				buf, err := EsReq("POST", scroll_url, []byte(jsonDSL))
+
+				if err != nil {
+					fmt.Println("lenth1: ", len(capturetable))
+					return
+				}
+				nextSources, err := Sourcelistforscroll(buf)
+
+				if nextSources == nil {
+					return
+				}
+
+				nextM := nextSources["sourcelist"].([]map[string]interface{})
+				//fmt.Println("id",nextSources)
+				if nextM == nil || len(nextM) == 0 {
+					//fmt.Println("lenth: ", len(capturetable))
+					return
+				}
+				//fmt.Println("id")
+				for _, source := range nextM {
+					tmpList = append(tmpList, source["id"].(string))
+				}
+				//fmt.Println("tmpList: ", len(tmpList))
+				lock.Lock()
+				capturetable = append(capturetable, tmpList...)
+				lock.Unlock()
+
+				next_scroll_id = nextSources["scroll_id"].(string)
+			}
+
+
+			fmt.Println(len(capturetable))
+
+		}()
+	}
+	wg.Wait()
+
+	//fmt.Println("lenth_all: ", len(capturetable))
+	//fmt.Println("鑰楁椂锛�", time.Since(ts))
+	return capturetable
 }
 
 //鑾峰彇搴曞簱浜哄憳ID
@@ -398,7 +469,7 @@
     url := "http://" + serverIp + ":" + serverPort +
         "/" + indexName + "/_search?search_type=dfs_query_then_fetch"
     fmt.Println(url)
-    fmt.Println(prama)
+    fmt.Println(prama)		
     buf, err := EsReq("POST", url,[]byte(prama))
     if err != nil {
         fmt.Println("http request videoUrlInfo info is err!")
@@ -415,4 +486,275 @@
         tabsource[tableId] = append(tabsource[tableId], id)
     }
     return tabsource
-}
\ No newline at end of file
+}
+
+//鍒濆鍖栧疄鏃舵姄鎷�
+func InitRealTimeCapture(serverIp string, serverPort string, indexName string, isAlarm bool	,quantity int) ([]protomsg.Videopersons, error){
+	var videopersonsInfo []protomsg.Videopersons
+	url := "http://" + serverIp + ":" + serverPort +
+		"/" + indexName + "/_search"
+	queryStr := ""
+	if isAlarm == true {
+		queryStr = `"query":{
+		"match_all":{}
+	},`
+	} else {
+		queryStr = `"query":{
+		"bool":{
+			"filter":[
+				{
+					"term":{
+						"isAlarm":1
+					}
+				}
+			]
+		}
+	},`
+	}
+	DSLJson := `{
+	"size":`+strconv.Itoa(quantity)+`,
+	`+queryStr+`
+	"sort":[{"picDate":{"order":"desc"}}],
+	"_source": ["baseInfo", "alarmRules", "sex", "analyServerName", "sdkName", "ageDescription", "content", "id", "cameraAddr", "picMaxUrl", "picDate", "race", "videoUrl", "picSmUrl", "taskName", "personIsHub", "isAlarm", "analyServerIp", "cameraId"]
+	}`
+	buf, err := EsReq("POST", url, []byte(DSLJson))
+	if err != nil {
+		return videopersonsInfo, err
+	}
+
+	sources, err := Sourcelist(buf)
+	if err != nil {
+		return videopersonsInfo, err
+	}
+
+	videoperson := Videopersonsbyid(sources)
+	//fmt.Println(len(videoperson))
+	return videoperson, nil
+}
+
+//瀹炴椂鎶撴媿
+func RealTimeCapture(serverIp string, serverPort string, indexName string, isAlarm bool	) ([]protomsg.Videopersons, error){
+	var videopersonsInfo []protomsg.Videopersons
+	url := "http://" + serverIp + ":" + serverPort +
+		"/" + indexName + "/_search"
+	queryStr := ""
+	if isAlarm == true {
+		fmt.Println("continue")
+	} else {
+		queryStr = `
+				{
+					"term":{
+						"isAlarm":1
+					}
+				}
+					`
+	}
+	DSLJson := `{
+	"size":20,
+	"query":{
+		"bool":{
+			"filter":[
+				{
+					"range":{
+						"picDate":{
+							"gte":"now+8h-30s",
+							"lt":"now+8h"	
+						}		
+					}
+				},
+			`+queryStr+`
+			]
+		}
+	},
+	"_source": ["baseInfo", "alarmRules", "sex", "analyServerName", "sdkName", "ageDescription", "content", "id", "cameraAddr", "picMaxUrl", "picDate", "race", "videoUrl", "picSmUrl", "taskName", "personIsHub", "isAlarm", "analyServerIp", "cameraId", "isAckAlarm"]
+	}`
+
+	buf, err := EsReq("POST", url, []byte(DSLJson))
+	if err != nil {
+		return videopersonsInfo, err
+	}
+
+	sources, err := Sourcelist(buf)
+	if err != nil {
+		return videopersonsInfo, err
+	}
+
+	videoperson := Videopersonsbyid(sources)
+	fmt.Println(len(videoperson))
+	return videoperson, nil
+}
+
+//缁煎悎缁熻
+func StatisticsComprehensive(serverIp string, serverPort string, indexName string, isAlarm bool) (total int, err error){
+	isAlarmStr := ""
+	if isAlarm == true {
+		isAlarmStr = `,{"term":{"isAlarm":1}}`
+	}
+	url := "http://" + serverIp + ":" + serverPort +
+		"/" + indexName + "/_search"
+	DSLJson := `{
+	"size":0,
+	"query":{
+		"bool":{
+			"filter":[{
+				"range":{
+					"picDate":{
+						"gte":"now+8h/d"
+						}
+					}
+				}
+				`+isAlarmStr+`
+				]
+			}
+		}
+	}`
+	//fmt.Println(DSLJson)
+	buf, err := EsReq("POST",url,[]byte(DSLJson))
+	if err != nil {
+		return total, err
+	}
+	var info interface{}
+	json.Unmarshal(buf, &info)
+	out, ok := info.(map[string]interface{})
+	if !ok {
+		return total, errors.New("http response interface can not change map[string]interface{}")
+	}
+	middle, ok := out["hits"].(map[string]interface{})
+	if !ok {
+		return total, errors.New("first hits change error!")
+	}
+	total = int(middle["total"].(float64))
+	//fmt.Println(total)
+	return total,nil
+}
+
+//瀹炴椂鎶ヨ浠诲姟姣旂巼
+func RealTimeAlarmTaskRate(serverIp string, serverPort string, indexName string) (sources []map[string]interface{},err error){
+	url := "http://" + serverIp + ":" + serverPort +
+		"/"+indexName+"/_search"
+	DSLJson := `{
+	"size":0,
+	"query":{
+		"bool":{
+			"filter":[{
+				"range":{
+					"picDate":{
+						"gte":"now+8h/d"
+					}
+				}
+			}]
+		}
+	},
+	"aggs":{
+		"sdkName_status":{
+			"terms":{
+				"field":"taskName.raw"
+			}
+		}
+	}
+}`
+	buf, err := EsReq("POST",url,[]byte(DSLJson))
+	if err != nil {
+		return nil, err
+	}
+	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!")
+	}
+	sdkName_status, ok := middle["sdkName_status"].(map[string]interface{})
+	if !ok {
+		return nil, errors.New("first hits change error!")
+	}
+
+	for _, in := range sdkName_status["buckets"].([]interface{}){
+		var source = make(map[string]interface{},0)
+		tmpbuf, ok := in.(map[string]interface{})
+		if !ok {
+			fmt.Println("change to source error!")
+			continue
+		}
+		sdkName := tmpbuf["key"].(string)
+		count := int(tmpbuf["doc_count"].(float64))
+		source["name"] = sdkName
+		source["value"] = count
+		sources = append(sources, source)
+	}
+	//fmt.Println("tmpSource",sources)
+	return sources,nil
+}
+
+//鑱氬悎浠诲姟鍒楄〃锛宼askId+taskName
+func AggregateTaskList(serverIp string, serverPort string, indexName string) (sources []map[string]interface{},err error){
+	url := "http://" + serverIp + ":" + serverPort +
+		"/"+indexName+"/_search"
+	DSLJson := `{
+    "size": 0,
+    "aggs": {
+        "task_status": {
+            "composite": {
+                "sources": [
+                    {
+                        "taskId": {
+                            "terms": {
+                                "field": "taskId"
+                            }
+                        }
+                    },
+                    {
+                        "taskName": {
+                            "terms": {
+                                "field": "taskName.raw"
+                            }
+                        }
+                    }
+                ],
+                "size":"1000"
+            }
+        }
+    }
+}`
+	buf, err := EsReq("POST",url,[]byte(DSLJson))
+	if err != nil {
+		return nil, err
+	}
+	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!")
+	}
+	task_status, ok := middle["task_status"].(map[string]interface{})
+	if !ok {
+		return nil, errors.New("first hits change error!")
+	}
+
+	for _, in := range task_status["buckets"].([]interface{}){
+		var source = make(map[string]interface{},0)
+		tmpbuf, ok := in.(map[string]interface{})
+		if !ok {
+			fmt.Println("change to source error!")
+			continue
+		}
+		task := tmpbuf["key"].(map[string]interface{})
+		count := int(tmpbuf["doc_count"].(float64))
+		taskName := task["taskName"].(string)
+		taskId := task["taskId"].(string)
+		source["taskName"] = taskName
+		source["taskId"] = taskId
+		source["count"] = count
+		sources = append(sources, source)
+	}
+	//fmt.Println("tmpSource",sources)
+	return sources,nil
+
+}

--
Gitblit v1.8.0