fix
sunty
2020-10-23 e189b1942c8130d473a1a4128c3d9ef5edfaa564
EsApi.go
@@ -180,7 +180,7 @@
/*******************sort []map util*******************/
//根据时间范围聚合所有区域人信息,返回固定条数
func GetFaceDataByTimeAndTotal(startTime string, total int, thresholdTime int, thresholdStayTime int, serverIp string, serverPort string, indexName string) (resData []map[string]interface{}, err error) {
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": {
@@ -189,7 +189,8 @@
                {
                    "range": {
                        "picDate": {
                            "gte": "` + startTime + `"
                            "gte": "` + startTime + `",
                     "lte": "` + endTime + `"
                        }
                    }
                },
@@ -256,20 +257,23 @@
   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 {
      mapsSort := MapsSort{}
      mapsSort.Key = "endTime"
      mapsSort.MapList = faceSource
      sort.Sort(&mapsSort)
      return mapsSort.MapList[:total], nil
   }
   return faceSource, 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) {
@@ -354,16 +358,23 @@
   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])
      }
   }
   return faceSource, nil
   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 []string, err error) {
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": {
@@ -399,7 +410,27 @@
                    }
                ],
                "size": 10000000
            }
            },
               "aggs": {
                   "top_attention_hits": {
                       "top_hits": {
                           "size": 1,
                           "sort": [
                               {
                                   "picDate": {
                                       "order": "desc"
                                   }
                               }
                           ],
                           "_source": {
                               "includes": [
                                   "picDate"
                               ]
                           }
                       }
                      }
                  }
           }
        }
    }
}`
@@ -413,11 +444,18 @@
   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){
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": {
@@ -457,15 +495,15 @@
        }
    }
}`
   buf,err := EsReq("POST",requestUrl,[]byte(requestBody))
   if err != nil{
      return nil,err
   buf, err := EsReq("POST", requestUrl, []byte(requestBody))
   if err != nil {
      return nil, err
   }
   result,err := SourceStatistics(buf)
   if err != nil{
      return nil,err
   result, err := SourceStatistics(buf)
   if err != nil {
      return nil, err
   }
   return result,nil
   return result, nil
}
/**************************************customer analysis util end**************************************/
@@ -1047,7 +1085,7 @@
   if category != "all" {
      filterArr = append(filterArr, `            {
               "term":{
                  "targetInfo.targetType":"`+category+`"
                  "targetInfo.targetType.raw":"`+category+`"
               }
            }`)
@@ -1067,7 +1105,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
@@ -1157,7 +1195,6 @@
         }
      }
   }`
   //logPrint(DSLJson)
   buf, err := EsReq("POST", url, []byte(DSLJson))
   if err != nil {
      return total, err
@@ -1195,9 +1232,9 @@
      }
   },
   "aggs":{
      "sdkName_status":{
      "taskName_status":{
         "terms":{
            "field":"sdkName.raw"
            "field":"taskName.raw"
         }
      }
   }
@@ -1216,11 +1253,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{})
@@ -1698,3 +1735,66 @@
   }
   return result, nil
}
type ShardInfo struct {
   ShardIndex string `json:"shardIndex"` //分片所属索引名称
   ShardNum   int    `json:"shardNum"`   //分片号
   ShardRole  string `json:"shardRole"`  //分片角色(主分片:primary 副本分片:replica)
   ShardState string `json:"shardState"` //分片状态(启用:STARTED 未启用:UNASSIGNED)
   ShardDocs  int    `json:"shardDocs"`  //分片已保存文档数
   ShardStore string `json:"shardStore"` //分片当前存储数据大小
   ShardIp    string `json:"shardIp"`    //分片所在节点ip
   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
}