Skip to content

Commit

Permalink
Print a score for the entire category in the Markdown report, calcula…
Browse files Browse the repository at this point in the history
…ted as weighted average of all rules' scores
  • Loading branch information
bvobart committed Aug 3, 2021
1 parent 15623de commit 72969ef
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 5 deletions.
35 changes: 35 additions & 0 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})
}
14 changes: 14 additions & 0 deletions api/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{},
Expand Down
15 changes: 10 additions & 5 deletions utils/markdown/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down Expand Up @@ -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")
Expand All @@ -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())
}
Expand Down

0 comments on commit 72969ef

Please sign in to comment.