mirror of
https://codeberg.org/scip/esctl.git
synced 2026-08-24 10:04:18 +02:00
enhance/ilm-forecast (#51)
This commit is contained in:
@@ -34,7 +34,6 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
matchDuration = regexp.MustCompile(`(\d+)([dhms])`)
|
||||
IlmPhaseOrder = []string{"hot", "warm", "cold", "frozen", "delete"}
|
||||
)
|
||||
|
||||
@@ -47,6 +46,7 @@ type NextPhase struct {
|
||||
|
||||
type PhaseData struct {
|
||||
index string
|
||||
policy string
|
||||
size int64
|
||||
age time.Duration
|
||||
minage time.Duration
|
||||
@@ -86,30 +86,55 @@ types.Lifecycle{
|
||||
hot for 7 days or 25Gig, then Rollover => warm 14 days => frozen 60 days => delete
|
||||
*/
|
||||
|
||||
func IlmForecastList(conf *cfg.Config) error {
|
||||
func IlmForecastList(conf *cfg.Config, filter string) error {
|
||||
phaseData, err := getIlmPhaseData(conf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
table := printer.NewTable(conf, 8, 0)
|
||||
table.Addheaders("index", "current size", "current age", "virtual age", "min age", "min size", "current phase", "next phase")
|
||||
flt := regexp.MustCompile(filter)
|
||||
|
||||
minage := time.Duration(0)
|
||||
if conf.Ilm.MinAge != "" {
|
||||
minage = parseDuration(conf.Ilm.MinAge)
|
||||
}
|
||||
|
||||
headers := []string{"index", "current size", "current age",
|
||||
"virtual age", "min age", "min size", "current phase", "next phase"}
|
||||
if conf.Verbose {
|
||||
headers = append(headers, "ilm policy")
|
||||
}
|
||||
|
||||
table := printer.NewTable(conf, len(headers), 0)
|
||||
table.Addheaders(headers...)
|
||||
|
||||
for _, phase := range phaseData {
|
||||
if filter != "" && !flt.MatchString(phase.index) {
|
||||
continue
|
||||
}
|
||||
|
||||
if conf.Ilm.MinAge != "" && phase.age < minage {
|
||||
continue
|
||||
}
|
||||
|
||||
virtualAge := virtualAge(&phase)
|
||||
|
||||
table.AddRow(
|
||||
row := []string{
|
||||
phase.index,
|
||||
humanize.Bytes(uint64(phase.size)),
|
||||
phase.age.String(),
|
||||
virtualAge.String(),
|
||||
|
||||
phase.minage.String(),
|
||||
formatDuration(phase.age),
|
||||
formatDuration(virtualAge),
|
||||
formatDuration(phase.minage),
|
||||
humanize.Bytes(uint64(phase.minsize)),
|
||||
|
||||
phase.currentPhase,
|
||||
phase.nextPhase,
|
||||
)
|
||||
}
|
||||
|
||||
if conf.Verbose {
|
||||
row = append(row, phase.policy)
|
||||
}
|
||||
|
||||
table.AddRow(row...)
|
||||
}
|
||||
|
||||
table.Sort()
|
||||
@@ -241,6 +266,7 @@ func getIlmPhaseData(conf *cfg.Config) ([]PhaseData, error) {
|
||||
|
||||
list = append(list, PhaseData{
|
||||
index: indexName,
|
||||
policy: *explain.Policy,
|
||||
size: size,
|
||||
age: age,
|
||||
minage: nextPhase.minage,
|
||||
@@ -365,39 +391,3 @@ func findNextPhase(policy types.IlmPolicy, currentPhase string) *NextPhase {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
/*
|
||||
We could use time.ParseDuration(), but this doesn't support days.
|
||||
|
||||
We could also use github.com/xhit/go-str2duration/v2, which does
|
||||
the job, but it's just another dependency, just for this little
|
||||
gem. And we don't need a time.Time value. And int is good enough
|
||||
for duration comparison.
|
||||
|
||||
Convert a duration into an integer. Valid time units are "s", "m", "h" and "d".
|
||||
|
||||
via https://codeberg.org/scip/tablizer/src/branch/main/lib/sort.go#L113
|
||||
*/
|
||||
func parseDuration(duration string) time.Duration {
|
||||
|
||||
seconds := 0
|
||||
|
||||
for _, match := range matchDuration.FindAllStringSubmatch(duration, -1) {
|
||||
if len(match) == 3 {
|
||||
durationvalue, _ := strconv.Atoi(match[1])
|
||||
|
||||
switch match[2][0] {
|
||||
case 'd':
|
||||
seconds += durationvalue * 86400
|
||||
case 'h':
|
||||
seconds += durationvalue * 3600
|
||||
case 'm':
|
||||
seconds += durationvalue * 60
|
||||
case 's':
|
||||
seconds += durationvalue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return time.Duration(seconds) * time.Second
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user