-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathformatters.go
234 lines (216 loc) · 4.91 KB
/
formatters.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package main
import (
"fmt"
"math"
"time"
)
func getFormatter(s string) func(i interface{}) string {
switch s {
case "start", "end", "timestamp":
return fmtTimestamp
case "size", "buffer_size":
return fmtSizeBytes
case "received_points", "selected_points", "list_limit", "select_points_limit":
return fmtLargeNum
case "mem_usage":
return fmtSizeMb
case "uptime":
return fmtDuration
case "idle_time":
return fmtDuration
case "drop_threshold":
return fmtPercentage
case "idle_percentage":
return fmtPercentage
case "time_precision":
return fmtTimePrecision
case "time":
return fmtTimeit
case "expiration_num", "expiration_log":
return fmtExpiration
case "shard_duration":
return fmtShardDuration
default:
return func(i interface{}) string { return fmt.Sprint(i) }
}
}
func fmtTimeit(i interface{}) string {
f, ok := i.(float64)
if ok {
return fmt.Sprintf("%.6f seconds", f)
}
return fmt.Sprint(i)
}
func fmtTimePrecision(i interface{}) string {
tp, ok := i.(string)
if ok {
switch tp {
case "s":
return fmt.Sprintf("%s (%s)", tp, "second")
case "ms":
return fmt.Sprintf("%s (%s)", tp, "millisecond")
case "us":
return fmt.Sprintf("%s (%s)", tp, "microsecond")
case "ns":
return fmt.Sprintf("%s (%s)", tp, "namesecond")
}
}
return fmt.Sprint(i)
}
func fmtPercentage(i interface{}) string {
p, ok := i.(float64)
if ok {
return fmt.Sprintf("%v (%d%%)", i, int(p*100))
}
o, ok := i.(int)
if ok {
return fmt.Sprintf("%d%%", o)
}
return fmt.Sprint(i)
}
func fmtSizeBytes(i interface{}) string {
size, ok := i.(int)
if ok {
lookup := "BKMGTPEZYXWVU"
if size > 0 {
i := int(min(int(math.Log(float64(size)))/int(math.Log(1024)), 12))
size = int(size * 100 / int(math.Pow(1024, float64(i))) / 100)
if i > 0 {
return fmt.Sprintf("%d %cB", size, lookup[i])
}
}
return fmt.Sprintf("%d bytes", size)
}
return fmt.Sprint(i)
}
func fmtSizeMb(i interface{}) string {
size, ok := i.(int)
if ok {
size *= int(math.Pow(1024, 2))
return fmtSizeBytes(size)
}
return fmt.Sprint(i)
}
func fmtTimestamp(i interface{}) string {
ts, ok := i.(int)
if ok && timePrecision != nil {
switch *timePrecision {
case "s":
return time.Unix(int64(ts), 0).Format(time.UnixDate)
case "ms":
s := int64(ts / 1000)
ns := int64(ts % 1000 * 1000000)
return time.Unix(s, ns).Format(time.UnixDate)
case "us":
s := int64(ts / 1000000)
ns := int64(ts % 1000000 * 1000)
return time.Unix(s, ns).Format(time.UnixDate)
case "ns":
s := int64(ts / 1000000000)
ns := int64(ts % 1000000000)
return time.Unix(s, ns).Format(time.UnixDate)
}
}
return fmt.Sprint(i)
}
func fmtTimestampUTC(i interface{}) string {
ts, ok := i.(int)
if ok && timePrecision != nil {
switch *timePrecision {
case "s":
return time.Unix(int64(ts), 0).UTC().Format(time.RFC1123)
case "ms":
s := int64(ts / 1000)
ns := int64(ts % 1000 * 1000000)
return time.Unix(s, ns).UTC().Format(time.RFC1123)
case "us":
s := int64(ts / 1000000)
ns := int64(ts % 1000000 * 1000)
return time.Unix(s, ns).UTC().Format(time.RFC1123)
case "ns":
s := int64(ts / 1000000000)
ns := int64(ts % 1000000000)
return time.Unix(s, ns).UTC().Format(time.RFC1123)
}
}
return fmt.Sprint(i)
}
func fmtLargeNum(i interface{}) string {
s := []rune(fmt.Sprint(i))
l := len(s)
if _, ok := i.(int); ok && l > 6 {
var res []rune
for n, c := range s {
res = append(res, c)
if l-n > 1 && (l-n)%3 == 1 {
res = append(res, '.')
}
}
s = res
}
return string(s)
}
func fmtExpiration(i interface{}) string {
if i == nil {
return "never"
}
seconds, ok := i.(int)
if ok {
if seconds == 1 {
return "1 second"
}
if seconds <= 60*2 {
return fmt.Sprintf("%d seconds", seconds)
}
if seconds <= 3600*2 {
return fmt.Sprintf("%d minutes", seconds/60)
}
if seconds <= 86400*2 {
return fmt.Sprintf("%d hours", seconds/3600)
}
if seconds <= 86400*28 {
return fmt.Sprintf("%d days", seconds/86400)
}
return fmt.Sprintf("%d weeks", seconds/(86400*7))
}
return fmt.Sprint(i)
}
func fmtDuration(i interface{}) string {
seconds, ok := i.(int)
if ok {
if seconds == 1 {
return "1 second"
}
if seconds <= 60*2 {
return fmt.Sprintf("%d seconds", seconds)
}
if seconds <= 3600*2 {
return fmt.Sprintf("%d minutes", seconds/60)
}
if seconds <= 86400*2 {
return fmt.Sprintf("%d hours", seconds/3600)
}
if seconds <= 2592000*2 {
return fmt.Sprintf("%d days", seconds/86400)
}
if seconds <= 31557600*2 {
return fmt.Sprintf("%d months", seconds/2592000)
}
return fmt.Sprintf("%d years", seconds/31557600)
}
return fmt.Sprint(i)
}
func fmtShardDuration(i interface{}) string {
seconds, ok := i.(int)
if ok {
var hours = seconds / 3600
if hours < 24 {
return fmt.Sprintf("%dh", hours)
}
if hours%24 > 0 {
return fmt.Sprintf("%dd %dh", hours/24, hours%24)
}
return fmt.Sprintf("%dd", hours/24)
}
return fmt.Sprint(i)
}