Skip to content

Commit 37a1782

Browse files
authored
Merge pull request #21 from SwissLife-OSS/add-pri-size
Add primary shard size column
2 parents 0dce13f + 3c9c27e commit 37a1782

File tree

2 files changed

+64
-27
lines changed

2 files changed

+64
-27
lines changed

cmd_ilm_list.go

Lines changed: 57 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ func ilmList(ctx context.Context, cmd *cli.Command) error {
2727

2828
phase := cmd.String("phase")
2929
sortColumns := cmd.StringSlice("sort")
30-
minSizeStr := cmd.String("min-size")
30+
minPriSizeStr := cmd.String("min-pri-size")
31+
minTotalSizeStr := cmd.String("min-total-size")
3132
minAge := time.Duration(cmd.Int("min-age-days")) * 24 * time.Hour
3233

3334
if !isRegionValid(region) {
@@ -39,17 +40,25 @@ func ilmList(ctx context.Context, cmd *cli.Command) error {
3940
return fmt.Errorf(`invalid region, expected format "<provider>-<region>", e.g. "azure-westeurope"`)
4041
}
4142

42-
allowedSortColumns := []string{"age", "size"}
43+
allowedSortColumns := []string{"age", "pri-size", "total-size"}
4344
for _, sortColumn := range sortColumns {
4445
if !slices.Contains(allowedSortColumns, sortColumn) {
4546
return fmt.Errorf("column %q is not allowed for sorting, use one of %v", sortColumn, allowedSortColumns)
4647
}
4748
}
4849

49-
var minSize int64
5050
var err error
51-
if minSizeStr != "" {
52-
minSize, err = units.FromHumanSize(minSizeStr)
51+
var minPriSize int64
52+
if minPriSizeStr != "" {
53+
minPriSize, err = units.FromHumanSize(minPriSizeStr)
54+
if err != nil {
55+
return fmt.Errorf("failed to parse minimum size: %w", err)
56+
}
57+
}
58+
59+
var minTotalSize int64
60+
if minTotalSizeStr != "" {
61+
minTotalSize, err = units.FromHumanSize(minTotalSizeStr)
5362
if err != nil {
5463
return fmt.Errorf("failed to parse minimum size: %w", err)
5564
}
@@ -71,20 +80,28 @@ func ilmList(ctx context.Context, cmd *cli.Command) error {
7180
return err
7281
}
7382

74-
indices, err := client.Cat.Indices().H("index", "store.size").Bytes(bytes.B).Do(ctx)
83+
indices, err := client.Cat.Indices().H("index", "store.size", "pri.store.size").Bytes(bytes.B).Do(ctx)
7584
if err != nil {
7685
return err
7786
}
7887

79-
sizes := make(map[string]int64, len(indices))
88+
totalSizes := make(map[string]int64, len(indices))
89+
priSizes := make(map[string]int64, len(indices))
8090

8191
for _, index := range indices {
82-
size, err := strconv.ParseInt(*index.StoreSize, 10, 64)
92+
totalSize, err := strconv.ParseInt(*index.StoreSize, 10, 64)
93+
if err != nil {
94+
return err
95+
}
96+
97+
totalSizes[*index.Index] = totalSize
98+
99+
priSize, err := strconv.ParseInt(*index.PriStoreSize, 10, 64)
83100
if err != nil {
84101
return err
85102
}
86103

87-
sizes[*index.Index] = size
104+
priSizes[*index.Index] = priSize
88105
}
89106

90107
ilms, err := client.Ilm.ExplainLifecycle("_all").OnlyManaged(true).Do(ctx)
@@ -93,13 +110,14 @@ func ilmList(ctx context.Context, cmd *cli.Command) error {
93110
}
94111

95112
type indexDetails struct {
96-
name string
97-
phase string
98-
action string
99-
step string
100-
policy string
101-
age time.Duration
102-
size int64
113+
name string
114+
phase string
115+
action string
116+
step string
117+
policy string
118+
age time.Duration
119+
priSize int64
120+
totalSize int64
103121
}
104122

105123
indexILM := make([]indexDetails, 0, len(ilms.Indices))
@@ -114,9 +132,15 @@ func ilmList(ctx context.Context, cmd *cli.Command) error {
114132
continue
115133
}
116134

117-
size := sizes[index]
135+
priSize := priSizes[index]
118136

119-
if size < minSize {
137+
if priSize < minPriSize {
138+
continue
139+
}
140+
141+
totalSize := totalSizes[index]
142+
143+
if totalSize < minTotalSize {
120144
continue
121145
}
122146

@@ -129,7 +153,7 @@ func ilmList(ctx context.Context, cmd *cli.Command) error {
129153
continue
130154
}
131155

132-
indexILM = append(indexILM, indexDetails{name: index, phase: *managed.Phase, action: *managed.Action, step: *managed.Step, policy: *managed.Policy, age: age, size: size})
156+
indexILM = append(indexILM, indexDetails{name: index, phase: *managed.Phase, action: *managed.Action, step: *managed.Step, policy: *managed.Policy, age: age, priSize: priSize, totalSize: totalSize})
133157
}
134158

135159
// Apply the sort criteria as less functions controlled by:
@@ -157,10 +181,19 @@ func ilmList(ctx context.Context, cmd *cli.Command) error {
157181
return false, false
158182
})
159183

160-
case "size":
184+
case "pri-size":
185+
lessFuncs = append(lessFuncs, func(i, j int) (final bool, less bool) {
186+
if indexILM[i].priSize != indexILM[j].priSize {
187+
return true, indexILM[i].priSize > indexILM[j].priSize
188+
}
189+
190+
return false, false
191+
})
192+
193+
case "total-size":
161194
lessFuncs = append(lessFuncs, func(i, j int) (final bool, less bool) {
162-
if indexILM[i].size != indexILM[j].size {
163-
return true, indexILM[i].size > indexILM[j].size
195+
if indexILM[i].totalSize != indexILM[j].totalSize {
196+
return true, indexILM[i].totalSize > indexILM[j].totalSize
164197
}
165198

166199
return false, false
@@ -182,12 +215,12 @@ func ilmList(ctx context.Context, cmd *cli.Command) error {
182215

183216
data := make([][]string, 0, len(indexILM))
184217
for _, item := range indexILM {
185-
data = append(data, []string{item.name, item.phase, item.action, item.step, item.policy, formatDuration(item.age), units.BytesSize(float64(item.size))})
218+
data = append(data, []string{item.name, item.phase, item.action, item.step, item.policy, formatDuration(item.age), units.BytesSize(float64(item.priSize)), units.BytesSize(float64(item.totalSize))})
186219
}
187220

188221
table := tablewriter.NewWriter(os.Stdout)
189222
table.Header([]string{
190-
"Index", "Phase", "Action", "Step", "Policy", "Age", "Size",
223+
"Index", "Phase", "Action", "Step", "Policy", "Age", "Pri Size", "Total Size",
191224
})
192225
err = table.Bulk(data)
193226
if err != nil {

main.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,15 @@ func run(ctx context.Context, args []string) error {
7171
&cli.StringSliceFlag{
7272
Name: "sort",
7373
Aliases: []string{"s"},
74-
Usage: "Sort indices by the given columns, allowed columns are: age, size",
74+
Usage: "Sort indices by the given columns, allowed columns are: age, pri-size, total-size",
7575
},
7676
&cli.StringFlag{
77-
Name: "min-size",
78-
Usage: "Minimum size of index in order to be contained in the result, supported units: k, m, g, t, p",
77+
Name: "min-total-size",
78+
Usage: "Minimum total size (primary shard + replicas) of index in order to be contained in the result, supported units: k, m, g, t, p",
79+
},
80+
&cli.StringFlag{
81+
Name: "min-pri-size",
82+
Usage: "Minimum primary shard size of index in order to be contained in the result, supported units: k, m, g, t, p",
7983
},
8084
&cli.IntFlag{
8185
Name: "min-age-days",

0 commit comments

Comments
 (0)