package controllers
|
|
import (
|
"basic.com/valib/logger.git"
|
"bytes"
|
"encoding/json"
|
"fmt"
|
"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"
|
"webserver/service"
|
)
|
|
type EsController struct{}
|
|
// @Security ApiKeyAuth
|
// @Summary 比对数据查询
|
// @Description 比对数据查询
|
// @Accept json
|
// @Produce json
|
// @Tags es
|
// @Param reqMap body models.EsSearch true "collection 为空"
|
// @Success 200 {string} json "{"code":200, msg:"", success:true}"
|
// @Failure 500 {string} json "{"code":500, msg:"", success:false}"
|
// @Router /data/api-v/es/queryEsCompareData [POST]
|
func PostEsCompareData(c *gin.Context) {
|
searchBody := new(models.EsSearch)
|
err := c.BindJSON(&searchBody)
|
if err != nil || searchBody.PicUrl == "" || len(searchBody.DataBases) == 0 {
|
util.ResponseFormat(c, code.RequestParamError, "参数有误")
|
return
|
}
|
if searchBody.CompareNum !="" {
|
//二次搜索,不需要再比对了
|
co := service.GetCompResultByNum(searchBody.CompareNum)
|
if co != nil {
|
//二次搜索和排序
|
twiceM := GetCompareDataTwice(co,searchBody)
|
util.ResponseFormat(c,code.Success,twiceM)
|
return
|
} else {
|
m :=make(map[string]interface{},0)
|
m["compareNum"] = searchBody.CompareNum
|
m["total"] = 0
|
m["totalList"] = []CompareResult{}
|
util.ResponseFormat(c,code.CompareResultGone,m)
|
return
|
}
|
}
|
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 + "\"]}},"
|
}
|
//判断摄像机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{} {
|
var filterName = "feature"
|
if isEsSource {
|
filterName = "FaceFeature"
|
}
|
fmt.Println("查询" + filterName)
|
dataSource := make([]interface{}, 0, 20)
|
for _, obj := range sources {
|
source := obj.(map[string]interface{})
|
//feature := source[filterName].(string) // linux
|
//featByte, _ := base64.StdEncoding.DecodeString(feature) // linux
|
//score := gosdk.FaceCompare(featByte, campareByte) // linux
|
score := rand.Float32() // windows
|
if score >= threshold*0.01 {
|
source["score"] = int(score * 100)
|
dataSource = append(dataSource, source)
|
}
|
}
|
return dataSource
|
}
|
|
func GetEsClusterInfo(ip string) ([]map[string]interface{}, error){
|
serverIp := ""
|
if ip != "" {
|
serverIp = ip
|
} else {
|
localConf, err2 := cache.GetServerInfo()
|
if err2 !=nil || localConf.ServerIp == "" {
|
logger.Debug("localConfig is wrong!!!")
|
return nil,err2
|
}
|
serverIp = localConf.ServerIp
|
}
|
str := "curl "+serverIp+":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
|
}
|
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{})
|
context := strings.Fields(infos[i])
|
nodeIp := context[0]
|
Type := context[8]
|
var nodeType string
|
if Type == "*"{
|
nodeType = "主节点"
|
}
|
if Type == "-"{
|
nodeType = "从节点"
|
}
|
nodeInfo["ip"] = nodeIp
|
nodeInfo["nodeType"] = nodeType
|
url := "http://"+nodeIp+":9200"
|
buf := esutil.HttpGet(url)
|
var info interface{}
|
json.Unmarshal(buf,&info)
|
tmpInfo := info.(map[string]interface{})
|
tmpName := tmpInfo["name"].(string)
|
versinInfo := tmpInfo["version"].(map[string]interface{})
|
buildDate := versinInfo["build_date"].(string)
|
nodeInfo["name"] = tmpName
|
nodeInfo["buildDate"] = buildDate
|
nodeInfos = append(nodeInfos, nodeInfo)
|
}
|
|
return nodeInfos,nil
|
}
|
|
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)
|
var out bytes.Buffer
|
cmd.Stdout = &out
|
err := cmd.Run()
|
if err != nil {
|
|
}
|
infos := strings.Split(string(out.String()),"\n")[0]
|
return infos
|
|
}
|