From a2872649895a709316b5acef62f34bdb10864e42 Mon Sep 17 00:00:00 2001 From: Brian Brazil Date: Sun, 15 Nov 2015 09:26:38 +0000 Subject: [PATCH] Print offsets in promql. --- promql/printer.go | 15 ++++++++++++--- promql/printer_test.go | 9 +++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/promql/printer.go b/promql/printer.go index 44b05f149..507518eb3 100644 --- a/promql/printer.go +++ b/promql/printer.go @@ -17,6 +17,7 @@ import ( "fmt" "sort" "strings" + "time" "github.com/prometheus/common/model" @@ -174,7 +175,11 @@ func (node *MatrixSelector) String() string { Name: node.Name, LabelMatchers: node.LabelMatchers, } - return fmt.Sprintf("%s[%s]", vecSelector.String(), strutil.DurationToString(node.Range)) + offset := "" + if node.Offset != time.Duration(0) { + offset = fmt.Sprintf(" OFFSET %s", strutil.DurationToString(node.Offset)) + } + return fmt.Sprintf("%s[%s]%s", vecSelector.String(), strutil.DurationToString(node.Range), offset) } func (node *NumberLiteral) String() string { @@ -202,10 +207,14 @@ func (node *VectorSelector) String() string { } labelStrings = append(labelStrings, matcher.String()) } + offset := "" + if node.Offset != time.Duration(0) { + offset = fmt.Sprintf(" OFFSET %s", strutil.DurationToString(node.Offset)) + } if len(labelStrings) == 0 { - return node.Name + return fmt.Sprintf("%s%s", node.Name, offset) } sort.Strings(labelStrings) - return fmt.Sprintf("%s{%s}", node.Name, strings.Join(labelStrings, ",")) + return fmt.Sprintf("%s{%s}%s", node.Name, strings.Join(labelStrings, ","), offset) } diff --git a/promql/printer_test.go b/promql/printer_test.go index 8b3f4154e..473305b61 100644 --- a/promql/printer_test.go +++ b/promql/printer_test.go @@ -33,6 +33,15 @@ func TestExprString(t *testing.T) { { in: `up > BOOL 0`, }, + { + in: `a OFFSET 1m`, + }, + { + in: `a{c="d"}[5m] OFFSET 1m`, + }, + { + in: `a[5m] OFFSET 1m`, + }, } for _, test := range inputs {