diff --git a/cmd/tsdb/main.go b/cmd/tsdb/main.go index 60f39e5d5..fe84340c0 100644 --- a/cmd/tsdb/main.go +++ b/cmd/tsdb/main.go @@ -43,6 +43,8 @@ func main() { benchWriteOutPath = benchWriteCmd.Flag("out", "set the output path").Default("benchout/").String() benchWriteNumMetrics = benchWriteCmd.Flag("metrics", "number of metrics to read").Default("10000").Int() benchSamplesFile = benchWriteCmd.Arg("file", "input file with samples data, default is (../../testdata/20k.series)").Default("../../testdata/20k.series").String() + listCmd = cli.Command("ls", "list db blocks") + listPath = listCmd.Arg("db path", "database path (default is benchout/storage)").Default("benchout/storage").String() ) switch kingpin.MustParse(cli.Parse(os.Args[1:])) { @@ -53,6 +55,12 @@ func main() { samplesFile: *benchSamplesFile, } wb.run() + case listCmd.FullCommand(): + db, err := tsdb.Open(*listPath, nil, nil, nil) + if err != nil { + exitWithError(err) + } + printBlocks(db.Blocks()) } flag.CommandLine.Set("log.level", "debug") } @@ -323,3 +331,21 @@ func exitWithError(err error) { fmt.Fprintln(os.Stderr, err) os.Exit(1) } + +func printBlocks(blocks []tsdb.DiskBlock) { + tw := tsdb.GetNewTabWriter(os.Stdout) + defer tw.Flush() + + fmt.Fprintln(tw, "BLOCK ULID\tMIN TIME\tMAX TIME\tNUM SAMPLES\tNUM CHUNKS\tNUM SERIES") + for _, b := range blocks { + fmt.Fprintf(tw, + "%v\t%v\t%v\t%v\t%v\t%v\n", + b.Meta().ULID, + b.Meta().MinTime, + b.Meta().MaxTime, + b.Meta().Stats.NumSamples, + b.Meta().Stats.NumChunks, + b.Meta().Stats.NumSeries, + ) + } +} diff --git a/tabwriter.go b/tabwriter.go new file mode 100644 index 000000000..8e84a9c67 --- /dev/null +++ b/tabwriter.go @@ -0,0 +1,18 @@ +package tsdb + +import ( + "io" + "text/tabwriter" +) + +const ( + minwidth = 0 + tabwidth = 0 + padding = 2 + padchar = ' ' + flags = 0 +) + +func GetNewTabWriter(output io.Writer) *tabwriter.Writer { + return tabwriter.NewWriter(output, minwidth, tabwidth, padding, padchar, flags) +}