#41 use weighted average to calculate overall grade

This commit is contained in:
Shawn Smith
2015-10-14 15:46:18 +09:00
parent 94a36ba160
commit 9a8266c240
7 changed files with 35 additions and 6 deletions

View File

@@ -5,6 +5,7 @@ package check
type Check interface {
Name() string
Description() string
Weight() float64
// Percentage returns the passing percentage of the check,
// as well as a map of filename to output
Percentage() (float64, []FileSummary, error)

View File

@@ -11,6 +11,11 @@ func (g GoVet) Name() string {
return "go_vet"
}
// Weight returns the weight this check has in the overall average
func (g GoVet) Weight() float64 {
return .225
}
// Percentage returns the percentage of .go files that pass go vet
func (g GoVet) Percentage() (float64, []FileSummary, error) {
return GoTool(g.Dir, g.Filenames, []string{"go", "tool", "vet"})

View File

@@ -11,6 +11,11 @@ func (g GoCyclo) Name() string {
return "gocyclo"
}
// Weight returns the weight this check has in the overall average
func (g GoCyclo) Weight() float64 {
return .225
}
// Percentage returns the percentage of .go files that pass gofmt
func (g GoCyclo) Percentage() (float64, []FileSummary, error) {
return GoTool(g.Dir, g.Filenames, []string{"gocyclo", "-over", "15"})

View File

@@ -11,6 +11,11 @@ func (g GoFmt) Name() string {
return "gofmt"
}
// Weight returns the weight this check has in the overall average
func (g GoFmt) Weight() float64 {
return .225
}
// Percentage returns the percentage of .go files that pass gofmt
func (g GoFmt) Percentage() (float64, []FileSummary, error) {
return GoTool(g.Dir, g.Filenames, []string{"gofmt", "-s", "-l"})

View File

@@ -11,6 +11,11 @@ func (g GoLint) Name() string {
return "golint"
}
// Weight returns the weight this check has in the overall average
func (g GoLint) Weight() float64 {
return .225
}
// Percentage returns the percentage of .go files that pass golint
func (g GoLint) Percentage() (float64, []FileSummary, error) {
return GoTool(g.Dir, g.Filenames, []string{"golint"})

View File

@@ -16,7 +16,12 @@ func (g License) Name() string {
return "license"
}
// Percentage returns 0 if no LICENSE, 100 if LICENSE
// Weight returns the weight this check has in the overall average
func (g License) Weight() float64 {
return .1
}
// Percentage returns 0 if no LICENSE, 1 if LICENSE
func (g License) Percentage() (float64, []FileSummary, error) {
cmd := exec.Command("find", g.Dir, "-maxdepth", "1", "-type", "f", "-name", "LICENSE")
var out bytes.Buffer
@@ -29,7 +34,7 @@ func (g License) Percentage() (float64, []FileSummary, error) {
return 0.0, []FileSummary{{"", "http://choosealicense.com/", []Error{}}}, nil
}
return 100.0, []FileSummary{}, nil
return 1.0, []FileSummary{}, nil
}
// Description returns the description of License

View File

@@ -42,6 +42,7 @@ type score struct {
Name string `json:"name"`
Description string `json:"description"`
FileSummaries []check.FileSummary `json:"file_summaries"`
Weight float64 `json:"weight"`
Percentage float64 `json:"percentage"`
}
@@ -128,7 +129,8 @@ func newChecksResp(repo string, forceRefresh bool) (checksResp, error) {
if len(filenames) == 0 {
return checksResp{}, fmt.Errorf("No .go files found")
}
checks := []check.Check{check.GoFmt{Dir: dir, Filenames: filenames},
checks := []check.Check{
check.GoFmt{Dir: dir, Filenames: filenames},
check.GoVet{Dir: dir, Filenames: filenames},
check.GoLint{Dir: dir, Filenames: filenames},
check.GoCyclo{Dir: dir, Filenames: filenames},
@@ -146,6 +148,7 @@ func newChecksResp(repo string, forceRefresh bool) (checksResp, error) {
Name: c.Name(),
Description: c.Description(),
FileSummaries: summaries,
Weight: c.Weight(),
Percentage: p,
}
ch <- s
@@ -160,15 +163,15 @@ func newChecksResp(repo string, forceRefresh bool) (checksResp, error) {
for i := 0; i < len(checks); i++ {
s := <-ch
resp.Checks = append(resp.Checks, s)
total += s.Percentage
total += s.Percentage * s.Weight
for _, fs := range s.FileSummaries {
issues[fs.Filename] = true
}
}
resp.Average = total / float64(len(checks))
resp.Average = total
resp.Issues = len(issues)
resp.Grade = grade(resp.Average * 100)
resp.Grade = grade(total * 100)
return resp, nil
}