From 0539744e901cc74f2ba7d1dd89d6f8bdc893d79b Mon Sep 17 00:00:00 2001 From: Steve Yen Date: Sat, 16 Dec 2017 08:39:10 -0800 Subject: [PATCH] scorch mergeplan.ToBarChart() refactored to callable API Refactored out API so it's usable from other places. --- index/scorch/mergeplan/merge_plan.go | 58 +++++++++++++++++++++++ index/scorch/mergeplan/merge_plan_test.go | 47 +----------------- 2 files changed, 59 insertions(+), 46 deletions(-) diff --git a/index/scorch/mergeplan/merge_plan.go b/index/scorch/mergeplan/merge_plan.go index 61510c1c..c77fe37f 100644 --- a/index/scorch/mergeplan/merge_plan.go +++ b/index/scorch/mergeplan/merge_plan.go @@ -18,8 +18,10 @@ package mergeplan import ( + "fmt" "math" "sort" + "strings" ) // A Segment represents the information that the planner needs to @@ -292,3 +294,59 @@ func ScoreSegments(segments []Segment, o *MergePlanOptions) float64 { return score } + +// ------------------------------------------ + +// ToBarChart returns an ASCII rendering of the segments and the plan. +// The barMax is the max width of the bars in the bar chart. +func ToBarChart(prefix string, barMax int, segments []Segment, plan *MergePlan) string { + rv := make([]string, 0, len(segments)) + + var maxFullSize int64 + for _, segment := range segments { + if maxFullSize < segment.FullSize() { + maxFullSize = segment.FullSize() + } + } + if maxFullSize < 0 { + maxFullSize = 1 + } + + for _, segment := range segments { + barFull := int(segment.FullSize()) + barLive := int(segment.LiveSize()) + + if maxFullSize > int64(barMax) { + barFull = int(float64(barMax) * float64(barFull) / float64(maxFullSize)) + barLive = int(float64(barMax) * float64(barLive) / float64(maxFullSize)) + } + + barKind := " " + barChar := "." + + if plan != nil { + TASK_LOOP: + for taski, task := range plan.Tasks { + for _, taskSegment := range task.Segments { + if taskSegment == segment { + barKind = "*" + barChar = fmt.Sprintf("%d", taski) + break TASK_LOOP + } + } + } + } + + bar := + strings.Repeat(barChar, barLive)[0:barLive] + + strings.Repeat("x", barFull-barLive)[0:barFull-barLive] + + rv = append(rv, fmt.Sprintf("%s %5d: %5d /%5d - %s %s", prefix, + segment.Id(), + segment.LiveSize(), + segment.FullSize(), + barKind, bar)) + } + + return strings.Join(rv, "\n") +} diff --git a/index/scorch/mergeplan/merge_plan_test.go b/index/scorch/mergeplan/merge_plan_test.go index bf2f24be..ca4d80bd 100644 --- a/index/scorch/mergeplan/merge_plan_test.go +++ b/index/scorch/mergeplan/merge_plan_test.go @@ -20,7 +20,6 @@ import ( "os" "reflect" "sort" - "strings" "testing" ) @@ -451,49 +450,5 @@ func emit(descrip string, cycle int, step int, segments []Segment, plan *MergePl } fmt.Printf("%s %d.%d ---------- %s\n", descrip, cycle, step, suffix) - - var maxFullSize int64 - for _, segment := range segments { - if maxFullSize < segment.FullSize() { - maxFullSize = segment.FullSize() - } - } - - barMax := 100 - - for _, segment := range segments { - barFull := int(segment.FullSize()) - barLive := int(segment.LiveSize()) - - if maxFullSize > int64(barMax) { - barFull = int(float64(barMax) * float64(barFull) / float64(maxFullSize)) - barLive = int(float64(barMax) * float64(barLive) / float64(maxFullSize)) - } - - barKind := " " - barChar := "." - - if plan != nil { - TASK_LOOP: - for taski, task := range plan.Tasks { - for _, taskSegment := range task.Segments { - if taskSegment == segment { - barKind = "*" - barChar = fmt.Sprintf("%d", taski) - break TASK_LOOP - } - } - } - } - - bar := - strings.Repeat(barChar, barLive)[0:barLive] + - strings.Repeat("x", barFull-barLive)[0:barFull-barLive] - - fmt.Printf("%s %5d: %5d /%5d - %s %s\n", descrip, - segment.Id(), - segment.LiveSize(), - segment.FullSize(), - barKind, bar) - } + fmt.Printf("%s\n", ToBarChart(descrip, 100, segments, plan)) }