liuxiaolong
2020-06-05 7c811247ecf143e08c576986a884bedadc57dd66
controllers/es.go
@@ -8,11 +8,9 @@
   "github.com/gin-gonic/gin"
   "math/rand"
   "os/exec"
   "strconv"
   "strings"
   "webserver/cache"
   "webserver/extend/code"
   "webserver/extend/config"
   "webserver/extend/esutil"
   "webserver/extend/util"
   "webserver/models"
@@ -66,81 +64,6 @@
   m["total"] = 0
   m["totalList"] = []CompareResult{}
   util.ResponseFormat(c, code.CompareResultGone, m)
}
func searchEsData(searchBody models.EsSearch) map[string]interface{} {
   //请求索引
   index := config.EsInfo.EsIndex.VideoPersons.IndexName //  wp只查人脸数据
   queryStr := ""
   queryBody := searchBody.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 := searchBody.SearchTime[0]
   lteDate := searchBody.SearchTime[1]
   //判断任务ID
   taskIdStr := ""
   taskId := searchBody.Tasks
   if taskId != nil && len(taskId) > 0 {
      esTaskId := strings.Replace(strings.Trim(fmt.Sprint(taskId), "[]"), " ", "\",\"", -1)
      taskIdStr = "{\"terms\":{\"taskId\":[\"" + esTaskId + "\"]}},"
   }
   //判断摄像机ID
   cameraIdStr := ""
   cameraId := searchBody.TreeNodes
   if cameraId != nil && len(cameraId) > 0 {
      esCameraId := strings.Replace(strings.Trim(fmt.Sprint(cameraId), "[]"), " ", "\",\"", -1)
      cameraIdStr = "{\"terms\":{\"cameraId\":[\"" + esCameraId + "\"]}},"
   }
   //判断库表ID
   tableId := searchBody.Tabs
   esTableId := ""
   esTableIdStr := ""
   if tableId != nil && len(tableId) > 0 {
      esTableId = strings.Replace(strings.Trim(fmt.Sprint(tableId), "[]"), " ", "\",\"", -1)
      esTableIdStr = "{\"terms\":{\"baseInfo.tableId\":[\"" + esTableId + "\"]}},"
   }
   isCollectionStr := ""
   isCollection := searchBody.Collection
   if isCollection != "" {
      isCollectionStr = "{\"term\":{\"collection\":\"" + isCollection + "\"}},"
   }
   webPage := searchBody.Page
   webSize := searchBody.Size
   from := (webPage - 1) * webSize
   esFrom := strconv.Itoa(from)
   esSize := strconv.Itoa(webSize)
   //使用es底层机制处理分页
   //请求头
   localConf, err2 := cache.GetServerInfo()
   if err2 != nil || localConf.AlarmIp == "" || localConf.ServerId == "" {
      logger.Debug("localConfig is wrong!!!")
      return nil
   }
   url := "http://" + localConf.AlarmIp + ":" + strconv.Itoa(int(localConf.AlarmPort)) +
      "/" + index + "/_search?search_type=dfs_query_then_fetch"
   analyServerFilterStr := "{\"term\":{\"analyServerId\":\"" + localConf.ServerId + "\"}},"
   //请求体
   prama := "{\"from\":\"" + esFrom + "\",\"size\":\"" + esSize + "\"," +
      //   prama := "{\"size\":\"0\"," +
      "\"query\":{\"bool\":{" + queryStr +
      "\"filter\":[" +
      cameraIdStr +
      taskIdStr +
      isCollectionStr +
      esTableIdStr +
      analyServerFilterStr +
      "{\"range\":{\"picDate\":{\"from\":\"" + gteDate + "\",\"to\":\"" + lteDate + "\",\"include_lower\":true,\"include_upper\":true,\"boost\":1}}}]}}," +
      "\"sort\":[{\"_score\":{\"order\":\"desc\"}},{\"picDate\":{\"order\":\"desc\"}}]," +
      "\"_source\":[\"baseInfo\",\"alarmRules\",\"sex\",\"analyServerName\",\"sdkName\",\"ageDescription\",\"content\",\"id\",\"cameraAddr\",\"picMaxUrl\",\"picDate\",\"race\",\"videoUrl\",\"picSmUrl\",\"taskName\",\"personIsHub\",\"isAlarm\",\"analyServerIp\",\"cameraId\"]}"
   fmt.Println(prama)
   //数据解析
   tokenRes := esutil.GetEsDataReq(url, prama, true)
   return tokenRes
}
func sourceCompare(sources []interface{}, isEsSource bool, campareByte []byte, threshold float32) []interface{} {
@@ -395,12 +318,14 @@
// @Failure 500 {string} json "{"code":500, msg:"", success:false}"
// @Router /data/api-v/es/updateEsHosts [POST]
func (em *EsManagementController) UpdateEsHosts(c *gin.Context) {
   flag := "修改成功"
   var hosts Hosts
   c.BindJSON(&hosts)
   nodeInfos, err := getEsClusterInfors(hosts.OldIp)
   var nodeType string
   if err != nil {
      logger.Fatal(err)
      util.ResponseFormat(c, code.QueryClusterInfoErr, err)
   }
   newMasterHosts := make([]string, 0)
   allHosts := make([]string, 0)
@@ -410,7 +335,7 @@
      if ip == hosts.OldIp {
         nodeType = tmpType
      }
      if tmpType == "master" {
      if tmpType == "主节点" {
         newMasterHosts = append(newMasterHosts, ip)
      }
      if ip != hosts.OldIp {
@@ -418,9 +343,12 @@
      }
   }
   if nodeType == "master" {
   if nodeType == "主节点" {
      str1 := "sed -ie 's/network.host: " + hosts.OldIp + "/network.host: " + hosts.NewIp + "/g' /opt/elasticsearch/config/elasticsearch.yml"
      runScript(str1)
      msg1 := runScript(str1)
      if msg1 == "运行失败" {
         flag = "修改配置文件失败"
      }
      for i, host := range newMasterHosts {
         if host == hosts.OldIp {
            newMasterHosts[i] = hosts.NewIp
@@ -439,24 +367,34 @@
      outs := strings.Replace(ots, "\"", "\\\"", -1)
      oldUnicastHost := "\\" + strings.Replace(outs, "]", "\\]", -1)
      str2 := "sed -ie 's/discovery.zen.ping.unicast.hosts: " + oldUnicastHost + "/discovery.zen.ping.unicast.hosts: " + newUnicastHost + "/g' /opt/elasticsearch/config/elasticsearch.yml"
      runScript(str2)
      msg2 := runScript(str2)
      if msg2 == "运行失败" {
         flag = "修改配置文件失败"
      }
      for _, host := range allHosts {
         str3 := "sshpass -p \"123\" ssh basic@" + host + " \"cd /opt/elasticsearch/config  ; " + str2 + " ; cat elasticsearch.yml\""
         runScript(str3)
         msg := runScript(str3)
         if msg == "运行失败" {
            flag = "修改配置文件失败"
         }
      }
   } else {
      str1 := "sed -ie 's/network.host: " + hosts.OldIp + "/network.host: " + hosts.NewIp + "/g' /opt/elasticsearch/config/elasticsearch.yml"
      runScript(str1)
      msg1 := runScript(str1)
      if msg1 == "运行失败" {
         flag = "修改配置文件失败"
      }
   }
   if flag == "修改配置文件失败" {
      util.ResponseFormat(c, code.UpdateFail, flag)
   }
   util.ResponseFormat(c, code.Success, flag)
}
type Hosts struct {
   NewIp string `json:newIp`
   OldIp string `json:oldIp`
}
func forwardCommand() {
}
//func AddEsCluster(hosts []string) (string){