From c94a181e7e325a63e24c62133023cd0d3592e126 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enver=20Bi=C5=A1evac?= Date: Mon, 23 Dec 2024 16:20:40 +0000 Subject: [PATCH] fix: [code-2912]: minor improvements, requested changes (#3197) * minor improvements, requested changes --- app/api/controller/space/usage.go | 9 +++++++- app/api/handler/space/usage.go | 23 ++++++++++++--------- app/api/request/time.go | 34 +++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 11 deletions(-) create mode 100644 app/api/request/time.go diff --git a/app/api/controller/space/usage.go b/app/api/controller/space/usage.go index d559088c6..18ca35911 100644 --- a/app/api/controller/space/usage.go +++ b/app/api/controller/space/usage.go @@ -20,6 +20,7 @@ import ( "github.com/harness/gitness/app/auth" "github.com/harness/gitness/app/paths" + "github.com/harness/gitness/errors" "github.com/harness/gitness/types" "github.com/harness/gitness/types/enum" ) @@ -32,7 +33,13 @@ func (c *Controller) GetUsageMetrics( startDate int64, endDate int64, ) (*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 { return nil, fmt.Errorf("could not find root space: %w", err) } diff --git a/app/api/handler/space/usage.go b/app/api/handler/space/usage.go index d912bdbc8..b6af5ef05 100644 --- a/app/api/handler/space/usage.go +++ b/app/api/handler/space/usage.go @@ -37,16 +37,19 @@ func HandleUsageMetric(spaceCtrl *space.Controller) http.HandlerFunc { now := time.Now() start := now.Add(-30 * 24 * time.Hour).UnixMilli() - startDate, ok, _ := request.QueryParamAsPositiveInt64(r, "start_date") - if !ok { - startDate = start - } - endDate, ok, _ := request.QueryParamAsPositiveInt64(r, "start_date") - if !ok { - endDate = now.UnixMilli() - } + startDate := request.QueryParamAsUnixMillisOrDefault( + r, + request.QueryParamStartTime, + start, + ) - rule, err := spaceCtrl.GetUsageMetrics( + endDate := request.QueryParamAsUnixMillisOrDefault( + r, + request.QueryParamEndTime, + now.UnixMilli(), + ) + + metric, err := spaceCtrl.GetUsageMetrics( ctx, session, spaceRef, @@ -58,6 +61,6 @@ func HandleUsageMetric(spaceCtrl *space.Controller) http.HandlerFunc { return } - render.JSON(w, http.StatusOK, rule) + render.JSON(w, http.StatusOK, metric) } } diff --git a/app/api/request/time.go b/app/api/request/time.go new file mode 100644 index 000000000..154dfbdfe --- /dev/null +++ b/app/api/request/time.go @@ -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 +}