sunty
2019-09-25 9969f8072b23fd8d03cb38445a81ae881bd01ee2
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
package esutil
 
import (
    "encoding/json"
    "errors"
    "fmt"
    "strconv"
    "strings"
 
    "basic.com/pubsub/protomsg.git"
        )
 
// 查询底库人员信息
func Personinfos( queryIndex int, queryNums int, indexName string, serverIp string, serverPort string, analyServerId string) ([]*protomsg.Esinfo, error){
            var dbinfos []*protomsg.Esinfo
            point   := strconv.Itoa(queryIndex)
            number   := strconv.Itoa(queryNums)
            JsonDSL  := ""
            if indexName == "videopersons" {
                 JsonDSL =  `  {
                    "from": ` + point +  `, 
                    "query": {
                        "bool": {
                            "filter": [
                                {
                                    "term": {
                                        "analyServerId": "` + analyServerId + `"
                                    }
                                }
                            ]
                        }
                    },
                    "size":`+ number +`,
                    "_source": [
                        "id",
                        "faceFeature"
                    ]
                }`
            }else {
                 JsonDSL =  `  {
                    "from": ` + point +  `,        
                    "query": {
                        "match_all": {}
                    },
                     "size":`+ number +`,
                     "_source": [
                       "id",
                       "tableId",
                       "faceFeature"
                       ]
                    }`
            }
            //fmt.Println("url: "+"http://"+serverIp+":"+serverPort+"/"+indexName+"/_search","body: ",JsonDSL)
    buf, err := EsReq("POST", "http://"+serverIp+":"+serverPort+"/"+indexName+"/_search",[]byte(JsonDSL))
    if err != nil {
        return dbinfos ,errors.New("http request dbtablename info is err!")
    }
 
      // 返回 _source 数组
      sources, err := Sourcelist(buf)
      if err != nil {
            return dbinfos,err 
      }
 
      // 返回所有查询的数据
     dbpersoninfos :=  Parsesources(sources)
      return dbpersoninfos, nil
}
 
// 根据底库id查询底库信息
func Dbtablefosbyid (id []string, indexName string, serverIp string, serverPort string) ([]protomsg.Dbtable, error) {
    var dbinfo []protomsg.Dbtable
    dbtableId := strings.Replace(strings.Trim(fmt.Sprint(id), "[]"), " ", "\",\"", -1)
        var dbinfoRequest = `
            {
                "query": {
                    "bool": {
                        "filter": [{
                            "terms": {
                                "id": [
                                    "`+ dbtableId +`"
                                ]
                            }
                        }]
                    }
                },
                "size":1000000
            }
        `
    buf, err := EsReq("POST", "http://"+serverIp+":"+serverPort+"/"+indexName+"/_search",[]byte(dbinfoRequest))
    if err != nil {
        return dbinfo,  err
    }
 
    sources, err := Sourcelist(buf)
    if err != nil {
        return dbinfo , err
    }
 
    dbtable := Dbtablebyid(sources)
    return dbtable, nil
}
 
// 根据抓拍人员id查询抓拍人员信息
func Videopersonsinfosbyid (id []string, indexName string, serverIp string, serverPort string) ([]protomsg.Videopersons, error) {
    var videopersonsInfo []protomsg.Videopersons
    videopersonsPersonId := strings.Replace(strings.Trim(fmt.Sprint(id), "[]"), " ", "\",\"", -1)
    var dbinfoRequest = `
            {
                "query": {
                    "bool": {
                        "filter": [{
                            "terms": {
                                "id": [
                                    "`+ videopersonsPersonId +`"
                                ]
                            }
                        }]
                    }
                },
                "size":1000000
            }
        `
    buf, err := EsReq("POST", "http://"+serverIp+":"+serverPort+"/"+indexName+"/_search",[]byte(dbinfoRequest))
    if err != nil {
        return videopersonsInfo,  err
    }
 
    sources, err := Sourcelist(buf)
    if err != nil {
        return videopersonsInfo , err
    }
 
    videoperson := Videopersonsbyid(sources)
    println(videoperson)
    return videoperson,nil
}
 
// 根据底库人员id查询底库人员信息
func Dbpersoninfosbyid (id []string, indexName string, serverIp string, serverPort string) ([]protomsg.Dbperson, error) {
    var dbinfo []protomsg.Dbperson
    dbtablePersonId := strings.Replace(strings.Trim(fmt.Sprint(id), "[]"), " ", "\",\"", -1)
    var dbinfoRequest = `
            {
                "query": {
                    "bool": {
                        "filter": [{
                            "terms": {
                                "id": [
                                    "`+ dbtablePersonId +`"
                                ]
                            }
                        }]
                    }
                },
                "size":1000000
            }
        `
    buf, err := EsReq("POST", "http://"+serverIp+":"+serverPort+"/"+indexName+"/_search",[]byte(dbinfoRequest))
    if err != nil {
        return dbinfo,  err
    }
 
    sources, err := Sourcelist(buf)
    if err != nil {
        return dbinfo , err
    }
 
    dbperson := Dbpersonbyid(sources)
    println(dbperson)
    return dbperson,nil
}
//根据抓拍库人员id查询特征值
func GetVideoPersonFaceFeatureById (id string, indexName string, serverIp string, serverPort string) (string, error) {
    var jsonDSL = `
            {
                "query": {
                    "bool": {
                        "filter": [{
                            "term": {
                                "id":"`+ id +`"
                            }
                        }]
                    }
                },
                "_source":["faceFeature"]
            }
        `
    buf, err := EsReq("POST", "http://"+serverIp+":"+serverPort+"/"+indexName+"/_search",[]byte(jsonDSL))
    if err != nil {
        return "",  err
    }
 
    sources, err := Sourcelist(buf)
    if err != nil {
        return "" , err
    }
    faceFeature := sources[0]["faceFeature"].(string)
    return faceFeature,nil
}
 
// 根据tableid 查询tablename
func Dbtablename(tableid string, indexName string, serverIp string, serverPort string) (tablename string, err error) {
    var dbinfotable =` {
        "query": {
            "bool": {
                "filter": [
                {
                    "term": {
                        "id":"`+tableid+`"
                    }
                }
                ]
            }
        },
        "_source": [
            "tableName"
        ],
        "size":1000000
    }
    `
    buf, err := EsReq("POST", "http://"+serverIp+":"+serverPort+"/"+indexName+"/_search",[]byte(dbinfotable))
    if err != nil {
        return "" ,errors.New("http request dbtablename info is err!")
    }
    sources, err := Sourcelist(buf)
    if err != nil {
          return "",err 
    }
    for _, source := range sources {
        if name, ok := source["tableName"].(string); ok {
            tablename = name
            break
        }
    }
    return tablename, nil 
}
 
 
//根据抓拍人员id更新(videourl)摄像机地址
func UpdateVideourlById(id string, videoUrl string, indexName string, serverIp string, serverPort string)(statu int){
    var info interface{}
    var videoUrlInfo = `
        {
          "script": {
            "source": "ctx._source.videoUrl='` + videoUrl + `'"    
          },
          "query": {
            "term": {
              "id": "` +id+ `"
            }
          },
            "size":1000000
        }
        `
    buf, err := EsReq("POST", "http://"+serverIp+":"+serverPort+"/"+indexName+"/_update_by_query",[]byte(videoUrlInfo))
    if err != nil {
        fmt.Println("http request videoUrlInfo info is err!")
        statu = 500
        return
    }
    json.Unmarshal(buf, &info)
    out, ok := info.(map[string]interface{})
    if !ok {
        fmt.Println("http response interface can not change map[string]interface{}")
        statu = 500
        return
    }
 
    middle, ok := out["updated"].(float64)
    if !ok {
        fmt.Println("first updated change error!")
        statu = 500
        return
    }
    if middle == 1{
        statu = 200
        return
    }
    if middle == 0{
        statu = 201
        return
    }
    return statu
}
 
//获取当前节点抓拍库所有人员ID
func GetAllLocalVideopersonsId(compareArgs protomsg.CompareArgs, indexName string, serverIp string, serverPort string) (capturetable []string) {
    ts := time.Now()
    queryStr := ""
    queryBody := compareArgs.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 := compareArgs.SearchTime[0]
    lteDate := compareArgs.SearchTime[1]
    //判断任务ID
    taskIdStr := ""
    taskId := compareArgs.Tasks
    if taskId != nil && len(taskId) > 0 {
        esTaskId := strings.Replace(strings.Trim(fmt.Sprint(taskId), "[]"), " ", "\",\"", -1)
        taskIdStr = "{\"terms\":{\"taskId\":[\"" + esTaskId + "\"]}},"
    }
    //判断摄像机ID
    cameraIdStr := ""
    cameraId := compareArgs.TreeNodes
    if cameraId != nil && len(cameraId) > 0 {
        esCameraId := strings.Replace(strings.Trim(fmt.Sprint(cameraId), "[]"), " ", "\",\"", -1)
        cameraIdStr = "{\"terms\":{\"cameraId\":[\"" + esCameraId + "\"]}},"
    }
 
    //判断库表ID
    tableId := compareArgs.Tabs
    esTableId := ""
    esTableIdStr := ""
    if tableId != nil && len(tableId) > 0 {
        esTableId = strings.Replace(strings.Trim(fmt.Sprint(tableId), "[]"), " ", "\",\"", -1)
        esTableIdStr = "{\"terms\":{\"baseInfo.tableId\":[\"" + esTableId + "\"]}},"
    }
    isCollectStr := ""
    isCollect := compareArgs.Collection
    if isCollect != "" {
        isCollectStr = "{\"term\":{\"isCollect\":\"" + isCollect + "\"}},"
    }
 
    //使用es底层机制处理分页
 
    analyServerId := compareArgs.AnalyServerId
    if analyServerId == "" {
        fmt.Println("no analyServerId")
        return
    }
    analyServerFilterStr := "{\"term\":{\"analyServerId\":\"" + analyServerId + "\"}},"
 
    //首次请求头
    url := "http://" + serverIp + ":" + serverPort +
        "/" + indexName + "/_search?search_type=dfs_query_then_fetch;scroll=1m"
 
    var lock sync.RWMutex
    var wg sync.WaitGroup
 
    for i := 0; i < 32; i++ {
        //请求体
        prama := "{" +
            "\"slice\":{\"id\":" + strconv.Itoa(i) + ",\"max\":48}," +
            "\"size\":\"1000\"," +
            "\"query\":{\"bool\":{" + queryStr +
            "\"filter\":[" +
            cameraIdStr +
            taskIdStr +
            isCollectStr +
            esTableIdStr +
            analyServerFilterStr +
            "{\"range\":{\"picDate\":{\"from\":\"" + gteDate + "\",\"to\":\"" + lteDate + "\",\"include_lower\":true,\"include_upper\":true,\"boost\":1}}}]}}," +
            "\"_source\":[\"id\"]" +
            "}"
        wg.Add(1)
        go func() {
            defer wg.Done()
 
            //fmt.Println(url)
            //fmt.Println(prama)
            buf, err := EsReq("POST", url, []byte(prama))
 
            if err != nil {
                fmt.Println("http request videoUrlInfo info is err!")
                fmt.Println(len(capturetable))
                return
            }
 
            sources, err := Sourcelistforscroll(buf)
 
            if err != nil {
                fmt.Println(len(capturetable))
                return
            }
            for _, source := range sources["sourcelist"].([]map[string]interface{}) {
                capturetable = append(capturetable, source["id"].(string))
            }
 
            scroll_id := sources["scroll_id"].(string)
 
            //scroll请求头
            scroll_url := "http://" + serverIp + ":" + serverPort + "/_search/scroll"
 
            for {
                var tmpList []string
                next_scroll_id := ""
                if next_scroll_id != "" {
                    scroll_id = next_scroll_id
                }
                jsonDSL := `{
            "scroll": "1m", 
            "scroll_id" : "` + scroll_id + `"
        }`
                //fmt.Println(scroll_url)
                //fmt.Println(jsonDSL)
                buf, err := EsReq("POST", scroll_url, []byte(jsonDSL))
 
                if err != nil {
                    fmt.Println("lenth1: ", len(capturetable))
                    return
                }
                nextSources, err := Sourcelistforscroll(buf)
 
                if nextSources == nil {
                    return
                }
 
                nextM := nextSources["sourcelist"].([]map[string]interface{})
                //fmt.Println("id",nextSources)
                if nextM == nil || len(nextM) == 0 {
                    //fmt.Println("lenth: ", len(capturetable))
                    return
                }
                //fmt.Println("id")
                for _, source := range nextM {
                    tmpList = append(tmpList, source["id"].(string))
                }
                //fmt.Println("tmpList: ", len(tmpList))
                lock.Lock()
                capturetable = append(capturetable, tmpList...)
                lock.Unlock()
 
                next_scroll_id = nextSources["scroll_id"].(string)
            }
 
 
            fmt.Println(len(capturetable))
 
        }()
    }
    wg.Wait()
 
    //fmt.Println("lenth_all: ", len(capturetable))
    //fmt.Println("耗时:", time.Since(ts))
    return capturetable
}
 
//获取底库人员ID
func GetDbpersonsId(compareArgs  protomsg.CompareArgs,indexName string, serverIp string, serverPort string) (source map[string][]string) {
    queryStr := ""
    queryBody := compareArgs.InputValue
    //检索框
    if queryBody != "" {
        queryStr = "\"must\":[{\"multi_match\":{\"query\":\"" + queryBody + "\",\"fields\":[\"personName^1.5\",\"age^1.5\",\"idCard^1.5\",\"phoneNum^1.5\",\"sex^2.0\",\"reserved^2.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}}],"
    }
    //判断库表ID
    tableId := compareArgs.Tabs
    esTableId := ""
    esTableIdStr := ""
    if tableId != nil && len(tableId) > 0 {
        esTableId = strings.Replace(strings.Trim(fmt.Sprint(tableId), "[]"), " ", "\",\"", -1)
        esTableIdStr = "{\"terms\":{\"tableId\":[\"" + esTableId + "\"]}}"
    }
 
    prama := "{" +
        "\"size\":\"100000000\"," +
        "\"query\":{\"bool\":{" + queryStr +
        "\"filter\":[" +
        esTableIdStr +
        "]}}," +
        "\"_source\":[\"id\",\"tableId\"]" +
        "}"
 
    url := "http://" + serverIp + ":" + serverPort +
        "/" + indexName + "/_search?search_type=dfs_query_then_fetch"
    fmt.Println(url)
    fmt.Println(prama)
    buf, err := EsReq("POST", url,[]byte(prama))
    if err != nil {
        fmt.Println("http request videoUrlInfo info is err!")
        return
    }
    sources, err := Sourcelist(buf)
    if err != nil {
        return
    }
    tabsource := make(map[string][]string)
    for _, source := range  sources{
        tableId := source["tableId"].(string)
        id := source["id"].(string)
        tabsource[tableId] = append(tabsource[tableId], id)
    }
    return tabsource
}