From 72969efebf6db54abe270d0da9fd46171eafc61a Mon Sep 17 00:00:00 2001 From: Bart van Oort Date: Tue, 3 Aug 2021 22:44:22 +0200 Subject: [PATCH] Print a score for the entire category in the Markdown report, calculated as weighted average of all rules' scores --- api/api_test.go | 35 +++++++++++++++++++++++++++++++++++ api/report.go | 14 ++++++++++++++ utils/markdown/project.go | 15 ++++++++++----- 3 files changed, 59 insertions(+), 5 deletions(-) diff --git a/api/api_test.go b/api/api_test.go index 62a9f76..dc2b8eb 100644 --- a/api/api_test.go +++ b/api/api_test.go @@ -85,3 +85,38 @@ func TestNewCustomRule(t *testing.T) { require.Equal(t, cr.Weight, rule.Weight) require.False(t, rule.Disabled) } + +func TestReportOverallScore(t *testing.T) { + t.Run("Empty", func(t *testing.T) { + report := api.NewReport() + require.Equal(t, 0.0, report.OverallScore()) + }) + + t.Run("SameWeight", func(t *testing.T) { + report := api.NewReport() + rule1 := api.Rule{Slug: "test-1", Weight: 1} + rule2 := api.Rule{Slug: "test-2", Weight: 1} + rule3 := api.Rule{Slug: "test-3", Weight: 1} + rule4 := api.Rule{Slug: "test-4", Weight: 1} + + report.Scores[rule1] = 100 + report.Scores[rule2] = 30 + report.Scores[rule3] = 70 + report.Scores[rule4] = 40 + require.Equal(t, 60.0, report.OverallScore()) + }) + + t.Run("Weighted", func(t *testing.T) { + report := api.NewReport() + rule1 := api.Rule{Slug: "test-1", Weight: 1} + rule2 := api.Rule{Slug: "test-2", Weight: 2} + rule3 := api.Rule{Slug: "test-3", Weight: 3} + rule4 := api.Rule{Slug: "test-4", Weight: 4} + + report.Scores[rule1] = 100 + report.Scores[rule2] = 50 + report.Scores[rule3] = 70 + report.Scores[rule4] = 100 + require.Equal(t, 81.0, report.OverallScore()) + }) +} diff --git a/api/report.go b/api/report.go index 2f879a7..00f3fa1 100644 --- a/api/report.go +++ b/api/report.go @@ -15,6 +15,20 @@ type Report struct { Details map[Rule]string } +// OverallScore returns the weighted average of the scores of each rule, weighted with each rule's respective weight. +func (r Report) OverallScore() float64 { + sumScores, sumWeights := 0.0, 0.0 + for rule, score := range r.Scores { + sumScores += rule.Weight * score + sumWeights += rule.Weight + } + + if sumWeights == 0 { + return 0 + } + return sumScores / sumWeights +} + func NewReport() Report { return Report{ Scores: map[Rule]float64{}, diff --git a/utils/markdown/project.go b/utils/markdown/project.go index 765ac89..aa6b4a4 100644 --- a/utils/markdown/project.go +++ b/utils/markdown/project.go @@ -43,13 +43,13 @@ func writeProjectHeader(output *strings.Builder, project api.ProjectReport) { if project.Git.RemoteURL != "" { output.WriteString("Git: Remote URL | `" + project.Git.RemoteURL + "`\n") - output.WriteString("Git: Commit | `" + project.Git.Commit + "`\n") - output.WriteString("Git: Branch | `" + project.Git.Branch + "`\n") - output.WriteString("Git: Dirty Workspace? | " + humanizeBool(project.Git.Dirty) + "\n") + output.WriteString("Git: Commit | `" + project.Git.Commit + "`\n") + output.WriteString("Git: Branch | `" + project.Git.Branch + "`\n") + output.WriteString("Git: Dirty Workspace? | " + humanizeBool(project.Git.Dirty) + "\n") } output.WriteString(fmt.Sprintf("Number of Python files | %d\n", len(project.PythonFiles))) - output.WriteString(fmt.Sprintf("Lines of Python code | %d\n", project.PythonFiles.CountLoC())) + output.WriteString(fmt.Sprintf("Lines of Python code | %d\n", project.PythonFiles.CountLoC())) output.WriteString("\n---\n\n") } @@ -85,7 +85,9 @@ func writeProjectReports(output *strings.Builder, reports map[api.Category]api.R } func writeCategoryReport(output *strings.Builder, category api.Category, linter api.Linter, report api.Report) { - output.WriteString(fmt.Sprintln("###", category.Name, "(`"+category.Slug+"`)")) + overallScore := report.OverallScore() + + output.WriteString(fmt.Sprintf("### %s (`%s`) — **%.1f**%%\n", category.Name, category.Slug, overallScore)) output.WriteString("\n") output.WriteString("Passed | Score | Weight | Rule | Slug\n") output.WriteString(":-----:|------:|-------:|------|-----\n") @@ -105,6 +107,9 @@ func writeCategoryReport(output *strings.Builder, category api.Category, linter } } + output.WriteString(" | _Total_ | | | \n") + output.WriteString(fmt.Sprintf("%s | **%.1f**%% | | %s | `%s`\n", getPassedEmoji(overallScore), overallScore, category.Name, category.Slug)) + output.WriteString("\n") output.WriteString(details.String()) }