Skip to content

Commit

Permalink
监控数据查询增加对任意时间范围的支持
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaoxiangchun committed Jun 1, 2020
1 parent 3f63e47 commit ecd4a94
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 26 deletions.
1 change: 1 addition & 0 deletions pkg/apis/monitor/unifiedmonitor_const.go
Expand Up @@ -22,6 +22,7 @@ type MetricFunc struct {

type MetricInputQuery struct {
From string `json:"from"`
To string `json:"to"`
Interval string `json:"interval"`
MetricQuery []*AlertQuery `json:"metric_query"`
}
47 changes: 33 additions & 14 deletions pkg/monitor/models/unifiedmonitor.go
Expand Up @@ -149,7 +149,7 @@ func (self *SUnifiedMonitorManager) PerformQuery(ctx context.Context, userCred m
}
for _, q := range inputQuery.MetricQuery {
setDefaultValue(q, inputQuery)
err = self.ValidateInputQuery(*q)
err = self.ValidateInputQuery(q)
if err != nil {
return jsonutils.NewDict(), err
}
Expand Down Expand Up @@ -178,37 +178,56 @@ func doQuery(query monitor.MetricInputQuery) (*mq.Metrics, error) {
return metricQ.ExecuteQuery()
}

func (self *SUnifiedMonitorManager) ValidateInputQuery(query monitor.AlertQuery) error {
func (self *SUnifiedMonitorManager) ValidateInputQuery(query *monitor.AlertQuery) error {
if query.From == "" {
query.From = "30m"
}
if query.Model.Interval == "" {
query.Model.Interval = "5m"
}
if query.To == "" {
query.To = "now"
}
if _, err := time.ParseDuration(query.Model.Interval); err != nil {
return httperrors.NewInputParameterError("Invalid interval format: %s", query.Model.Interval)
}
return validators.ValidateAlertConditionQuery(query)
return validators.ValidateSelectOfMetricQuery(*query)
}

func setDefaultValue(query *monitor.AlertQuery, inputQuery *monitor.MetricInputQuery) {
setDataSourceId(query)
query.From = inputQuery.From
query.To = "now"
query.To = inputQuery.To
query.Model.Interval = inputQuery.Interval

if len(query.Model.GroupBy) == 0 {
query.Model.GroupBy = append(query.Model.GroupBy, monitor.MetricQueryPart{
Type: "field",
Params: []string{"*"},
query.Model.GroupBy = append(query.Model.GroupBy,
monitor.MetricQueryPart{
Type: "field",
Params: []string{"*"},
})
}

query.Model.GroupBy = append(query.Model.GroupBy,
monitor.MetricQueryPart{
Type: "time",
Params: []string{inputQuery.Interval},
},
monitor.MetricQueryPart{
Type: "fill",
Params: []string{"linear"},
})

for i, sel := range query.Model.Selects {
if len(sel) > 1 {
continue
}
sel = append(sel, monitor.MetricQueryPart{
Type: "mean",
Params: []string{},
})
query.Model.Selects[i] = sel
}
//query.Model.GroupBy = append(query.Model.GroupBy, monitor.MetricQueryPart{
// Type: "time",
// Params: []string{inputQuery.Interval},
//}, monitor.MetricQueryPart{
// Type: "fill",
// Params: []string{"none"},
//})
}

func setDataSourceId(query *monitor.AlertQuery) {
Expand Down
7 changes: 6 additions & 1 deletion pkg/monitor/tsdb/driver/influxdb/query.go
Expand Up @@ -87,7 +87,12 @@ func (query *Query) renderTags() []string {
}

func (query *Query) renderTimeFilter(queryCtx *tsdb.TsdbQuery) string {
from := "now() - " + queryCtx.TimeRange.From
from := ""
if strings.Contains(queryCtx.TimeRange.From, "now-") {
from = "now() - " + strings.Replace(queryCtx.TimeRange.From, "now-", "", 1)
} else {
from = "now() - " + queryCtx.TimeRange.From
}
to := ""

if queryCtx.TimeRange.To != "now" && queryCtx.TimeRange.To != "" {
Expand Down
46 changes: 35 additions & 11 deletions pkg/monitor/validators/validators.go
Expand Up @@ -99,24 +99,48 @@ func ValidateAlertQueryModel(input monitor.MetricQuery) error {
return nil
}

func ValidateSelectOfMetricQuery(input monitor.MetricQuery) error {
for _, sel := range input.Selects {
func ValidateSelectOfMetricQuery(input monitor.AlertQuery) error {
if err := ValidateFromAndToValue(input); err != nil {
return err
}

if err := ValidateAlertQueryModel(input.Model); err != nil {
return err
}

for _, sel := range input.Model.Selects {
if len(sel) == 0 {
return httperrors.NewInputParameterError("select for nothing in query")
}
}
if len(input.GroupBy) == 0 {
input.GroupBy = append(input.GroupBy, monitor.MetricQueryPart{
Type: "fill",
Params: []string{"none"},
}, monitor.MetricQueryPart{
Type: "time",
Params: []string{input.Interval},
})
}
return nil
}

func ValidateFromAndToValue(input monitor.AlertQuery) error {
fromRaw := strings.Replace(input.From, "now-", "", 1)

fromDur, err := time.ParseDuration("-" + fromRaw)
if err != nil {
return err
}

if input.To == "now" {
return nil
} else if strings.HasPrefix(input.To, "now-") {
withoutNow := strings.Replace(input.To, "now-", "", 1)

toDur, err := time.ParseDuration("-" + withoutNow)
if err == nil {
if toDur >= fromDur {
return nil
}
return httperrors.NewInputParameterError("query from:%s,to:%s err", input.From, input.To)
}
return err
}
return httperrors.NewInputParameterError("query to:%s format err", input.To)
}

func ValidateAlertConditionReducer(input monitor.Condition) error {
return nil
}
Expand Down

0 comments on commit ecd4a94

Please sign in to comment.