Files
esctl/pkg/es/shard.go

148 lines
3.1 KiB
Go
Raw Normal View History

2026-05-19 13:58:08 +02:00
/*
Copyright © 2026 Thomas von Dein
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package es
import (
"context"
"fmt"
"log/slog"
"codeberg.org/scip/esctl/pkg/cfg"
"codeberg.org/scip/esctl/pkg/printer"
2026-05-19 13:58:08 +02:00
"github.com/elastic/go-elasticsearch/v9/typedapi/cat/shards"
)
func colorzizeShard(conf *cfg.Config, state, name string) string {
2026-05-19 13:58:08 +02:00
color := "red" // in case of RELOCATING and UNASSIGNED
switch state {
case "STARTED":
color = "green"
case "INITIALIZING":
color = "yellow"
}
return printer.Colorize(conf, color, name)
2026-05-19 13:58:08 +02:00
}
func resolvePrirep(state string) string {
switch state {
case "p":
return "primary"
}
return "replica"
}
func filterShards(conf *cfg.Config, shardlist shards.Response) shards.Response {
filtered := shards.Response{}
size := len(shardlist)
if conf.MaxItems > 0 {
if size > conf.MaxItems {
size = conf.MaxItems
}
}
for idx, shard := range shardlist {
if conf.Failed {
if *shard.State == "STARTED" {
continue
}
}
if conf.Primary {
if *shard.Prirep != "p" {
continue
}
}
if idx == size-1 {
break
}
filtered = append(filtered, shard)
}
return filtered
}
func ShardList(conf *cfg.Config) error {
res, err := conf.DefaultCluster.ES().Cat.Shards().
2026-05-19 13:58:08 +02:00
Do(context.Background())
if err != nil {
return fmt.Errorf("failed to get shards: %s", esErrorString(err))
2026-05-19 13:58:08 +02:00
}
shardlist := filterShards(conf, res)
slog.Debug("ES result", "shards", res)
return printShards(conf, shardlist)
}
func printShards(conf *cfg.Config, shardlist shards.Response) error {
headers := []string{"index", "shard", "is primary", "store", "dataset", "docs"}
if conf.Verbose {
headers = append(headers, "node", "ip")
}
table := printer.NewTable(conf, len(headers), len(shardlist))
2026-05-19 13:58:08 +02:00
table.Addheaders(headers...)
for idx, shard := range shardlist {
name := colorzizeShard(conf, *shard.State, *shard.Index)
2026-05-19 13:58:08 +02:00
2026-07-07 07:29:03 +02:00
table.Entries[idx] = []any{
2026-05-19 13:58:08 +02:00
name,
*shard.Shard,
resolvePrirep(*shard.Prirep),
*shard.Store,
*shard.Dataset,
*shard.Docs,
}
if conf.Verbose {
table.Entries[idx] = append(table.Entries[idx],
2026-05-19 13:58:08 +02:00
*shard.Node,
*shard.Ip,
)
}
}
table.Sort()
if err := table.Print(); err != nil {
2026-05-19 13:58:08 +02:00
return err
}
return nil
}
func ShardShow(conf *cfg.Config, index string) error {
res, err := conf.DefaultCluster.ES().Cat.Shards().Index(index).
2026-05-19 13:58:08 +02:00
Do(context.Background())
if err != nil {
return fmt.Errorf("failed to get shards: %s", esErrorString(err))
2026-05-19 13:58:08 +02:00
}
slog.Debug("ES result", "shards", res)
conf.Verbose = true
return printShards(conf, res)
}