qixiaoning
2025-07-25 94f3085afd10d76fa6e0640b5eed1d615b11ecea
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
package controllers
 
import (
    "basic.com/pubsub/esutil.git"
    "basic.com/valib/bhomeclient.git"
    "basic.com/valib/bhomedbapi.git"
    "basic.com/valib/logger.git"
    "encoding/json"
    "strconv"
    "vamicro/config"
)
 
// @Security ApiKeyAuth
// @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/tagList [POST]
func (sc *EsSearchController) PostEsTagList(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply {
    var dtApi bhomedbapi.DbTableApi
    dts, err := dtApi.FindAllDbTablesByCurServer("-1")
    if err == nil {
        if dts !=nil {
            sources := make([]map[string]interface{}, 0)
            for _, dt := range dts {
                tokenRes := make(map[string]interface{})
                tokenRes["key"] = dt.Id
                tokenRes["title"] = dt.TableName
                tokenRes["value"] = dt.Id
                tokenRes["status"] = dt.IsDelete
                tokenRes["bwType"] = dt.BwType //0:白名单,1:黑名单
                tokenRes["analyServerId"] = dt.AnalyServerId //为空是同步库,不为空是本地库
                if dt.IsDelete == 1 {
                    if GetTotalFromDb(dt.Id) == false {
                        continue
                    }
                }
                sources = append(sources, tokenRes)
            }
            return &bhomeclient.Reply{Success:true,Data:sources}
        }
    }
    return &bhomeclient.Reply{Success:false,Msg:"查询底库标签失败"}
}
 
//判断底库是否有数据
func GetTotalFromDb(id string) (flag bool) {
    flag = false
    var sApi bhomedbapi.SysSetApi
    flag, localConf := sApi.GetServerInfo()
    if !flag || localConf.AlarmIp == "" ||localConf.AlarmPort ==0 {
        logger.Debug("localConf err")
        return false
    }
    url := "http://" + localConf.AlarmIp + ":" + strconv.Itoa(int(localConf.AlarmPort)) +
        "/" + config.EsInfo.EsIndex.AiOcean.IndexName + "/_search"
    prama := "{\"query\":{\"bool\":{\"filter\":[{\"term\":{\"baseInfo.tableId\":\"" + id + "\"}}]}},\"size\":0}"
    buf, err := esutil.EsReq("POST", url, []byte(prama))
    if err != nil {
        logger.Debug("http request info is err!")
        return
    }
    var info interface{}
    json.Unmarshal(buf, &info)
    out, ok := info.(map[string]interface{})
    if !ok {
        logger.Debug("http response interface can not change map[string]interface{}")
        return
    }
    middle, ok := out["hits"].(map[string]interface{})
    if !ok {
        logger.Debug("first hits change error!")
        return
    }
 
    total := int(middle["total"].(float64))
    if total > 0 {
        flag = true
    }
    return flag
}