2019-12-08 22:52:31 +00:00
|
|
|
package promql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/storage"
|
2020-04-28 12:28:22 +00:00
|
|
|
"github.com/VictoriaMetrics/metricsql"
|
2019-12-08 22:52:31 +00:00
|
|
|
)
|
|
|
|
|
2019-12-25 19:35:47 +00:00
|
|
|
// IsRollup verifies whether s is a rollup with non-empty window.
|
|
|
|
//
|
|
|
|
// It returns the wrapped query with the corresponding window, step and offset.
|
2021-07-12 14:16:38 +00:00
|
|
|
func IsRollup(s string) (childQuery string, window, step, offset *metricsql.DurationExpr) {
|
2019-12-25 19:35:47 +00:00
|
|
|
expr, err := parsePromQLWithCache(s)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
re, ok := expr.(*metricsql.RollupExpr)
|
2021-07-12 14:16:38 +00:00
|
|
|
if !ok || re.Window == nil {
|
2019-12-25 19:35:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
wrappedQuery := re.Expr.AppendString(nil)
|
|
|
|
return string(wrappedQuery), re.Window, re.Step, re.Offset
|
|
|
|
}
|
|
|
|
|
2019-12-08 22:52:31 +00:00
|
|
|
// IsMetricSelectorWithRollup verifies whether s contains PromQL metric selector
|
|
|
|
// wrapped into rollup.
|
|
|
|
//
|
|
|
|
// It returns the wrapped query with the corresponding window with offset.
|
2021-07-12 14:16:38 +00:00
|
|
|
func IsMetricSelectorWithRollup(s string) (childQuery string, window, offset *metricsql.DurationExpr) {
|
2019-12-08 22:52:31 +00:00
|
|
|
expr, err := parsePromQLWithCache(s)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2019-12-25 19:35:47 +00:00
|
|
|
re, ok := expr.(*metricsql.RollupExpr)
|
2021-07-12 14:16:38 +00:00
|
|
|
if !ok || re.Window == nil || re.Step != nil {
|
2019-12-08 22:52:31 +00:00
|
|
|
return
|
|
|
|
}
|
2019-12-25 19:35:47 +00:00
|
|
|
me, ok := re.Expr.(*metricsql.MetricExpr)
|
|
|
|
if !ok || len(me.LabelFilters) == 0 {
|
2019-12-08 22:52:31 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
wrappedQuery := me.AppendString(nil)
|
|
|
|
return string(wrappedQuery), re.Window, re.Offset
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseMetricSelector parses s containing PromQL metric selector
|
2019-12-25 19:35:47 +00:00
|
|
|
// and returns the corresponding LabelFilters.
|
2019-12-08 22:52:31 +00:00
|
|
|
func ParseMetricSelector(s string) ([]storage.TagFilter, error) {
|
|
|
|
expr, err := parsePromQLWithCache(s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-12-25 19:35:47 +00:00
|
|
|
me, ok := expr.(*metricsql.MetricExpr)
|
2019-12-08 22:52:31 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("expecting metricSelector; got %q", expr.AppendString(nil))
|
|
|
|
}
|
2019-12-25 19:35:47 +00:00
|
|
|
if len(me.LabelFilters) == 0 {
|
|
|
|
return nil, fmt.Errorf("labelFilters cannot be empty")
|
2019-12-08 22:52:31 +00:00
|
|
|
}
|
2019-12-25 19:35:47 +00:00
|
|
|
tfs := toTagFilters(me.LabelFilters)
|
|
|
|
return tfs, nil
|
2019-12-08 22:52:31 +00:00
|
|
|
}
|