From 1e9b4efa8053e3d27be8a5803c91bf37923ffe3b Mon Sep 17 00:00:00 2001 From: Taras Madan Date: Fri, 12 Jul 2024 11:47:53 +0200 Subject: pkg/cover/heatmap.go: add blocks count --- pkg/cover/heatmap.go | 29 ++++++++++++++++----- pkg/cover/heatmap_test.go | 56 ++++++++++++++++++++++------------------ pkg/cover/templates/heatmap.html | 13 +++++++++- 3 files changed, 66 insertions(+), 32 deletions(-) diff --git a/pkg/cover/heatmap.go b/pkg/cover/heatmap.go index e5d2f4476..9cc3286e5 100644 --- a/pkg/cover/heatmap.go +++ b/pkg/cover/heatmap.go @@ -20,11 +20,12 @@ import ( ) type templateHeatmapRow struct { - Items []*templateHeatmapRow - Name string - Coverage []int64 - IsDir bool - Depth int + Items []*templateHeatmapRow + Name string + Coverage []int64 + IsDir bool + Depth int + LastDayInstrumented int64 builder map[string]*templateHeatmapRow instrumented map[civil.Date]int64 @@ -72,6 +73,10 @@ func (thm *templateHeatmapRow) prepareDataFor(dates []civil.Date) { } thm.Coverage = append(thm.Coverage, dateCoverage) } + if len(dates) > 0 { + lastDate := dates[len(dates)-1] + thm.LastDayInstrumented = thm.instrumented[lastDate] + } for _, item := range thm.builder { item.prepareDataFor(dates) } @@ -169,6 +174,18 @@ func DoHeatMap(w io.Writer, ns string, dateFrom, dateTo civil.Date) error { return heatmapTemplate.Execute(w, templateData) } +func approximateInstrumented(points int64) string { + dim := "_" + if points > 10000 { + dim = "K" + points /= 1000 + } + return fmt.Sprintf("%d%s", points, dim) +} + //go:embed templates/heatmap.html var templatesHeatmap string -var heatmapTemplate = template.Must(template.New("").Parse(templatesHeatmap)) +var templateHeatmapFuncs = template.FuncMap{ + "approxInstr": approximateInstrumented, +} +var heatmapTemplate = template.Must(template.New("").Funcs(templateHeatmapFuncs).Parse(templatesHeatmap)) diff --git a/pkg/cover/heatmap_test.go b/pkg/cover/heatmap_test.go index c40d14ccc..d450a5243 100644 --- a/pkg/cover/heatmap_test.go +++ b/pkg/cover/heatmap_test.go @@ -40,17 +40,19 @@ func TestFilesCoverageToTemplateData(t *testing.T) { Root: &templateHeatmapRow{ Items: []*templateHeatmapRow{ { - Items: []*templateHeatmapRow{}, - Name: "file1", - Coverage: []int64{100}, - IsDir: false, - Depth: 0, + Items: []*templateHeatmapRow{}, + Name: "file1", + Coverage: []int64{100}, + IsDir: false, + Depth: 0, + LastDayInstrumented: 1, }, }, - Name: "", - Coverage: []int64{100}, - IsDir: false, - Depth: 0, + Name: "", + Coverage: []int64{100}, + IsDir: false, + Depth: 0, + LastDayInstrumented: 1, }, Dates: []string{"2024-07-01"}, }, @@ -77,28 +79,32 @@ func TestFilesCoverageToTemplateData(t *testing.T) { { Items: []*templateHeatmapRow{ { - Items: []*templateHeatmapRow{}, - Name: "file1", - Coverage: []int64{100, 0}, - IsDir: false, - Depth: 1, + Items: []*templateHeatmapRow{}, + Name: "file1", + Coverage: []int64{100, 0}, + IsDir: false, + Depth: 1, + LastDayInstrumented: 0, }, { - Items: []*templateHeatmapRow{}, - Name: "file2", - Coverage: []int64{0, 0}, - IsDir: false, - Depth: 1, + Items: []*templateHeatmapRow{}, + Name: "file2", + Coverage: []int64{0, 0}, + IsDir: false, + Depth: 1, + LastDayInstrumented: 1, }, }, - Name: "dir", - Coverage: []int64{100, 0}, - IsDir: true, - Depth: 0, + Name: "dir", + Coverage: []int64{100, 0}, + IsDir: true, + Depth: 0, + LastDayInstrumented: 1, }, }, - Name: "", - Coverage: []int64{100, 0}, + Name: "", + Coverage: []int64{100, 0}, + LastDayInstrumented: 1, }, Dates: []string{"2024-07-01", "2024-07-02"}, }, diff --git a/pkg/cover/templates/heatmap.html b/pkg/cover/templates/heatmap.html index 9f5d1da2c..4d3e159b2 100644 --- a/pkg/cover/templates/heatmap.html +++ b/pkg/cover/templates/heatmap.html @@ -15,6 +15,11 @@ display: inline-block; width: 50px; } + .instrumented_column { + display: inline-block; + width: 70px; + text-align: right; + } .tree_depth_0 {width: 0px;} .tree_depth_1 {width: 20px;} .tree_depth_2 {width: 40px;} @@ -64,13 +69,16 @@
  • - total + total covered
    {{ range $cov := .Root.Coverage }}
    {{ $cov }}%
    {{ end }} +
    + of {{ approxInstr .Root.LastDayInstrumented }} blocks +

  • {{template "dir" .Root}} @@ -105,6 +113,9 @@ {{ $cov }}% {{ end }} +
    + {{ approxInstr $child.LastDayInstrumented }} +
    {{ if $child.IsDir }}