mirror of
https://github.com/cozystack/cozystack.git
synced 2026-11-21 12:13:55 +09:00
fix(observability-proxy): read a minus after an offset as an operator
The compound-offset refusal took any minus right after an offset's duration for a compound, so x offset 1h-1 or x offset 5m-y answered 422. metricsql continues a duration token only with a minus followed by a duration part (scanSingleDuration), so only that form is refused now; anything else is the subtraction it is. Assisted-by: LLM Signed-off-by: ZverGuy <maximbel2003@gmail.com>
This commit is contained in:
1 parent
84f19e14d8
commit
56fc64575a
2 files changed
+9
-1
No files matched your search
@@ -49,6 +49,10 @@ var (
|
||||
querySeconds = regexp.MustCompile(`^\d+(?:\.\d+)?$`)
|
||||
)
|
||||
|
||||
// negativeDurationPart is what metricsql's scanSingleDuration accepts
|
||||
// after a minus inside one duration token.
|
||||
var negativeDurationPart = regexp.MustCompile(`^(?:\d+(?:\.\d+)?(?:ms|s|m|h|d|w|y|i)|\$__interval)`)
|
||||
|
||||
var queryDurationUnits = map[string]time.Duration{
|
||||
"ms": time.Millisecond, "s": time.Second, "m": time.Minute, "h": time.Hour,
|
||||
"d": 24 * time.Hour, "w": 7 * 24 * time.Hour, "y": 365 * 24 * time.Hour,
|
||||
@@ -212,7 +216,8 @@ func scanOffsets(lower string) (back, ahead float64, ok bool) {
|
||||
// metricsql lexes 1h-30m as one duration whose parts after the
|
||||
// first negative one all subtract (DurationValue), so -1h-30m is
|
||||
// -90m and 1h-2h is -1h; the scan does not read such compounds.
|
||||
if end < len(arg) && arg[end] == '-' {
|
||||
// A minus not followed by a duration part is an operator.
|
||||
if end < len(arg) && arg[end] == '-' && negativeDurationPart.MatchString(arg[end+1:]) {
|
||||
return 0, 0, false
|
||||
}
|
||||
d, ok := parseQueryDuration(arg[:end])
|
||||
|
||||
@@ -450,6 +450,9 @@ func TestQueryLookahead(t *testing.T) {
|
||||
{`x offset 1h - 30m`, 0},
|
||||
{`x offset -1h + 30m`, time.Hour},
|
||||
{`x offset -1h30m`, 90 * time.Minute},
|
||||
// A minus not followed by a duration is an operator.
|
||||
{`x offset 1h-1`, 0},
|
||||
{`x offset 5m-y`, 0},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.query, func(t *testing.T) {
|
||||
|
||||
Reference in new issue
Block a user