putonghao
2022-09-14 705f76d542397154da2acf5461f2888828cbd5b8
EsApi.go
@@ -257,8 +257,8 @@
   if err != nil {
      return nil, err
   }
   if len(source) == 0{
      return source,nil
   if len(source) == 0 {
      return source, nil
   }
   faceSource := make([]map[string]interface{}, 0)
   for index, info := range source {
@@ -358,8 +358,8 @@
   if err != nil {
      return nil, err
   }
   if len(source) == 0{
      return source,nil
   if len(source) == 0 {
      return source, nil
   }
   faceSource := make([]map[string]interface{}, 0)
   for index, info := range source {
@@ -888,18 +888,25 @@
      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
}
@@ -944,7 +951,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}},"
      }
   }
   //判断布防等级
@@ -1276,10 +1288,19 @@
}
//聚合任务列表,taskId+taskName
func AggregateTaskList(serverIp string, serverPort string, indexName string, analyServerId string) (sources []map[string]interface{}, err error) {
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": {
@@ -1288,8 +1309,9 @@
            {
            "term": {
            "analyServerId": "` + analyServerId + `"
               }
            }
            }
             ` + cameIdFilterStr + `
         ]
         }
      }`
@@ -1621,7 +1643,7 @@
}
//按日期范围,服务器Id删除数据
func DeleteAnalyServerData(serverIp string, serverPort string, indexName string, startTime string, endTime string, analyServerId string) (result bool, err error) {
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":{
@@ -1643,20 +1665,17 @@
      }
   }
}   `
   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("解码失败")
      return -1, errors.New("解码失败")
   }
   if deleteRes == -1 {
      result = false
   } else {
      result = true
   }
   return result, nil
   return deleteRes,nil
}
//给所有节点追加删除任务信息
@@ -1735,3 +1754,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
}