From 7af5c22a1beb8d707ab2b254529007653457ee1c Mon Sep 17 00:00:00 2001 From: liugq Date: Wed, 21 Sep 2022 11:08:21 +0800 Subject: [PATCH] filter dashbard by cluster id --- plugin/api/insight/dashboard.go | 44 ++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/plugin/api/insight/dashboard.go b/plugin/api/insight/dashboard.go index fe83e963..b57f1e67 100644 --- a/plugin/api/insight/dashboard.go +++ b/plugin/api/insight/dashboard.go @@ -5,7 +5,6 @@ package insight import ( - "fmt" httprouter "infini.sh/framework/core/api/router" "infini.sh/framework/core/insight" "infini.sh/framework/core/orm" @@ -13,7 +12,6 @@ import ( "net/http" log "src/github.com/cihub/seelog" "strconv" - "strings" ) func (h *InsightAPI) createDashboard(w http.ResponseWriter, req *http.Request, ps httprouter.Params) { @@ -146,17 +144,12 @@ func (h *InsightAPI) deleteDashboard(w http.ResponseWriter, req *http.Request, p } func (h *InsightAPI) searchDashboard(w http.ResponseWriter, req *http.Request, ps httprouter.Params) { - var ( keyword = h.GetParameterOrDefault(req, "keyword", "") - queryDSL = `{"query":{"bool":{"must":[%s]}}, "size": %d, "from": %d}` strSize = h.GetParameterOrDefault(req, "size", "20") strFrom = h.GetParameterOrDefault(req, "from", "0") - mustBuilder = &strings.Builder{} + clusterID = h.GetParameter(req, "cluster_id") ) - if keyword != "" { - mustBuilder.WriteString(fmt.Sprintf(`{"query_string":{"default_field":"*","query": "%s"}}`, keyword)) - } size, _ := strconv.Atoi(strSize) if size <= 0 { size = 20 @@ -166,9 +159,38 @@ func (h *InsightAPI) searchDashboard(w http.ResponseWriter, req *http.Request, p from = 0 } - q := orm.Query{} - queryDSL = fmt.Sprintf(queryDSL, mustBuilder.String(), size, from) - q.RawQuery = []byte(queryDSL) + must := []util.MapStr{} + if keyword != "" { + must = append(must, util.MapStr{ + "query_string": util.MapStr{ + "default_field":"*", + "query": keyword, + }, + }) + } + + if clusterID != "" { + must = append(must, util.MapStr{ + "term": util.MapStr{ + "cluster_id": util.MapStr{ + "value": clusterID, + }, + }, + }) + } + + queryDSL := util.MapStr{ + "size": size, + "from": from, + "query": util.MapStr{ + "bool": util.MapStr{ + "must": must, + }, + }, + } + q := orm.Query{ + RawQuery: util.MustToJSONBytes(queryDSL), + } err, res := orm.Search(&insight.Dashboard{}, &q) if err != nil {