liuxiaolong
2019-07-25 aa1b946f4ba9b62dee9b9464ff4450ebbc48c4ec
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package controllers
 
import (
    "basic.com/dbapi.git"
    "encoding/json"
    "fmt"
    "github.com/gin-gonic/gin"
    "math/rand"
    "strconv"
    "strings"
    "webserver/extend/code"
    "webserver/extend/config"
    "webserver/extend/esutil"
    "webserver/extend/util"
    "webserver/models"
    "webserver/service"
)
 
type EsController struct{}
 
// @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)
    c.BindJSON(&searchBody)
    isEsSource := true
    databases := searchBody.DataBases
    page := searchBody.Page
    if page <= 0 {
        page = 1
    }
    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,"图片特征值过多")
            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"
                }
            }
        }
    }
    dmap := make(map[string]interface{}, 2)
    dmap["datalist"] = sources
    dmap["total"] = dataLen
    util.ResponseFormat(c, code.Success, dmap)
}
 
// @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{} {
    //请求索引
    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底层机制处理分页
    //请求头
    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\"," +
        "\"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
}
 
// 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
}