scorch mergeplan.ToBarChart() refactored to callable API
Refactored out API so it's usable from other places.
This commit is contained in:
parent
dc4df18001
commit
0539744e90
|
@ -18,8 +18,10 @@
|
||||||
package mergeplan
|
package mergeplan
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
"sort"
|
"sort"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// A Segment represents the information that the planner needs to
|
// A Segment represents the information that the planner needs to
|
||||||
|
@ -292,3 +294,59 @@ func ScoreSegments(segments []Segment, o *MergePlanOptions) float64 {
|
||||||
|
|
||||||
return score
|
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")
|
||||||
|
}
|
||||||
|
|
|
@ -20,7 +20,6 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
|
||||||
"testing"
|
"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)
|
fmt.Printf("%s %d.%d ---------- %s\n", descrip, cycle, step, suffix)
|
||||||
|
fmt.Printf("%s\n", ToBarChart(descrip, 100, segments, plan))
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user