-
Notifications
You must be signed in to change notification settings - Fork 2
/
common.go
161 lines (135 loc) · 3.73 KB
/
common.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
package kube
import (
"fmt"
"strconv"
"strings"
"github.com/logrusorgru/aurora/v3"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
)
const (
warning_threshold = 90.00
critical_threshold = 95.00
)
const (
// nvidia.com/gpu, number
ResourceNvidiaGpuCounts v1.ResourceName = "nvidia.com/gpu"
// aliyun.com/gpu-count, number
ResourceAliyunGpuCounts v1.ResourceName = "aliyun.com/gpu-count"
// aliyun.com/gpu-mem, number
ResourceAliyunGpuMem v1.ResourceName = "aliyun.com/gpu-mem"
)
// NewGpuResource returns the list of NewGpuResource
func NewGpuResource(name v1.ResourceName, rl *v1.ResourceList) *resource.Quantity {
if val, ok := (*rl)[name]; ok {
return &val
}
return rl.Name(name, resource.DecimalSI)
}
//calcPercentage
func calcPercentage(dividend, divisor int64) float64 {
if divisor > 0 {
value, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", float64(float64(dividend)/float64(divisor)*100)), 64)
return value
}
return float64(0)
}
type MemoryResource struct {
*resource.Quantity
}
//NewMemoryResource
func NewMemoryResource(value int64) *MemoryResource {
return &MemoryResource{resource.NewQuantity(value, resource.BinarySI)}
}
//calcPercentage
func (r *MemoryResource) calcPercentage(divisor *resource.Quantity) float64 {
return calcPercentage(r.Value(), divisor.Value())
}
func (r *MemoryResource) String() string {
// XXX: Support more units
return fmt.Sprintf("%vMi", r.Value()/(1024*1024))
}
//ToQuantity
func (r *MemoryResource) ToQuantity() *resource.Quantity {
return resource.NewQuantity(r.Value(), resource.BinarySI)
}
type CpuResource struct {
*resource.Quantity
}
//NewCpuResource
func NewCpuResource(value int64) *CpuResource {
r := resource.NewMilliQuantity(value, resource.DecimalSI)
return &CpuResource{r}
}
//String
func (r *CpuResource) String() string {
// XXX: Support more units
return fmt.Sprintf("%vm", r.MilliValue())
}
//newFormat
func newFormat(a string, b string) string {
return fmt.Sprintf("%s/%s", a, b)
}
//intToString int转string
func intToString(a int) string {
str := strconv.Itoa(a)
return str
}
//float64ToString float64转string
func float64ToString(s float64) string {
//return strconv.FormatFloat(s, 'G', -1, 32)
return fmt.Sprintf("%v%%", strconv.FormatFloat(s, 'G', -1, 64))
}
//int64ToString int64转string
func int64ToString(a int64) string {
str := strconv.FormatInt(a, 10)
return str
}
//StringTofloat64
func stringTofloat64(a string) float64 {
value, _ := strconv.ParseFloat(a, 64)
return value
}
//calcPercentage
func (r *CpuResource) calcPercentage(divisor *resource.Quantity) float64 {
return calcPercentage(r.MilliValue(), divisor.MilliValue())
}
//ToQuantity
func (r *CpuResource) ToQuantity() *resource.Quantity {
return resource.NewMilliQuantity(r.MilliValue(), resource.DecimalSI)
}
//FieldString
func FieldString(str string) float64 {
switch {
case strings.Contains(str, "%"):
str1 := strings.Split(str, "%")
value, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", stringTofloat64(str1[0])), 64)
return value
case strings.Contains(str, "Mi"):
str1 := strings.Split(str, "Mi")
value, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", stringTofloat64(str1[0])), 64)
return value
case strings.Contains(str, "m"):
str1 := strings.Split(str, "m")
value, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", stringTofloat64(str1[0])), 64)
return value
default:
return float64(0)
}
}
//Compare
func ExceedsCompare(a string) string {
if FieldString(a) > float64(critical_threshold) {
return redColor(a)
} else if FieldString(a) > float64(warning_threshold) {
return yellowColor(a)
} else {
return a
}
}
func redColor(s string) string {
return fmt.Sprintf("%s", aurora.Red(s))
}
func yellowColor(s string) string {
return fmt.Sprintf("%s", aurora.Yellow(s))
}