From 7c811247ecf143e08c576986a884bedadc57dd66 Mon Sep 17 00:00:00 2001 From: liuxiaolong <liuxiaolong@aiotlink.com> Date: 星期五, 05 六月 2020 18:29:41 +0800 Subject: [PATCH] add refresh token to resp --- controllers/es.go | 444 +++++++++++++++++++++++++++++++++++++++---------------- 1 files changed, 315 insertions(+), 129 deletions(-) diff --git a/controllers/es.go b/controllers/es.go index dd53714..9a9f8fb 100644 --- a/controllers/es.go +++ b/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" @@ -20,6 +18,12 @@ ) type EsController struct{} + +type EsManagementController struct{} + +type EsClusterInfo struct { + Ip string `json:"ip"` +} // @Security ApiKeyAuth // @Summary 姣斿鏁版嵁鏌ヨ @@ -38,103 +42,28 @@ util.ResponseFormat(c, code.RequestParamError, "鍙傛暟鏈夎") return } - if searchBody.CompareNum !="" { + if searchBody.CompareNum != "" { //浜屾鎼滅储锛屼笉闇�瑕佸啀姣斿浜� co := service.GetCompResultByNum(searchBody.CompareNum) if co != nil { //浜屾鎼滅储鍜屾帓搴� - twiceM := GetCompareDataTwice(co,searchBody) - util.ResponseFormat(c,code.Success,twiceM) + twiceM := GetCompareDataTwice(co, searchBody) + util.ResponseFormat(c, code.Success, twiceM) return } else { - m :=make(map[string]interface{},0) + m := make(map[string]interface{}, 0) m["compareNum"] = searchBody.CompareNum m["total"] = 0 m["totalList"] = []CompareResult{} - util.ResponseFormat(c,code.CompareResultGone,m) + util.ResponseFormat(c, code.CompareResultGone, m) return } } - m :=make(map[string]interface{},0) + m := make(map[string]interface{}, 0) m["compareNum"] = searchBody.CompareNum 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 + "\"]}}," - } - //鍒ゆ柇鎽勫儚鏈篒D - 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) - //鏁版嵁瑙f瀽 - tokenRes := esutil.GetEsDataReq(url, prama, true) - return tokenRes + util.ResponseFormat(c, code.CompareResultGone, m) } func sourceCompare(sources []interface{}, isEsSource bool, campareByte []byte, threshold float32) []interface{} { @@ -158,47 +87,71 @@ return dataSource } -func GetEsClusterInfo(ip string) ([]map[string]interface{}, error){ +// @Security ApiKeyAuth +// @Summary 鏌ヨES闆嗙兢淇℃伅-鍏ュ彛 +// @Description 鏌ヨES闆嗙兢淇℃伅-鍏ュ彛 +// @Accept json +// @Produce json +// @Tags es +// @Param obj body controllers.EsClusterInfo true "鏌ヨ闆嗙兢鍙傛暟" +// @Success 200 {string} json "{"code":200, msg:"", success:true}" +// @Failure 500 {string} json "{"code":500, msg:"", success:false}" +// @Router /data/api-v/es/getEsClusterInfo [POST] +func (em *EsManagementController) GetEsClusterInfo(c *gin.Context) { + var body EsClusterInfo + c.BindJSON(&body) + ip := body.Ip serverIp := "" if ip != "" { serverIp = ip } else { localConf, err2 := cache.GetServerInfo() - if err2 !=nil || localConf.ServerIp == "" { + if err2 != nil || localConf.ServerIp == "" { logger.Debug("localConfig is wrong!!!") - return nil,err2 + util.ResponseFormat(c, code.QueryClusterInfoErr, err2) + return } serverIp = localConf.ServerIp } - str := "curl "+serverIp+":9200/_cat/nodes?v" - cmd := exec.Command("sh","-c",str) + nodeInfos, err := getEsClusterInfors(serverIp) + if err != nil { + util.ResponseFormat(c, code.QueryClusterInfoErr, err) + return + } + util.ResponseFormat(c, code.Success, nodeInfos) +} + +//鏌ヨES闆嗙兢淇℃伅-涓氬姟閫昏緫 +func getEsClusterInfors(ip string) ([]map[string]interface{}, error) { + str := "curl " + ip + ":9200/_cat/nodes?v" + cmd := exec.Command("sh", "-c", str) var out bytes.Buffer cmd.Stdout = &out err := cmd.Run() if err != nil { - return nil,err + return nil, err } - infos := strings.Split(string(out.String()),"\n") - totalNodes := len(infos)-1 + infos := strings.Split(string(out.String()), "\n") + totalNodes := len(infos) - 1 var nodeInfos []map[string]interface{} - for i:=1;i<totalNodes ;i++ { - nodeInfo := make(map[string]interface{}) + for i := 1; i < totalNodes; i++ { + nodeInfo := make(map[string]interface{}) context := strings.Fields(infos[i]) nodeIp := context[0] - Type := context[8] + Type := context[7] var nodeType string - if Type == "*"{ + if Type == "mdi" { nodeType = "涓昏妭鐐�" } - if Type == "-"{ + if Type == "di" { nodeType = "浠庤妭鐐�" } nodeInfo["ip"] = nodeIp nodeInfo["nodeType"] = nodeType - url := "http://"+nodeIp+":9200" + url := "http://" + nodeIp + ":9200" buf := esutil.HttpGet(url) var info interface{} - json.Unmarshal(buf,&info) + json.Unmarshal(buf, &info) tmpInfo := info.(map[string]interface{}) tmpName := tmpInfo["name"].(string) versinInfo := tmpInfo["version"].(map[string]interface{}) @@ -207,61 +160,294 @@ nodeInfo["buildDate"] = buildDate nodeInfos = append(nodeInfos, nodeInfo) } - - return nodeInfos,nil + return nodeInfos, err } -func AddEsCluster(hosts []string) (string){ - msg := "鍔犲叆澶辫触" - for i,val := range hosts{ - val =val+":9300" +// @Security ApiKeyAuth +// @Summary 鍒涘缓鑺傜偣 +// @Description 鍒涘缓鑺傜偣 +// @Accept json +// @Produce json +// @Tags es +// @Success 200 {string} json "{"code":200, msg:"", success:true}" +// @Failure 500 {string} json "{"code":500, msg:"", success:false}" +// @Router /data/api-v/es/createNode [POST] +func (em *EsManagementController) CreateNode(c *gin.Context) { + msg := "鍒涘缓鑺傜偣澶辫触锛岃鑱旂郴绠$悊鍛�" + str := "sh /opt/script/create_first_node.sh" + cmd := exec.Command("sh", "-c", str) + var out bytes.Buffer + cmd.Stdout = &out + err := cmd.Run() + if err != nil { + + } + infos := strings.Split(string(out.String()), "\n")[0] + if infos == "鏈嶅姟鍚姩鎴愬姛" { + msg = "鍒涘缓鑺傜偣鎴愬姛" + util.ResponseFormat(c, code.Success, msg) + return + } + util.ResponseFormat(c, code.CreateFirstNodeErr, msg) +} + +// @Security ApiKeyAuth +// @Summary 鍔犲叆闆嗙兢 +// @Description 鍔犲叆闆嗙兢 +// @Accept json +// @Produce json +// @Tags es +// @Param obj body controllers.AddCluster true "鍔犲叆闆嗙兢鍙傛暟" +// @Success 200 {string} json "{"code":200, msg:"", success:true}" +// @Failure 500 {string} json "{"code":500, msg:"", success:false}" +// @Router /data/api-v/es/addCluster [POST] +func (em *EsManagementController) AddCluster(c *gin.Context) { + var ac AddCluster + err := c.BindJSON(&ac) + if err != nil { + util.ResponseFormat(c, code.RequestParamError, "鍙傛暟鏈夎") + return + } + str := "sh /opt/script/add_cluster.sh " + ac.Option + "" + if ac.Option == "1" { + info, err := updateUnicastHosts(ac.Ip) + if err != nil || info == false { + util.ResponseFormat(c, code.QueryClusterInfoErr, err) + return + } + if info == true { + info := runScript(str) + if info == "杩愯澶辫触" { + util.ResponseFormat(c, code.AddClusterInfoErr, info) + return + } + } + } + if ac.Option == "2" { + info, err := updateUnicastHosts(ac.Ip) + if err != nil || info == false { + util.ResponseFormat(c, code.QueryClusterInfoErr, err) + return + } + if info == true { + info := runScript(str) + if info == "杩愯澶辫触" { + util.ResponseFormat(c, code.AddClusterInfoErr, info) + return + } + } + } else { + util.ResponseFormat(c, code.RequestParamError, "鍙傛暟鏈夎") + return + } + util.ResponseFormat(c, code.Success, "鍔犲叆鎴愬姛") +} + +//鑴氭湰灏佽 +func runScript(str string) string { + + cmd := exec.Command("sh", "-c", str) + var out bytes.Buffer + cmd.Stdout = &out + err := cmd.Run() + if err != nil { + return "杩愯澶辫触" + } + return out.String() +} + +type AddCluster struct { + Ip string `json:"ip"` //闆嗙兢IP + Option string `json:option` //鑺傜偣绫诲瀷 +} + +//鏇存柊缁勬挱鍒楄〃 +func updateUnicastHosts(host string) (bool, error) { + nodeInfos, err := getEsClusterInfors(host) + if err != nil { + return false, err + } + var hosts []string + for _, val := range nodeInfos { + nodeType := val["nodeType"].(string) + if nodeType == "涓昏妭鐐�" { + ip := val["ip"].(string) + hosts = append(hosts, ip) + } + } + msg := false + for i, val := range hosts { + val = val + ":9300" hosts[i] = val } - verificationHosts := "[\""+strings.Replace(strings.Trim(fmt.Sprint(hosts), "[]"), " ", "\",\"", -1)+"\"]" - for i,val := range hosts{ - val ="\\\""+val+"\\\"" + verificationHosts := "[\"" + strings.Replace(strings.Trim(fmt.Sprint(hosts), "[]"), " ", "\",\"", -1) + "\"]" + for i, val := range hosts { + val = "\\\"" + val + "\\\"" hosts[i] = val } oldUnicastHost := "\\[\\\"0.0.0.0:9300\\\"\\]" newUnicastHost := strings.Replace(strings.Trim(fmt.Sprint(hosts), ""), " ", ",", -1) - str := "sed -ie 's/discovery.zen.ping.unicast.hosts: "+oldUnicastHost+"/discovery.zen.ping.unicast.hosts: "+newUnicastHost+"/g' /opt/elasticsearch/config/elasticsearch.yml" + str := "sed -ie 's/discovery.zen.ping.unicast.hosts: " + oldUnicastHost + "/discovery.zen.ping.unicast.hosts: " + newUnicastHost + "/g' /opt/elasticsearch/config/elasticsearch.yml" fmt.Println(str) - cmd := exec.Command("sh","-c",str) + cmd := exec.Command("sh", "-c", str) var out bytes.Buffer cmd.Stdout = &out - err := cmd.Run() - if err != nil { - + err1 := cmd.Run() + if err1 != nil { + return false, err } res := getUnicastHosts() - fmt.Println("res: ",res) - res1 := "discovery.zen.ping.unicast.hosts: "+verificationHosts+"" - fmt.Println("res1: ",res1) - if res == res1{ - msg = "鍔犲叆鎴愬姛" + fmt.Println("res: ", res) + res1 := "discovery.zen.ping.unicast.hosts: " + verificationHosts + "" + fmt.Println("res1: ", res1) + if res == res1 { + msg = true } - str2 := "echo \"node.master: true\" >> /opt/elasticsearch/config/elasticsearch.yml" - cmd2 := exec.Command("sh","-c",str2) - var out2 bytes.Buffer - cmd2.Stdout = &out2 - err2 := cmd2.Run() - if err2 != nil { - msg = "鍔犲叆澶辫触" + return msg, err +} + +//淇敼elasticsearch.yml鏄犲皠鏂囦欢 +// @Security ApiKeyAuth +// @Summary 淇敼es闆嗙兢Ip +// @Description 淇敼es闆嗙兢Ip +// @Accept json +// @Produce json +// @Tags es +// @Param obj body controllers.Hosts true "淇敼es闆嗙兢Ip鍙傛暟" +// @Success 200 {string} json "{"code":200, msg:"", success:true}" +// @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) } - return msg + newMasterHosts := make([]string, 0) + allHosts := make([]string, 0) + for _, val := range nodeInfos { + ip := val["ip"].(string) + tmpType := val["nodeType"].(string) + if ip == hosts.OldIp { + nodeType = tmpType + } + if tmpType == "涓昏妭鐐�" { + newMasterHosts = append(newMasterHosts, ip) + } + if ip != hosts.OldIp { + allHosts = append(allHosts, ip) + } + } + + if nodeType == "涓昏妭鐐�" { + str1 := "sed -ie 's/network.host: " + hosts.OldIp + "/network.host: " + hosts.NewIp + "/g' /opt/elasticsearch/config/elasticsearch.yml" + msg1 := runScript(str1) + if msg1 == "杩愯澶辫触" { + flag = "淇敼閰嶇疆鏂囦欢澶辫触" + } + for i, host := range newMasterHosts { + if host == hosts.OldIp { + newMasterHosts[i] = hosts.NewIp + } + } + for i, val := range newMasterHosts { + val = "\\\"" + val + ":9300\\\"" + newMasterHosts[i] = val + } + newUnicastHost := strings.Replace(strings.Trim(fmt.Sprint(newMasterHosts), ""), " ", ",", -1) + + tmpStr := "cat /opt/elasticsearch/config/elasticsearch.yml | grep discovery.zen.ping.unicast.hosts:" + rs := runScript(tmpStr) + ts := strings.Split(rs, "\n")[0] + ots := strings.Split(ts, " ")[1] + 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" + 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\"" + 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" + msg1 := runScript(str1) + if msg1 == "杩愯澶辫触" { + flag = "淇敼閰嶇疆鏂囦欢澶辫触" + } + } + if flag == "淇敼閰嶇疆鏂囦欢澶辫触" { + util.ResponseFormat(c, code.UpdateFail, flag) + } + util.ResponseFormat(c, code.Success, flag) } -func getUnicastHosts() (string){ +type Hosts struct { + NewIp string `json:newIp` + OldIp string `json:oldIp` +} + +//func AddEsCluster(hosts []string) (string){ +// msg := "鍔犲叆澶辫触" +// for i,val := range hosts{ +// val =val+":9300" +// hosts[i] = val +// } +// verificationHosts := "[\""+strings.Replace(strings.Trim(fmt.Sprint(hosts), "[]"), " ", "\",\"", -1)+"\"]" +// for i,val := range hosts{ +// val ="\\\""+val+"\\\"" +// hosts[i] = val +// } +// oldUnicastHost := "\\[\\\"0.0.0.0:9300\\\"\\]" +// newUnicastHost := strings.Replace(strings.Trim(fmt.Sprint(hosts), ""), " ", ",", -1) +// str := "sed -ie 's/discovery.zen.ping.unicast.hosts: "+oldUnicastHost+"/discovery.zen.ping.unicast.hosts: "+newUnicastHost+"/g' /opt/elasticsearch/config/elasticsearch.yml" +// fmt.Println(str) +// cmd := exec.Command("sh","-c",str) +// var out bytes.Buffer +// cmd.Stdout = &out +// err := cmd.Run() +// if err != nil { +// +// } +// res := getUnicastHosts() +// fmt.Println("res: ",res) +// res1 := "discovery.zen.ping.unicast.hosts: "+verificationHosts+"" +// fmt.Println("res1: ",res1) +// if res == res1{ +// msg = "鍔犲叆鎴愬姛" +// } +// str2 := "echo \"node.master: true\" >> /opt/elasticsearch/config/elasticsearch.yml" +// cmd2 := exec.Command("sh","-c",str2) +// var out2 bytes.Buffer +// cmd2.Stdout = &out2 +// err2 := cmd2.Run() +// if err2 != nil { +// msg = "鍔犲叆澶辫触" +// } +// return msg +// +//} + +func getUnicastHosts() string { str := "cat /opt/elasticsearch/config/elasticsearch.yml | grep discovery.zen.ping.unicast.hosts:" - cmd := exec.Command("sh","-c",str) + cmd := exec.Command("sh", "-c", str) var out bytes.Buffer cmd.Stdout = &out err := cmd.Run() if err != nil { } - infos := strings.Split(string(out.String()),"\n")[0] + infos := strings.Split(string(out.String()), "\n")[0] return infos } -- Gitblit v1.8.0