From 574056a7e39f51f82d4f16b78d5c5d62ab5872d1 Mon Sep 17 00:00:00 2001 From: yusing Date: Fri, 28 Mar 2025 07:47:58 +0800 Subject: [PATCH] metrics: metric utils --- internal/metrics/utils/utils.go | 36 +++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 internal/metrics/utils/utils.go diff --git a/internal/metrics/utils/utils.go b/internal/metrics/utils/utils.go new file mode 100644 index 00000000..2432b751 --- /dev/null +++ b/internal/metrics/utils/utils.go @@ -0,0 +1,36 @@ +package metricsutils + +import ( + "net/url" + "strconv" + "time" +) + +func CalculateBeginEnd(n, limit, offset int) (int, int, bool) { + if n == 0 || offset >= n { + return 0, 0, false + } + if limit == 0 { + limit = n + } + if offset+limit > n { + limit = n - offset + } + return offset, offset + limit, true +} + +func QueryInt(query url.Values, key string, defaultValue int) int { + value, _ := strconv.Atoi(query.Get(key)) + if value == 0 { + return defaultValue + } + return value +} + +func QueryDuration(query url.Values, key string, defaultValue time.Duration) time.Duration { + value, _ := time.ParseDuration(query.Get(key)) + if value == 0 { + return defaultValue + } + return value +}