Add snapshot inspect filter param

pull/9098/head
Joel Watson 4 years ago
parent 10f325dc81
commit 262fb3a0c3

@ -32,15 +32,18 @@ type cmd struct {
// flags // flags
detailed bool detailed bool
kvDepth int depth int
filter string
} }
func (c *cmd) init() { func (c *cmd) init() {
c.flags = flag.NewFlagSet("", flag.ContinueOnError) c.flags = flag.NewFlagSet("", flag.ContinueOnError)
c.flags.BoolVar(&c.detailed, "detailed", false, c.flags.BoolVar(&c.detailed, "detailed", false,
"Provides detailed information about KV store data.") "Provides detailed information about KV store data.")
c.flags.IntVar(&c.kvDepth, "kv-depth", 2, c.flags.IntVar(&c.depth, "depth", 2,
"The key prefix depth used to breakdown KV store data. Defaults to 2.") "The key prefix depth used to breakdown KV store data. Defaults to 2.")
c.flags.StringVar(&c.filter, "filter", "",
"Filter KV keys using this prefix filter.")
c.flags.StringVar( c.flags.StringVar(
&c.format, &c.format,
"format", "format",
@ -110,7 +113,7 @@ func (c *cmd) Run(args []string) int {
} }
}() }()
stats, kstats, totalSize, err := enhance(readFile, c.detailed, c.kvDepth) stats, kstats, totalSize, err := enhance(readFile, c.detailed, c.depth, c.filter)
if err != nil { if err != nil {
c.UI.Error(fmt.Sprintf("Error extracting snapshot data: %s", err)) c.UI.Error(fmt.Sprintf("Error extracting snapshot data: %s", err))
return 1 return 1
@ -203,7 +206,7 @@ func (r *countingReader) Read(p []byte) (n int, err error) {
// enhance utilizes ReadSnapshot to populate the struct with // enhance utilizes ReadSnapshot to populate the struct with
// all of the snapshot's itemized data // all of the snapshot's itemized data
func enhance(file io.Reader, detailed bool, kvDepth int) (map[structs.MessageType]typeStats, map[string]typeStats, int, error) { func enhance(file io.Reader, detailed bool, depth int, filter string) (map[structs.MessageType]typeStats, map[string]typeStats, int, error) {
stats := make(map[structs.MessageType]typeStats) stats := make(map[structs.MessageType]typeStats)
kstats := make(map[string]typeStats) kstats := make(map[string]typeStats)
cr := &countingReader{wrappedReader: file} cr := &countingReader{wrappedReader: file}
@ -231,19 +234,28 @@ func enhance(file io.Reader, detailed bool, kvDepth int) (map[structs.MessageTyp
if s.Name == "KVS" { if s.Name == "KVS" {
switch val := val.(type) { switch val := val.(type) {
case map[string]interface{}: case map[string]interface{}:
fmt.Println("map-match")
for k, v := range val { for k, v := range val {
depth := kvDepth
if k == "Key" { if k == "Key" {
// check for whether a filter is specified. if it is, skip
// any keys that don't match.
if len(filter) > 0 && !strings.HasPrefix(v.(string), filter) {
break
}
split := strings.Split(v.(string), "/") split := strings.Split(v.(string), "/")
// handle the situation where the key is shorter than
// the specified depth.
actualDepth := depth
if depth > len(split) { if depth > len(split) {
depth = len(split) actualDepth = len(split)
} }
prefix := strings.Join(split[0:depth], "/") prefix := strings.Join(split[0:actualDepth], "/")
kvs := kstats[prefix] kvs := kstats[prefix]
if kvs.Name == "" { if kvs.Name == "" {
kvs.Name = prefix kvs.Name = prefix
} }
kvs.Sum += size kvs.Sum += size
kvs.Count++ kvs.Count++
kstats[prefix] = kvs kstats[prefix] = kvs

Loading…
Cancel
Save