Browse Source

support d and w (#3765)

Alex Meijer 2 weeks ago
parent
commit
1666901b31
2 changed files with 14 additions and 1 deletions
  1. 2 1
      pkg/costmodel/aggregation.go
  2. 12 0
      pkg/costmodel/aggregation_test.go

+ 2 - 1
pkg/costmodel/aggregation.go

@@ -11,6 +11,7 @@ import (
 	"github.com/opencost/opencost/core/pkg/filter/allocation"
 	"github.com/opencost/opencost/core/pkg/opencost"
 	"github.com/opencost/opencost/core/pkg/util/httputil"
+	"github.com/opencost/opencost/core/pkg/util/timeutil"
 	"github.com/opencost/opencost/pkg/env"
 )
 
@@ -159,7 +160,7 @@ func resolveStepFromQuery(qp httputil.QueryParams, window opencost.Window, accum
 		// quarter accumulation operates on daily inputs and calendar-rounded query windows
 		return resolveStepForAccumulate(24*time.Hour, accumulateBy), nil
 	default:
-		step, err := time.ParseDuration(stepRaw)
+		step, err := timeutil.ParseDuration(stepRaw)
 		if err != nil {
 			return 0, fmt.Errorf("invalid step %q: must be a Go duration or one of hour, day, week, month, quarter: %w", stepRaw, err)
 		}

+ 12 - 0
pkg/costmodel/aggregation_test.go

@@ -304,6 +304,18 @@ func TestResolveStepFromQuery(t *testing.T) {
 			accumulateBy: opencost.AccumulateOptionWeek,
 			expected:     7 * 24 * time.Hour,
 		},
+		{
+			name:         "day shorthand duration supported",
+			stepRaw:      "1d",
+			accumulateBy: opencost.AccumulateOptionNone,
+			expected:     24 * time.Hour,
+		},
+		{
+			name:         "week shorthand duration supported",
+			stepRaw:      "1w",
+			accumulateBy: opencost.AccumulateOptionNone,
+			expected:     7 * 24 * time.Hour,
+		},
 		{
 			name:         "invalid duration errors",
 			stepRaw:      "not-a-duration",