Merge pull request #7046 from zhaoxiangchun/feature/zxc-influxdb-tag-filter

增加influxdb的tag 的一些filter
This commit is contained in:
yunion-ci-robot
2020-07-06 11:50:48 +08:00
committed by GitHub
3 changed files with 45 additions and 12 deletions
+36
View File
@@ -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
}
+7 -10
View File
@@ -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
+2 -2
View File
@@ -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)
}