mirror of
https://github.com/yusing/godoxy.git
synced 2025-07-09 07:54:03 +02:00
metrics: metric utils
This commit is contained in:
parent
84e8dc0e06
commit
574056a7e3
1 changed files with 36 additions and 0 deletions
36
internal/metrics/utils/utils.go
Normal file
36
internal/metrics/utils/utils.go
Normal file
|
@ -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
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue