/
formatting_functions.go
217 lines (183 loc) · 4.76 KB
/
formatting_functions.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
/*
* Copyright (c) 2023, 2024 Oracle and/or its affiliates.
* Licensed under the Universal Permissive License v 1.0 as shown at
* https://oss.oracle.com/licenses/upl.
*/
package cmd
import (
"github.com/fatih/color"
"strconv"
"strings"
)
var (
red = color.New(color.FgRed).SprintFunc()
yellow = color.New(color.FgHiYellow).SprintFunc()
)
const stopped = "stopped"
// statusHAFormatter formats a column value and makes it Red if contains ENDANGERED.
var statusHAFormatter = func(s string) string {
if strings.Contains(s, endangered) {
return red(s)
}
return s
}
// hitRateFormatter formats a column value which represents a cache hit rate.
var hitRateFormatter = func(s string) string {
floatValue, err := strconv.ParseFloat(trimPercent(s), 32)
if err != nil {
return s
}
if floatValue > 75 {
return s
}
if floatValue > 50 {
return yellow(s)
}
return red(s)
}
// machineMemoryFormatting formats a column value which represents machine percent memory used.
var machineMemoryFormatting = func(s string) string {
floatValue, err := strconv.ParseFloat(trimPercent(s), 32)
if err != nil {
return s
}
if floatValue > 25 {
return s
}
if floatValue > 15 {
return yellow(s)
}
return red(s)
}
// errorFormatter formats a column value which represents an error or number that needs to be highlighted.
var errorFormatter = func(s string) string {
v, err := getInt64Value(s)
if err != nil || v == 0 {
return s
}
if v > 20 {
return red(s)
}
return yellow(s)
}
// endangeredPartitionsFormatter formats a column value which represents the number of endangered partitions.
var endangeredPartitionsFormatter = func(s string) string {
v, err := getInt64Value(s)
if err != nil || v == 0 || v == -1 {
return s
}
return red(s)
}
// vulnerablePartitionsFormatter formats a column value which represents the number of vulnerable or unbalanced partitions.
var vulnerablePartitionsFormatter = func(s string) string {
v, err := getInt64Value(s)
if err != nil || v == 0 || v == -1 {
return s
}
return yellow(s)
}
// packetFormatter formats a column value which represents packages where higher numbers need to be highlighted.
var packetFormatter = func(s string) string {
v, err := getInt64Value(s)
if err != nil {
return s
}
if v == 0 {
return s
}
if v > 10 {
return red(s)
}
return yellow(s)
}
// healthFormatter formats a column value when false will be displayed in red.
var healthFormatter = func(s string) string {
if s == stringFalse {
return red(s)
}
return s
}
// reporterFormatter formats a column value when "Stopped" will be displayed in red.
var reporterFormatter = func(s string) string {
if strings.Contains(strings.ToLower(s), stopped) {
return red(s)
}
return s
}
// trueBoolFormatter formats a column value when true will be displayed in red.
var trueBoolFormatter = func(s string) string {
if s == stringTrue {
return red(s)
}
return s
}
// yesBoolFormatter formats a column value when yes will be displayed in red.
var yesBoolFormatter = func(s string) string {
if strings.Contains(s, "yes") {
return red(s)
}
return s
}
// healthSummaryFormatter formats a column value for a health summary.
var healthSummaryFormatter = func(s string) string {
if !strings.Contains(s, "/") {
return s
}
// string contains something like "0/4"
result := strings.Split(s, "/")
if len(result) != 2 {
return s
}
value1, err := getInt64Value(result[0])
if err != nil {
return s
}
if value1 == 0 {
return red(s)
}
return yellow(s)
}
// healthMonitoringFormatter formats a column value for health monitoring summary.
var healthMonitoringFormatter = func(s string) string {
if strings.Contains(s, http200) || strings.Contains(s, "4") {
return s
}
if strings.Contains(s, "0/4") {
return red(s)
}
if strings.Contains(s, "503") || strings.Contains(s, "/") {
return yellow(s)
}
return red(s)
}
// healthSummaryFormatter formats a column value for federation state.
var federationStateFormatter = func(s string) string {
lowerCase := strings.ToLower(s)
if strings.Contains(lowerCase, "error") {
return red(s)
}
if strings.Contains(lowerCase, "paused") || strings.Contains(lowerCase, stopped) || strings.Contains(lowerCase, "connect_wait") {
return yellow(s)
}
return s
}
// networkStatsFormatter formats a column value representing publisher or receiver rates.
var networkStatsFormatter = func(s string) string {
floatValue, err := strconv.ParseFloat(trimPercent(s), 32)
if err != nil {
return s
}
if floatValue > 0.95 {
return s
}
if floatValue >= 0.9 {
return yellow(s)
}
return red(s)
}
func getInt64Value(s string) (int64, error) {
return strconv.ParseInt(strings.ReplaceAll(strings.TrimSpace(s), ",", ""), 10, 64)
}
func trimPercent(s string) string {
return strings.TrimSpace(strings.Replace(s, "%", "", 1))
}