From e190c7c78d1129819ad7a6d0179e3c1937708ee9 Mon Sep 17 00:00:00 2001 From: Bas Harenslak Date: Sun, 1 Oct 2017 22:18:50 +0200 Subject: [PATCH] Add list blocks command to CLI --- cmd/tsdb/main.go | 8 ++++++++ db.go | 20 ++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/cmd/tsdb/main.go b/cmd/tsdb/main.go index 60f39e5d5..514a0bed1 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("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) + } + fmt.Println(db.PrintBlocks()) } flag.CommandLine.Set("log.level", "debug") } diff --git a/db.go b/db.go index 24437ada0..d353040ca 100644 --- a/db.go +++ b/db.go @@ -38,6 +38,7 @@ import ( "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/tsdb/chunks" "github.com/prometheus/tsdb/labels" + "github.com/ryanuber/columnize" ) // DefaultOptions used for the DB. They are sane for setups using @@ -228,6 +229,25 @@ func (db *DB) Dir() string { return db.dir } +func (db *DB) PrintBlocks() string { + db.mtx.RLock() + defer db.mtx.RUnlock() + + var output []string + output = append(output, "BLOCK ULID | MIN TIME | MAX TIME | NUM SAMPLES | NUM CHUNKS | NUM SERIES") + for _, b := range db.blocks { + output = append(output, fmt.Sprintf("%v | %v | %v | %v | %v | %v", + b.Meta().ULID, + b.Meta().MinTime, + b.Meta().MaxTime, + b.Meta().Stats.NumSamples, + b.Meta().Stats.NumChunks, + b.Meta().Stats.NumSeries, + )) + } + return columnize.SimpleFormat(output) +} + func (db *DB) run() { defer close(db.donec)