@@ -70,7 +70,6 @@ func main() {
7070 printBlocks (db .Blocks (), listCmdHumanReadable )
7171
7272 case scanCmd .FullCommand ():
73- fmt .Println ("Scanning for temporary files..." )
7473 var tmpFiles []string
7574 filepath .Walk (* scanPath , func (path string , f os.FileInfo , _ error ) error {
7675 if ! f .IsDir () {
@@ -86,17 +85,35 @@ These are usually caused by a crash or incomplete compaction and
8685are safe to delete as long as you are sure that no other application is currently using this database.` )
8786
8887 promptDelete (tmpFiles , scanCmdHumanReadable )
89-
9088 }
9189
9290 scanner , err := tsdb .NewDBScanner (* scanPath )
9391 if err != nil {
9492 exitWithError (err )
9593 }
9694
97- for err , useless := range scanner .Scan () {
98- fmt .Printf ("Deleting these will loose all data in the listed time ranges.%v" , err )
99- promptDelete (useless , scanCmdHumanReadable )
95+ for err , unreadable := range scanner .Scan () {
96+ switch err .(type ) {
97+ case tsdb.ErrOverlap :
98+ var biggestIndex int
99+ biggest := & tsdb.Block {}
100+ for i , b := range unreadable {
101+ if b .Meta ().Stats .NumSamples > biggest .Meta ().Stats .NumSamples {
102+ biggest = b
103+ biggestIndex = i
104+ }
105+ }
106+ // Don't delete the bigest block in the overlaps.
107+ unreadable = append (unreadable [:biggestIndex ], unreadable [biggestIndex + 1 :]... )
108+
109+ fmt .Printf ("\n %v \n Deleting these will loose all data in the listed time ranges.\n \n " , err )
110+ fmt .Printf ("\n Block %v contains most samples is ommited from the list and it won't be deleted! \n \n " , biggest )
111+
112+ default :
113+ fmt .Printf ("\n Unreadable blocks : %v \n Deleting these will loose all data in the listed time ranges.\n \n " , err )
114+
115+ }
116+ promptDelete (unreadable , scanCmdHumanReadable )
100117 }
101118 }
102119 flag .CommandLine .Set ("log.level" , "debug" )
@@ -116,7 +133,7 @@ func promptDelete(i interface{}, humanReadable *bool) {
116133 printTmps (paths , humanReadable )
117134 }
118135
119- fmt .Printf ("Do you want to delete these (y/N)? " )
136+ fmt .Printf ("DELETE (y/N)? " )
120137 var s string
121138 _ , err := fmt .Scanln (& s )
122139 if err != nil {
@@ -131,8 +148,8 @@ func promptDelete(i interface{}, humanReadable *bool) {
131148 if err := os .Remove (path ); err != nil {
132149 fmt .Printf ("error deleting: %v, %v" , path , err )
133150 }
134- fmt .Printf ("%v \n " , path )
135151 }
152+ return
136153 }
137154 if s == "n" || s == "no" {
138155 return
@@ -420,26 +437,39 @@ func exitWithError(err error) {
420437}
421438
422439func printTmps (tmps []string , humanReadable * bool ) {
440+ tw := tabwriter .NewWriter (os .Stdout , 0 , 0 , 2 , ' ' , 0 )
441+ defer tw .Flush ()
423442
424- fmt .Println (tmps )
443+ fmt .Fprintln (tw , "PATH\t SIZE\t DATE\t " )
444+ for _ , path := range tmps {
445+ f , e := os .Stat (path )
446+ if e != nil {
447+ exitWithError (e )
448+ }
449+ fmt .Fprintf (tw ,
450+ "%v\t %v\t %v\n " ,
451+ path , f .Size (), f .ModTime ().Format ("2006-Jan-2 15:04:05 -07:00 MST" ),
452+ )
453+ }
425454}
426455
427456func printBlocks (blocks []* tsdb.Block , humanReadable * bool ) {
428457 tw := tabwriter .NewWriter (os .Stdout , 0 , 0 , 2 , ' ' , 0 )
429458 defer tw .Flush ()
430459
431- fmt .Fprintln (tw , "BLOCK ULID\t MIN TIME\t MAX TIME\t NUM SAMPLES\t NUM CHUNKS\t NUM SERIES" )
460+ fmt .Fprintln (tw , "BLOCK ULID\t MIN TIME\t MAX TIME\t NUM SAMPLES\t NUM CHUNKS\t NUM SERIES\t PATH " )
432461 for _ , b := range blocks {
433462 meta := b .Meta ()
434463
435464 fmt .Fprintf (tw ,
436- "%v\t %v\t %v\t %v\t %v\t %v\n " ,
465+ "%v\t %v\t %v\t %v\t %v\t %v\t %v \ n " ,
437466 meta .ULID ,
438467 getFormatedTime (meta .MinTime , humanReadable ),
439468 getFormatedTime (meta .MaxTime , humanReadable ),
440469 meta .Stats .NumSamples ,
441470 meta .Stats .NumChunks ,
442471 meta .Stats .NumSeries ,
472+ b .Dir (),
443473 )
444474 }
445475}
0 commit comments