fix: [code-2912]: minor improvements, requested changes (#3197)

* minor improvements, requested changes
This commit is contained in:
Enver Biševac 2024-12-23 16:20:40 +00:00 committed by Harness
parent 9d4fc2c796
commit c94a181e7e
3 changed files with 55 additions and 11 deletions

View File

@ -20,6 +20,7 @@ import (
"github.com/harness/gitness/app/auth" "github.com/harness/gitness/app/auth"
"github.com/harness/gitness/app/paths" "github.com/harness/gitness/app/paths"
"github.com/harness/gitness/errors"
"github.com/harness/gitness/types" "github.com/harness/gitness/types"
"github.com/harness/gitness/types/enum" "github.com/harness/gitness/types/enum"
) )
@ -32,7 +33,13 @@ func (c *Controller) GetUsageMetrics(
startDate int64, startDate int64,
endDate int64, endDate int64,
) (*types.UsageMetric, error) { ) (*types.UsageMetric, error) {
rootSpaceRef, _, err := paths.DisectRoot(spaceRef) rootSpaceRef, sub, err := paths.DisectRoot(spaceRef)
if sub != "" {
return nil, errors.InvalidArgument(
"metric api can be used only within %q space: please remove %q part",
rootSpaceRef, sub,
)
}
if err != nil { if err != nil {
return nil, fmt.Errorf("could not find root space: %w", err) return nil, fmt.Errorf("could not find root space: %w", err)
} }

View File

@ -37,16 +37,19 @@ func HandleUsageMetric(spaceCtrl *space.Controller) http.HandlerFunc {
now := time.Now() now := time.Now()
start := now.Add(-30 * 24 * time.Hour).UnixMilli() start := now.Add(-30 * 24 * time.Hour).UnixMilli()
startDate, ok, _ := request.QueryParamAsPositiveInt64(r, "start_date") startDate := request.QueryParamAsUnixMillisOrDefault(
if !ok { r,
startDate = start request.QueryParamStartTime,
} start,
endDate, ok, _ := request.QueryParamAsPositiveInt64(r, "start_date") )
if !ok {
endDate = now.UnixMilli()
}
rule, err := spaceCtrl.GetUsageMetrics( endDate := request.QueryParamAsUnixMillisOrDefault(
r,
request.QueryParamEndTime,
now.UnixMilli(),
)
metric, err := spaceCtrl.GetUsageMetrics(
ctx, ctx,
session, session,
spaceRef, spaceRef,
@ -58,6 +61,6 @@ func HandleUsageMetric(spaceCtrl *space.Controller) http.HandlerFunc {
return return
} }
render.JSON(w, http.StatusOK, rule) render.JSON(w, http.StatusOK, metric)
} }
} }

34
app/api/request/time.go Normal file
View File

@ -0,0 +1,34 @@
// Copyright 2023 Harness, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package request
import "net/http"
const (
QueryParamStartTime = "start_time"
QueryParamEndTime = "end_time"
)
func QueryParamAsUnixMillisOrDefault(
r *http.Request,
param string,
defaultValue int64,
) int64 {
millis, ok, _ := QueryParamAsPositiveInt64(r, param)
if !ok {
return defaultValue
}
return millis
}