liuxiaolong
2019-08-20 0fba63d0d13e94c71f690008e5b54b18e3cf2f5b
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
package controllers
 
import (
    "basic.com/dbapi.git"
    "encoding/json"
    "fmt"
 
    "github.com/gin-gonic/gin"
    "webserver/extend/code"
    "webserver/extend/config"
    "webserver/extend/esutil"
    "webserver/extend/util"
)
 
//标签列表
//任务列表
// @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/taskList [POST]
func (sc *EsSearchController) PostEsTagList(c *gin.Context) {
 
    url := "http://" + config.EsInfo.Masterip + ":" + config.EsInfo.Httpport +
        "/" + config.EsInfo.EsIndex.DbTables.IndexName + "/_search"
    var setApi dbapi.SysSetApi
    _, sysconf := setApi.GetServerInfo()
    prama := "{\"query\":{\"bool\":{\"filter\":[{\"terms\":{\"analyServerId\":[\"\",\"" + sysconf.ServerId + "\"]}}]}},\"size\":1000}"
    tokenRes := esutil.GetEsDataReq(url, prama, true)
    sources := make([]map[string]interface{}, 0)
    for _, value := range tokenRes["datalist"].([]interface{}) {
        tokenRes := make(map[string]interface{})
        tr := value.(map[string]interface{})
        key := tr["id"].(string)
        tokenRes["key"] = key
        tokenRes["title"] = tr["tableName"].(string)
        tokenRes["value"] = tr["id"].(string)
        status := int(tr["isDelete"].(float64))
        tokenRes["status"] = status
        if status == 1 {
            if GetTotalFromDb(key) == false {
                continue
            }
        }
        sources = append(sources, tokenRes)
    }
    util.ResponseFormat(c, code.Success, sources)
}
 
//判断底库是否有数据
func GetTotalFromDb(id string) (flag bool) {
    flag = false
    url := "http://" + config.EsInfo.Masterip + ":" + config.EsInfo.Httpport +
        "/" + config.EsInfo.EsIndex.VideoPersons.IndexName + "/_search"
    prama := "{\"query\":{\"bool\":{\"filter\":[{\"term\":{\"baseInfo.tableId\":\"" + id + "\"}}]}},\"size\":0}"
    buf, err := esutil.EsReq("POST", url, []byte(prama))
    if err != nil {
        fmt.Println("http request info is err!")
        return
    }
    var info interface{}
    json.Unmarshal(buf, &info)
    out, ok := info.(map[string]interface{})
    if !ok {
        fmt.Println("http response interface can not change map[string]interface{}")
        return
    }
    middle, ok := out["hits"].(map[string]interface{})
    if !ok {
        fmt.Println("first hits change error!")
        return
    }
 
    total := int(middle["total"].(float64))
    if total > 0 {
        flag = true
    }
    return flag
}