From f4e8f206a6760bdc31734dfcb1c65916b5b76311 Mon Sep 17 00:00:00 2001
From: liuxiaolong <736321739@qq.com>
Date: 星期四, 22 八月 2019 12:05:58 +0800
Subject: [PATCH] add cluster
---
controllers/es.go | 177 +++++++++++-----------------------------------------------
1 files changed, 34 insertions(+), 143 deletions(-)
diff --git a/controllers/es.go b/controllers/es.go
index 68937db..091c86b 100644
--- a/controllers/es.go
+++ b/controllers/es.go
@@ -1,7 +1,7 @@
package controllers
import (
- "encoding/json"
+ "basic.com/dbapi.git"
"fmt"
"github.com/gin-gonic/gin"
"math/rand"
@@ -23,141 +23,39 @@
// @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}"
+// @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)
- c.BindJSON(&searchBody)
- isEsSource := true
- databases := searchBody.DataBases
- page := searchBody.Page
- if page <= 0 {
- page = 1
+ err := c.BindJSON(&searchBody)
+ if err != nil || searchBody.PicUrl == "" || len(searchBody.DataBases) == 0 {
+ util.ResponseFormat(c, code.RequestParamError, "鍙傛暟鏈夎")
+ return
}
- size := searchBody.Size
- if size <= 0 {
- size = 8
- }
- from := (page - 1) * size
- to := page * size
- threshold := searchBody.Threshold
- if threshold < 0 || threshold > 100 {
- threshold = 60
- }
- picUrl := searchBody.PicUrl
- /*feature := searchBody.Feature*/
- var featByte []byte
- if len(picUrl) > 3 { // linux
- /*fileName := picUrl // picIp 瀹氫箟鍦� fileController weedfs 鏂囦欢鏈嶅姟鍣� 璁块棶 璺緞 鍓嶇紑
- detect := gorun.GetSimpleFaceDetect(fileName)
- if len(detect) != 1 {
- util.ResponseFormat(c,code.TooManyFeatureFindError,"鍥剧墖鐗瑰緛鍊艰繃澶�")
+ if searchBody.CompareNum !="" {
+ //浜屾鎼滅储锛屼笉闇�瑕佸啀姣斿浜�
+ co := service.GetCompResultByNum(searchBody.CompareNum)
+ if co != nil {
+ //浜屾鎼滅储鍜屾帓搴�
+ twiceM := GetCompareDataTwice(co,searchBody)
+ util.ResponseFormat(c,code.Success,twiceM)
return
- }else {
- featByte = detect[0]["feature"].([]byte)
- }*/ // linux
- }
- data := make(map[string]interface{})
- searchBody.Page = 1
- searchBody.Size = 15000
- //searchMap := Struct2Map(*searchBody)
- if len(databases) == 1 && databases[0] == "esData" {
- searchBody.IsAggs = false
- data = searchEsData(*searchBody)
- } else {
- isEsSource = false
- data = service.QueryDbPersonsForCompare(*searchBody)
- }
- sources := data["datalist"].([]interface{})
-
- if len(sources) > 0 {
- //杩涜姣斿
- sources = sourceCompare(sources, isEsSource, featByte, threshold)
- } else {
- fmt.Println("鏌ヨ鏉′欢涓嬫棤鏁版嵁 source 鏁版嵁涓虹┖锛�" + string(len(sources)))
- }
- dataLen := len(sources)
- if from > dataLen-1 {
- from = 0
- to = 0
- }
- if to > dataLen {
- to = dataLen
- }
- sources = sources[from:to] // 鏁版嵁 閮ㄥ垎鑾峰彇
- if !isEsSource { // 浜哄憳鏁版嵁瑕佸姞搴曞簱鍚嶇О
- for _, sou := range sources {
- tableId := sou.(map[string]interface{})["tableId"]
- if tableId != nil {
- info := QueryDbTableInfo(tableId.(string))
- if info["tableName"] != nil {
- sou.(map[string]interface{})["tableName"] = info["tableName"]
- } else {
- sou.(map[string]interface{})["tableName"] = "鍏朵粬"
- }
- if info["bwType"] != nil {
- sou.(map[string]interface{})["bwType"] = info["bwType"]
- } else {
- sou.(map[string]interface{})["bwType"] = "4"
- }
- }
+ } else {
+ m :=make(map[string]interface{},0)
+ m["compareNum"] = searchBody.CompareNum
+ m["total"] = 0
+ m["totalList"] = []CompareResult{}
+ util.ResponseFormat(c,code.CompareResultGone,m)
+ return
}
}
- dmap := make(map[string]interface{}, 2)
- dmap["datalist"] = sources
- dmap["total"] = dataLen
- util.ResponseFormat(c, code.Success, dmap)
+ m :=make(map[string]interface{},0)
+ m["compareNum"] = searchBody.CompareNum
+ m["total"] = 0
+ m["totalList"] = []CompareResult{}
+ util.ResponseFormat(c,code.CompareResultGone,m)
}
-
-// @Summary 鏌ヨ鎽勫儚鏈轰互鍙婂惎绠楁硶
-// @Description 鍏宠仈鏌ヨ鎽勫儚鏈轰互鍙婂惎绠楁硶
-// @Produce json
-// @Tags camera
-// @Success 200 {string} json "{"code":200, success:true, msg:"璇锋眰澶勭悊鎴愬姛", data:"鎽勫儚鏈轰俊鎭�"}"
-// @Failure 500 {string} json "{"code":500, success:false msg:"",data:"閿欒淇℃伅鍐呭"}"
-// @Router /data/api-v/camera/queryCameraAndTaskInfo [get]
-//func (ac *CameraController) QueryCameraAndTaskInfo(c *gin.Context) {
-// var cam models.Camera
-// rows := cam.FindAllCamTask()
-//
-// if len(rows) == 0 {
-// util.ResponseFormat(c, code.ComError, "娌℃湁璁板綍")
-// return
-// }
-// camList := make([]map[string]interface{}, 0, 5)
-// for _, cam := range rows {
-// newCam := make(map[string]interface{})
-// newCam["cameraid"] = cam.Id
-// newCam["rtspUrl"] = cam.Rtsp
-// tasks := cam.CamTask
-// taskList := make([]map[string]interface{}, 0)
-// if tasks != nil {
-// taskmap := make(map[string]interface{})
-// for _, task := range tasks {
-// taskId := taskmap[task.Taskid]
-// if taskId != nil {
-// cameraTasks := taskmap[task.Taskid].([]models.CameraTask)
-// cameraTasks = append(cameraTasks, task)
-// taskmap[task.Taskid] = cameraTasks
-// } else {
-// cameraTasks := make([]models.CameraTask, 0)
-// taskmap[task.Taskid] = append(cameraTasks, task)
-// }
-// }
-// for key, value := range taskmap {
-// i := make(map[string]interface{})
-// i["taskid"] = key
-// i["sdklist"] = value
-// taskList = append(taskList, i)
-// }
-// }
-// newCam["tasklist"] = taskList
-// camList = append(camList, newCam)
-// }
-// c.JSON(200, camList)
-//
-//}
func searchEsData(searchBody models.EsSearch) map[string]interface{} {
//璇锋眰绱㈠紩
@@ -166,7 +64,7 @@
queryBody := searchBody.InputValue
//妫�绱㈡
if queryBody != "" {
- queryStr = "\"must\":[{\"multi_match\":{\"query\":\"" + queryBody + "\",\"fields\":[\"tableName^1.5\",\"sex^2.0\",\"race^2.0\",\"content^1.0\",\"idCard^1.8\",\"cameraAddr^1.0\"]," +
+ 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]
@@ -191,7 +89,7 @@
esTableIdStr := ""
if tableId != nil && len(tableId) > 0 {
esTableId = strings.Replace(strings.Trim(fmt.Sprint(tableId), "[]"), " ", "\",\"", -1)
- esTableIdStr = "{\"terms\":{\"tableId\":[\"" + esTableId + "\"]}},"
+ esTableIdStr = "{\"terms\":{\"baseInfo.tableId\":[\"" + esTableId + "\"]}},"
}
isCollectionStr := ""
isCollection := searchBody.Collection
@@ -207,6 +105,11 @@
//璇锋眰澶�
url := "http://" + config.EsInfo.Masterip + ":" + config.EsInfo.Httpport +
"/" + index + "/_search?search_type=dfs_query_then_fetch"
+
+ var setApi dbapi.SysSetApi
+ _, sysconf := setApi.GetServerInfo()
+ analyServerFilterStr := "{\"term\":{\"analyServerId\":\"" + sysconf.ServerId + "\"}},"
+
//璇锋眰浣�
prama := "{\"from\":\"" + esFrom + "\",\"size\":\"" + esSize + "\"," +
// prama := "{\"size\":\"0\"," +
@@ -216,6 +119,7 @@
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\"]}"
@@ -244,17 +148,4 @@
}
}
return dataSource
-}
-
-// type 杞� map
-func Struct2Map(obj interface{}) map[string]interface{} {
- var data = make(map[string]interface{})
- bytes, _ := json.Marshal(obj)
- json.Unmarshal(bytes, &data)
- /*t := reflect.TypeOf(obj)
- v := reflect.ValueOf(obj)
- for i := 0; i < t.NumField(); i++ {
- data[t.Field(i).Name] = v.Field(i).Interface()
- }*/
- return data
}
--
Gitblit v1.8.0