Skip to content
This repository was archived by the owner on Jul 6, 2021. It is now read-only.

Commit f12ea76

Browse files
committed
Rework 5+ items in conclusions, the sorting in L003 implemented.
1 parent a88164e commit f12ea76

File tree

4 files changed

+74
-18
lines changed

4 files changed

+74
-18
lines changed

pghrep/src/checkup/checkuputil.go

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,12 @@ func GetUniques(array []string) []string {
153153
}
154154

155155
func LimitList(array []string) []string {
156-
if len(array) <= RECOMMENDATION_ITEMS_LIMIT {
157-
return array
158-
} else {
156+
if len(array) > (RECOMMENDATION_ITEMS_LIMIT + 1) {
159157
limitedArray := array[0:RECOMMENDATION_ITEMS_LIMIT]
160158
limitedArray = append(limitedArray, MSG_ETC_ITEM)
161159
return limitedArray
160+
} else {
161+
return array
162162
}
163163
}
164164

@@ -226,8 +226,8 @@ func GetMasterHostName(hosts ReportHosts) string {
226226
return firstHostName
227227
}
228228

229-
// Get map keys sorted by field num inside struct
230-
func GetItemsSortedByNum(data interface{}) []string {
229+
// Get map keys sorted by defined int field inside struct
230+
func SortItemsByInt(data interface{}, field string, reverse bool) []string {
231231
var result []string
232232
var numData map[int]string = map[int]string{}
233233
var keys []int
@@ -245,7 +245,7 @@ func GetItemsSortedByNum(data interface{}) []string {
245245
continue
246246
}
247247

248-
valNum := val.FieldByName("Num")
248+
valNum := val.FieldByName(field)
249249

250250
if valNum.Kind() == reflect.Invalid {
251251
continue
@@ -259,10 +259,64 @@ func GetItemsSortedByNum(data interface{}) []string {
259259

260260
sort.Ints(keys)
261261

262+
if reverse {
263+
sort.Sort(sort.Reverse(sort.IntSlice(keys)))
264+
}
265+
266+
for _, key := range keys {
267+
result = append(result, numData[key])
268+
}
269+
}
270+
271+
return result
272+
}
273+
274+
// Get map keys sorted by defined float64 field inside struct
275+
func SortItemsByFloat64(data interface{}, field string, reverse bool) []string {
276+
var result []string
277+
var numData map[float64]string = map[float64]string{}
278+
var keys []float64
279+
280+
v := reflect.ValueOf(data)
281+
282+
if v.Kind() == reflect.Map {
283+
v2 := v.MapKeys()
284+
285+
for _, itemData := range v2 {
286+
id := itemData.Interface()
287+
val := v.MapIndex(itemData)
288+
289+
if val.Kind() != reflect.Struct {
290+
continue
291+
}
292+
293+
valNum := val.FieldByName(field)
294+
295+
if valNum.Kind() == reflect.Invalid {
296+
continue
297+
}
298+
299+
num := valNum.Interface()
300+
floatNum := num.(float64)
301+
numData[floatNum] = id.(string)
302+
keys = append(keys, floatNum)
303+
}
304+
305+
sort.Float64s(keys)
306+
307+
if reverse {
308+
sort.Sort(sort.Reverse(sort.Float64Slice(keys)))
309+
}
310+
262311
for _, key := range keys {
263312
result = append(result, numData[key])
264313
}
265314
}
266315

267316
return result
268317
}
318+
319+
// Get map keys sorted by field num inside struct
320+
func GetItemsSortedByNum(data interface{}) []string {
321+
return SortItemsByInt(data, "Num", false)
322+
}

pghrep/src/checkup/f004/f004_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func TestF004Warnig(t *testing.T) {
138138
BloatRatioFactor: 2.46177370030581,
139139
},
140140
"table_3": F004HeapBloat{
141-
Num: 2,
141+
Num: 3,
142142
IsNa: "",
143143
TableName: "table_3",
144144
ExtraSizeBytes: 3915776,
@@ -153,7 +153,7 @@ func TestF004Warnig(t *testing.T) {
153153
BloatRatioFactor: 2.46177370030581,
154154
},
155155
"table_4": F004HeapBloat{
156-
Num: 2,
156+
Num: 4,
157157
IsNa: "",
158158
TableName: "table_4",
159159
ExtraSizeBytes: 3915776,
@@ -168,7 +168,7 @@ func TestF004Warnig(t *testing.T) {
168168
BloatRatioFactor: 2.46177370030581,
169169
},
170170
"table_5": F004HeapBloat{
171-
Num: 2,
171+
Num: 5,
172172
IsNa: "",
173173
TableName: "table_5",
174174
ExtraSizeBytes: 3915776,
@@ -183,7 +183,7 @@ func TestF004Warnig(t *testing.T) {
183183
BloatRatioFactor: 2.46177370030581,
184184
},
185185
"table_6": F004HeapBloat{
186-
Num: 2,
186+
Num: 6,
187187
IsNa: "",
188188
TableName: "table_6",
189189
ExtraSizeBytes: 3915776,

pghrep/src/checkup/l003/l003.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ func L003Process(report L003Report) (checkup.ReportResult, error) {
1818
var tables []string
1919

2020
for _, hostData := range report.Results {
21-
for _, tableData := range hostData.Data.Tables {
21+
sortedTables := checkup.SortItemsByFloat64(hostData.Data.Tables, "CapacityUsedPercent", true)
22+
for _, table := range sortedTables {
23+
tableData := hostData.Data.Tables[table]
2224
if tableData.CapacityUsedPercent <= MAX_RATIO_PERCENT {
2325
continue
2426
}

pghrep/src/checkup/l003/l003_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,20 +82,20 @@ func TestL003P1_N(t *testing.T) {
8282
var hostResult L003ReportHostResult = L003ReportHostResult{
8383
Data: L003ReportHostResultData{
8484
Tables: map[string]L003Table{
85-
"test_schema.orders": L003Table{
86-
Table: "test_schema.orders",
87-
Pk: "id",
88-
Type: "int4",
89-
CurrentMaxValue: 80000000,
90-
CapacityUsedPercent: 37.25,
91-
},
9285
"test_schema.orders_A": L003Table{
9386
Table: "test_schema.orders_A",
9487
Pk: "id",
9588
Type: "int4",
9689
CurrentMaxValue: 300000000,
9790
CapacityUsedPercent: 13.97,
9891
},
92+
"test_schema.ordersB": L003Table{
93+
Table: "test_schema.orders_B",
94+
Pk: "id",
95+
Type: "int4",
96+
CurrentMaxValue: 80000000,
97+
CapacityUsedPercent: 37.25,
98+
},
9999
},
100100
MinTableSizeBytes: 0,
101101
},

0 commit comments

Comments
 (0)