forked from segmentio/topicctl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
durations.go
49 lines (43 loc) · 1.29 KB
/
durations.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package util
import (
"fmt"
"time"
)
// PrettyDuration returns a human-formatted duration string given an golang
// duration value.
func PrettyDuration(duration time.Duration) string {
seconds := duration.Seconds()
if seconds < 1.0 {
return fmt.Sprintf("%dms", duration.Milliseconds())
} else if seconds < 240.0 {
return fmt.Sprintf("%ds", int(seconds))
} else if seconds < (2.0 * 60.0 * 60.0) {
return fmt.Sprintf("%dm", int(duration.Minutes()))
} else {
return fmt.Sprintf("%dh", int(duration.Hours()))
}
}
// PrettyRate returns a human-formatted rate from a count and a duration.
func PrettyRate(count int64, duration time.Duration) string {
if duration == 0 {
return ""
} else if count == 0 {
return "0"
}
ratePerSec := float64(count) / duration.Seconds()
ratePerMin := float64(count) / duration.Minutes()
ratePerHour := float64(count) / duration.Hours()
if ratePerSec >= 10.0 {
return fmt.Sprintf("%d/sec", int(ratePerSec))
} else if ratePerSec >= 1.0 {
return fmt.Sprintf("%0.1f/sec", ratePerSec)
} else if ratePerMin >= 10.0 {
return fmt.Sprintf("%d/min", int(ratePerMin))
} else if ratePerMin >= 1.0 {
return fmt.Sprintf("%0.1f/min", ratePerMin)
} else if ratePerHour >= 0.1 {
return fmt.Sprintf("%0.1f/hour", ratePerHour)
} else {
return fmt.Sprintf("~0")
}
}