Skip to content

Commit

Permalink
Changed formats
Browse files Browse the repository at this point in the history
  • Loading branch information
liiri committed Sep 26, 2021
1 parent aa1be8b commit afe0d5c
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 61 deletions.
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ OPTIONS:

## Output

Per supported [programming language](#languages), the tool will plot the number of source files, and following metrics, in both "total" and "average" sections:
Per supported [programming language](#languages), the tool will plot the number of source files, and following metrics, in both `total` and `average` sections:

* Lines of Code (`lines_of_code`) - Number of lines that don't contain whitespace or comments.
* Keywords Complexity (`keywords_complexity`) - Number of keywords per line of code. Keyword is a rough estimation of control statements that are defined per language, see [languageToKeywords](calculate/keywords.go).
Expand All @@ -38,21 +38,21 @@ Output example:

```json
{
"number_of_files": 13,
"number_of_files": 9,
"counters_by_language": {
"go": {
"lines_of_code": 5091,
"keywords_complexity": 2.947819616200504,
"indentations_complexity": 31.149417838304803,
"indentations_diff_complexity": 4.619751490661065
}
},
"averages_by_language": {
"go": {
"lines_of_code": 391.61538461538464,
"keywords_complexity": 0.22675535509234646,
"indentations_complexity": 2.396109064484985,
"indentations_diff_complexity": 0.35536549928162037
"total": {
"lines_of_code": 2375,
"keywords_complexity": 2.039535414531353,
"indentations_complexity": 11.931293051842783,
"indentations_diff_complexity": 1.9044440275914503
},
"average": {
"lines_of_code": 263.8888888888889,
"keywords_complexity": 0.22661504605903923,
"indentations_complexity": 1.3256992279825315,
"indentations_diff_complexity": 0.21160489195460558
}
}
}
}
Expand Down
22 changes: 12 additions & 10 deletions calculate/calculate.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ type context struct {
func newContext() *context {
return &context{
CodeSummary: CodeSummary{
CountersByLanguage: make(map[Language]*CodeCounters),
AveragesByLanguage: make(map[Language]*CodeCounters),
CountersByLanguage: make(map[Language]*SummaryCounters),
},
}
}
Expand Down Expand Up @@ -55,8 +54,8 @@ func Complexity(opts *options.Options) (*CodeSummary, error) {
return nil, fmt.Errorf("failed to walk files under '%v': %v", opts.CodePath, err)
}

for language, counters := range ctx.CountersByLanguage {
ctx.AveragesByLanguage[language] = counters.average(ctx.NumberOfFiles)
for _, counters := range ctx.CountersByLanguage {
counters.Average = counters.Total.average(ctx.NumberOfFiles)
}

return &ctx.CodeSummary, nil
Expand Down Expand Up @@ -96,18 +95,21 @@ func (ctx *context) visitPath(rootPath string, path string, info fs.FileInfo) er
return nil
}

counters, err := ctx.getCountersForPath(path, language)
fileCounters, err := ctx.getCountersForPath(path, language)
if err != nil {
return fmt.Errorf("failed to count at %v: %v", path, err)
}
ctx.verboseLog("+++ '%v': %v", path, counters)
ctx.verboseLog("+++ '%v': %v", path, fileCounters)

totalCounters, found := ctx.CountersByLanguage[language]
summaryCounters, found := ctx.CountersByLanguage[language]
if !found {
totalCounters = &CodeCounters{}
ctx.CountersByLanguage[language] = totalCounters
summaryCounters = &SummaryCounters{
Total: &CodeCounters{},
Average: &CodeCounters{},
}
ctx.CountersByLanguage[language] = summaryCounters
}
totalCounters.inc(counters)
summaryCounters.Total.inc(fileCounters)
ctx.NumberOfFiles++

return nil
Expand Down
55 changes: 31 additions & 24 deletions calculate/calculate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,12 @@ func TestEncodings(t *testing.T) {
if err != nil {
panic(err)
}
defer os.RemoveAll(basePath)
defer func() {
err := os.RemoveAll(basePath)
if err != nil {
panic(err)
}
}()

err = copy.Copy(sourcePath, basePath)
if err != nil {
Expand All @@ -150,7 +155,7 @@ func TestEncodings(t *testing.T) {
summary, err := Complexity(opts)
r.Nil(err)
r.Equal(float64(3), summary.NumberOfFiles)
r.Equal(float64(5*3), summary.CountersByLanguage["go"].LinesOfCode)
r.Equal(float64(5*3), summary.CountersByLanguage["go"].Total.LinesOfCode)
}

func inRange(r *assert.Assertions, value float64, min int, max int) {
Expand Down Expand Up @@ -183,28 +188,30 @@ func TestDogFood(t *testing.T) {
r.Equal(float64(9), summary.NumberOfFiles)

r.Len(summary.CountersByLanguage, 1)
inRange(r, summary.CountersByLanguage["go"].Lines, 2000, 4000)
inRange(r, summary.CountersByLanguage["go"].LinesOfCode, 2000, 4000)
inRange(r, summary.CountersByLanguage["go"].Keywords, 200, 400)
inRange(r, summary.CountersByLanguage["go"].Indentations, 2500, 3500)
inRange(r, summary.CountersByLanguage["go"].IndentationsNormalized, 2500, 3500)
inRange(r, summary.CountersByLanguage["go"].IndentationsDiff, 400, 600)
inRange(r, summary.CountersByLanguage["go"].IndentationsDiffNormalized, 400, 600)
inRange(r, summary.CountersByLanguage["go"].IndentationsComplexity, 10, 12)
inRange(r, summary.CountersByLanguage["go"].IndentationsDiffComplexity*100, 150, 250)
inRange(r, summary.CountersByLanguage["go"].KeywordsComplexity*100, 200, 250)

r.Len(summary.AveragesByLanguage, 1)
inRange(r, summary.AveragesByLanguage["go"].Lines, 300, 400)
inRange(r, summary.AveragesByLanguage["go"].LinesOfCode, 250, 300)
inRange(r, summary.AveragesByLanguage["go"].Keywords, 25, 35)
inRange(r, summary.AveragesByLanguage["go"].Indentations, 300, 350)
inRange(r, summary.AveragesByLanguage["go"].IndentationsNormalized, 300, 350)
inRange(r, summary.AveragesByLanguage["go"].IndentationsDiff, 50, 60)
inRange(r, summary.AveragesByLanguage["go"].IndentationsDiffNormalized, 50, 60)
inRange(r, summary.AveragesByLanguage["go"].IndentationsComplexity, 1, 2)
inRange(r, summary.AveragesByLanguage["go"].IndentationsDiffComplexity*100, 20, 30)
inRange(r, summary.AveragesByLanguage["go"].KeywordsComplexity*100, 20, 30)

total := summary.CountersByLanguage["go"].Total
inRange(r, total.Lines, 2000, 4000)
inRange(r, total.LinesOfCode, 2000, 4000)
inRange(r, total.Keywords, 200, 400)
inRange(r, total.Indentations, 2500, 3500)
inRange(r, total.IndentationsNormalized, 2500, 3500)
inRange(r, total.IndentationsDiff, 400, 600)
inRange(r, total.IndentationsDiffNormalized, 400, 600)
inRange(r, total.IndentationsComplexity, 10, 12)
inRange(r, total.IndentationsDiffComplexity*100, 150, 250)
inRange(r, total.KeywordsComplexity*100, 200, 250)

average := summary.CountersByLanguage["go"].Average
inRange(r, average.Lines, 300, 400)
inRange(r, average.LinesOfCode, 250, 300)
inRange(r, average.Keywords, 25, 35)
inRange(r, average.Indentations, 300, 350)
inRange(r, average.IndentationsNormalized, 300, 350)
inRange(r, average.IndentationsDiff, 50, 60)
inRange(r, average.IndentationsDiffNormalized, 50, 60)
inRange(r, average.IndentationsComplexity, 1, 2)
inRange(r, average.IndentationsDiffComplexity*100, 20, 30)
inRange(r, average.KeywordsComplexity*100, 20, 30)
}

func getCountersForCode(code string, language Language) (*CodeCounters, error) {
Expand Down
10 changes: 7 additions & 3 deletions calculate/counters.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ package calculate
import "fmt"

type CodeSummary struct {
NumberOfFiles float64 `json:"number_of_files"`
CountersByLanguage map[Language]*CodeCounters `json:"counters_by_language"`
AveragesByLanguage map[Language]*CodeCounters `json:"averages_by_language"`
NumberOfFiles float64 `json:"number_of_files"`
CountersByLanguage map[Language]*SummaryCounters `json:"counters_by_language"`
}

type SummaryCounters struct {
Total *CodeCounters `json:"total"`
Average *CodeCounters `json:"average"`
}

type CodeCounters struct {
Expand Down
18 changes: 9 additions & 9 deletions options/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ type Config struct {
var defaultConfig = &Config{
IncludePatterns: []string{},
ExcludePatterns: []string{
"**/bin/**",
"**/obj/**",
"**/venv/**",
"**/node_modules/**",
"**/.idea/**",
"**/.git/**",
"**/site-packages/**",
"**/vendor/**",
"**/test_resources/**",
"**/bin",
"**/obj",
"**/venv",
"**/node_modules",
"**/.idea",
"**/.git",
"**/site-packages",
"**/vendor",
"**/test_resources",
"**/tests/**",
"**/testing/**",
"**/resources/**",
Expand Down
2 changes: 1 addition & 1 deletion options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func ParseOptions(c *cli.Context) (*Options, error) {
opts.IncludePatterns = append(opts.IncludePatterns, cfg.IncludePatterns...)
}
if len(cfg.ExcludePatterns) > 0 {
opts.IncludePatterns = append(opts.ExcludePatterns, cfg.ExcludePatterns...)
opts.ExcludePatterns = append(opts.ExcludePatterns, cfg.ExcludePatterns...)
}

return opts, nil
Expand Down

0 comments on commit afe0d5c

Please sign in to comment.