-
Notifications
You must be signed in to change notification settings - Fork 81
/
unit.go
107 lines (95 loc) · 2.75 KB
/
unit.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
// Copyright 2022 The kubegems.io Authors
//
// 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 prometheus
import (
"fmt"
"strings"
)
// - name: short
// explain: 默认
// # units: [n, u, m, "", K, Mil, Bil, Tri] # 1000进制
// - name: bytes
// explain: 数据字节量
// units: [B, KB, MB, GB, TB, PB] # 1024进制
// - name: bytes/sec
// explain: 数据字节速率
// units: [B/s, KB/s, MB/s, GB/s, TB/s, PB/s]
// - name: duration
// explain: 时间
// units: [ns, us, ms, s, m, h, d, w]
// - name: percent
// explain: 百分比
// units: ["0.0-1.0", "0-100"]
var (
// 单位表
UnitValueMap = map[string]UnitValue{
"B": defaultUnitValue,
"KB": {Op: "/", Value: "1024"},
"MB": {Op: "/", Value: "(1024 * 1024)"},
"GB": {Op: "/", Value: "(1024 * 1024 * 1024)"},
"TB": {Op: "/", Value: "(1024 * 1024 * 1024 * 1024)"},
"PB": {Op: "/", Value: "(1024 * 1024 * 1024 * 1024 * 1024)"},
"B/s": defaultUnitValue,
"KB/s": {Op: "/", Value: "1024"},
"MB/s": {Op: "/", Value: "(1024 * 1024)"},
"GB/s": {Op: "/", Value: "(1024 * 1024 * 1024)"},
"TB/s": {Op: "/", Value: "(1024 * 1024 * 1024 * 1024)"},
"PB/s": {Op: "/", Value: "(1024 * 1024 * 1024 * 1024 * 1024)"},
"us": {Op: "*", Value: "(1000 * 1000)"},
"ms": {Op: "*", Value: "1000"},
"s": defaultUnitValue,
"m": {Op: "/", Value: "60"},
"h": {Op: "/", Value: "(60 * 60)"},
"d": {Op: "/", Value: "(24 * 60 * 60)"},
"w": {Op: "/", Value: "(7 * 24 * 60 * 60)"},
"0.0-1.0": {Op: "*", Value: "100", Show: "%"},
"0-100": {Show: "%"},
}
defaultUnitValue = UnitValue{
Op: "*",
Value: "1",
}
)
type UnitValue struct {
Show string
Op string
Value string
}
func ParseUnit(unit string) (UnitValue, error) {
tmp := strings.SplitN(unit, "-", 2)
switch len(tmp) {
case 1:
if tmp[0] == "short" || tmp[0] == "" {
return UnitValue{}, nil
} else {
return UnitValue{}, fmt.Errorf("unit %s not valid", unit)
}
case 2:
if tmp[0] == "custom" {
return UnitValue{
Show: tmp[1],
}, nil
}
if ret, ok := UnitValueMap[tmp[1]]; ok {
if ret.Show == "" {
ret.Show = tmp[1]
}
return ret, nil
} else {
return UnitValue{}, fmt.Errorf("unit %s not valid", unit)
}
default:
return UnitValue{}, fmt.Errorf("unit %s not valid", unit)
}
}