From 98d2e489ea9d976e7ab59f9dec8c072a7dd525de Mon Sep 17 00:00:00 2001 From: zhaoxiangchun <1422928955@qq.com> Date: Sat, 4 Jul 2020 19:23:40 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0influxdb=E7=9A=84tag=20?= =?UTF-8?q?=E7=9A=84=E4=B8=80=E4=BA=9Bfilter?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/monitor/models/datasource.go | 36 +++++++++++++++++++++++++++ pkg/monitor/models/suggestsysalert.go | 17 ++++++------- pkg/monitor/service/dispatcher.go | 4 +-- 3 files changed, 45 insertions(+), 12 deletions(-) diff --git a/pkg/monitor/models/datasource.go b/pkg/monitor/models/datasource.go index 71f3b3f779..6dca81bbc5 100644 --- a/pkg/monitor/models/datasource.go +++ b/pkg/monitor/models/datasource.go @@ -18,6 +18,7 @@ import ( "context" "database/sql" "fmt" + "regexp" "strings" "time" @@ -42,6 +43,7 @@ import ( var ( DataSourceManager *SDataSourceManager + compile = regexp.MustCompile(`\w{8}(-\w{4}){3}-\w{12}`) ) const ( @@ -333,6 +335,9 @@ func getAttributesOnMeasurement(database, tp string, output *monitor.InfluxMeasu tmpDict := jsonutils.NewDict() tmpArr := jsonutils.NewArray() for i := range res.Values { + if filterTagKey(res.Values[i][0].(*jsonutils.JSONString).Value()) { + continue + } tmpArr.Add(res.Values[i][0]) } tmpDict.Add(tmpArr, res.Columns[0]) @@ -355,14 +360,45 @@ func getTagValue(database string, output *monitor.InfluxMeasurement, db *influxd } res := dbRtn[0][0] tagValue := make(map[string][]string, 0) + keys := strings.Join(output.TagKey, ",") for i := range res.Values { val := res.Values[i][0].(*jsonutils.JSONString) + if !strings.Contains(keys, val.Value()) { + continue + } if _, ok := tagValue[val.Value()]; !ok { tagValue[val.Value()] = make([]string, 0) } tag := res.Values[i][1].(*jsonutils.JSONString) + if filterTagValue(tag.Value()) { + delete(tagValue, val.Value()) + continue + } tagValue[val.Value()] = append(tagValue[val.Value()], tag.Value()) } output.TagValue = tagValue + //TagKey == TagValue.keys + tagK := make([]string, 0) + for tag, _ := range output.TagValue { + tagK = append(tagK, tag) + } + output.TagKey = tagK return nil } + +func filterTagKey(key string) bool { + if strings.Contains(key, "_id") { + return true + } + if key == "perf_instance" { + return true + } + return false +} + +func filterTagValue(val string) bool { + if compile.MatchString(val) { + return true + } + return false +} diff --git a/pkg/monitor/models/suggestsysalert.go b/pkg/monitor/models/suggestsysalert.go index e571a478ea..d05ab63b89 100644 --- a/pkg/monitor/models/suggestsysalert.go +++ b/pkg/monitor/models/suggestsysalert.go @@ -27,7 +27,6 @@ import ( "yunion.io/x/onecloud/pkg/apis/monitor" "yunion.io/x/onecloud/pkg/cloudcommon/db" "yunion.io/x/onecloud/pkg/cloudcommon/db/lockman" - computemodels "yunion.io/x/onecloud/pkg/compute/models" "yunion.io/x/onecloud/pkg/httperrors" "yunion.io/x/onecloud/pkg/mcclient" "yunion.io/x/onecloud/pkg/mcclient/auth" @@ -130,12 +129,7 @@ func (manager *SSuggestSysAlertManager) ListItemFilter( q = q.In("provider", query.Brands) } if len(query.Cloudaccount) > 0 { - cpq := computemodels.CloudaccountManager.Query().SubQuery() - subcpq := cpq.Query(cpq.Field("id")).Filter(sqlchemy.OR( - sqlchemy.In(cpq.Field("id"), query.Cloudaccount), - sqlchemy.In(cpq.Field("name"), query.Cloudaccount), - )).SubQuery() - q.In("cloudaccount", subcpq) + q.In("cloudaccount", query.Cloudaccount) } if len(query.CloudEnv) > 0 { q = q.Equals("cloud_env", query.CloudEnv) @@ -526,13 +520,16 @@ func (self *SSuggestSysAlertManager) getMeterForcastCosts(ctx context.Context, u } session := auth.GetAdminSession(ctx, "", "") param := jsonutils.NewDict() - param.Add(jsonutils.NewString(domainId), "domain_id") - param.Add(jsonutils.NewString(projectId), "project_id") + if len(domainId) > 0 { + param.Add(jsonutils.NewString(domainId), "domain_id") + } + if len(projectId) > 0 { + param.Add(jsonutils.NewString(projectId), "project_id") + } meterRtn, err := modules.AmountEstimations.GetById(session, "month", param) if err != nil { return meterCost, err } - log.Errorln(meterRtn.String()) amount, _ := meterRtn.Float("amount") meterCost.Amount = amount return meterCost, nil diff --git a/pkg/monitor/service/dispatcher.go b/pkg/monitor/service/dispatcher.go index 270e1e56dd..c1bda03de5 100644 --- a/pkg/monitor/service/dispatcher.go +++ b/pkg/monitor/service/dispatcher.go @@ -36,11 +36,11 @@ func addCommonAlertDispatcher(prefix string, app *appsrv.Application) { } func performHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) { + _, query, body := fetchEnv(ctx, w, r) + appsrv.SendJSON(w, wrap(jsonutils.NewDict(), "subscription")) SubscriptionWorkerManager.Run(func() { - _, query, body := fetchEnv(ctx, w, r) ctx = context.WithValue(context.Background(), auth.AUTH_TOKEN, auth.AdminCredential()) subscriptionmodel.SubscriptionManager.PerformWrite(ctx, auth.AdminCredential(), query, body) - appsrv.SendJSON(w, wrap(jsonutils.NewDict(), "subscription")) }, nil, nil) }