Skip to content

Commit

Permalink
first stab at #311
Browse files Browse the repository at this point in the history
  • Loading branch information
boyter committed Jan 4, 2022
1 parent 1cd46df commit 640ce26
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 22 deletions.
24 changes: 12 additions & 12 deletions SCC-OUTPUT-REPORT.html
Expand Up @@ -12,12 +12,12 @@
<tbody><tr>
<th>Go</th>
<th>36</th>
<th>8929</th>
<th>1433</th>
<th>425</th>
<th>7071</th>
<th>1428</th>
<th>352948</th>
<th>9090</th>
<th>1443</th>
<th>429</th>
<th>7218</th>
<th>1481</th>
<th>357991</th>
</tr><tr>
<th>Java</th>
<th>24</th>
Expand Down Expand Up @@ -607,11 +607,11 @@
<tfoot><tr>
<th>Total</th>
<th>176</th>
<th>26779</th>
<th>3031</th>
<th>1761</th>
<th>21987</th>
<th>2414</th>
<th>1810546</th>
<th>26940</th>
<th>3041</th>
<th>1765</th>
<th>22134</th>
<th>2467</th>
<th>1815589</th>
</tr></tfoot>
</table></body></html>
129 changes: 119 additions & 10 deletions processor/formatters.go
Expand Up @@ -12,6 +12,7 @@ import (
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -247,15 +248,7 @@ func toCSVSummary(input chan *FileJob) string {
language := aggregateLanguageSummary(input)
language = sortLanguageSummary(language)

records := [][]string{{
"Language",
"Lines",
"Code",
"Comments",
"Blanks",
"Complexity",
"Bytes"},
}
records := [][]string{}

for _, result := range language {
records = append(records, []string{
Expand All @@ -268,9 +261,72 @@ func toCSVSummary(input chan *FileJob) string {
fmt.Sprint(result.Bytes)})
}

// Cater for the common case of adding plural even for those options that don't make sense
// as its quite common for those who English is not a first language to make a simple mistake
switch {
case SortBy == "name" || SortBy == "names" || SortBy == "language" || SortBy == "languages":
sort.Slice(records, func(i, j int) bool {
return strings.Compare(records[i][0], records[j][0]) < 0
})
case SortBy == "line" || SortBy == "lines":
sort.Slice(records, func(i, j int) bool {
i1, _ := strconv.ParseInt(records[i][1], 10, 64)
i2, _ := strconv.ParseInt(records[j][1], 10, 64)
return i1 > i2
})
case SortBy == "blank" || SortBy == "blanks":
sort.Slice(records, func(i, j int) bool {
i1, _ := strconv.ParseInt(records[i][4], 10, 64)
i2, _ := strconv.ParseInt(records[j][4], 10, 64)
return i1 > i2
})
case SortBy == "code" || SortBy == "codes":
sort.Slice(records, func(i, j int) bool {
i1, _ := strconv.ParseInt(records[i][2], 10, 64)
i2, _ := strconv.ParseInt(records[j][2], 10, 64)
return i1 > i2
})
case SortBy == "comment" || SortBy == "comments":
sort.Slice(records, func(i, j int) bool {
i1, _ := strconv.ParseInt(records[i][3], 10, 64)
i2, _ := strconv.ParseInt(records[j][3], 10, 64)
return i1 > i2
})
case SortBy == "complexity" || SortBy == "complexitys":
sort.Slice(records, func(i, j int) bool {
i1, _ := strconv.ParseInt(records[i][5], 10, 64)
i2, _ := strconv.ParseInt(records[j][5], 10, 64)
return i1 > i2
})
case SortBy == "byte" || SortBy == "bytes":
sort.Slice(records, func(i, j int) bool {
i1, _ := strconv.ParseInt(records[i][6], 10, 64)
i2, _ := strconv.ParseInt(records[j][6], 10, 64)
return i1 > i2
})
default:
sort.Slice(records, func(i, j int) bool {
i1, _ := strconv.ParseInt(records[i][1], 10, 64)
i2, _ := strconv.ParseInt(records[j][1], 10, 64)
return i1 > i2
})
}

recordsEnd := [][]string{{
"Language",
"Lines",
"Code",
"Comments",
"Blanks",
"Complexity",
"Bytes"},
}

recordsEnd = append(recordsEnd, records...)

b := &bytes.Buffer{}
w := csv.NewWriter(b)
_ = w.WriteAll(records)
_ = w.WriteAll(recordsEnd)
w.Flush()

return b.String()
Expand Down Expand Up @@ -302,6 +358,59 @@ func toCSVFiles(input chan *FileJob) string {
fmt.Sprint(result.Bytes)})
}

// Cater for the common case of adding plural even for those options that don't make sense
// as its quite common for those who English is not a first language to make a simple mistake
switch {
case SortBy == "name" || SortBy == "names":
sort.Slice(records, func(i, j int) bool {
return strings.Compare(records[i][2], records[j][2]) < 0
})
case SortBy == "language" || SortBy == "languages":
sort.Slice(records, func(i, j int) bool {
return strings.Compare(records[i][0], records[j][0]) < 0
})
case SortBy == "line" || SortBy == "lines":
sort.Slice(records, func(i, j int) bool {
i1, _ := strconv.ParseInt(records[i][3], 10, 64)
i2, _ := strconv.ParseInt(records[j][3], 10, 64)
return i1 > i2
})
case SortBy == "blank" || SortBy == "blanks":
sort.Slice(records, func(i, j int) bool {
i1, _ := strconv.ParseInt(records[i][6], 10, 64)
i2, _ := strconv.ParseInt(records[j][6], 10, 64)
return i1 > i2
})
case SortBy == "code" || SortBy == "codes":
sort.Slice(records, func(i, j int) bool {
i1, _ := strconv.ParseInt(records[i][4], 10, 64)
i2, _ := strconv.ParseInt(records[j][4], 10, 64)
return i1 > i2
})
case SortBy == "comment" || SortBy == "comments":
sort.Slice(records, func(i, j int) bool {
i1, _ := strconv.ParseInt(records[i][5], 10, 64)
i2, _ := strconv.ParseInt(records[j][5], 10, 64)
return i1 > i2
})
case SortBy == "complexity" || SortBy == "complexitys":
sort.Slice(records, func(i, j int) bool {
i1, _ := strconv.ParseInt(records[i][7], 10, 64)
i2, _ := strconv.ParseInt(records[j][7], 10, 64)
return i1 > i2
})
case SortBy == "byte" || SortBy == "bytes":
sort.Slice(records, func(i, j int) bool {
i1, _ := strconv.ParseInt(records[i][8], 10, 64)
i2, _ := strconv.ParseInt(records[j][8], 10, 64)
return i1 > i2
})
default:
sort.Slice(records, func(i, j int) bool {
return records[i][2] > records[j][2]
})
}

b := &bytes.Buffer{}
w := csv.NewWriter(b)
_ = w.WriteAll(records)
Expand Down
52 changes: 52 additions & 0 deletions processor/formatters_test.go
Expand Up @@ -649,6 +649,58 @@ func TestToCsvStreamMultiple(t *testing.T) {
}
}

func TestToCsvFilesSorted(t *testing.T) {
fj1 := &FileJob{
Language: "Go",
Filename: "bbbb.go",
Extension: "go",
Location: "./",
Bytes: 90,
Lines: 90,
Code: 90,
Comment: 90,
Blank: 90,
Complexity: 90,
WeightedComplexity: 90,
Binary: false,
}
fj2 := &FileJob{
Language: "Go",
Filename: "aaaa.go",
Extension: "go",
Location: "./",
Bytes: 1000,
Lines: 1000,
Code: 1000,
Comment: 1000,
Blank: 1000,
Complexity: 1000,
WeightedComplexity: 1000,
Binary: false,
}

Files = true
SortBy = "lines"

inputChan1 := make(chan *FileJob, 1000)
inputChan1 <- fj1
inputChan1 <- fj2
close(inputChan1)
res1 := toCSV(inputChan1)

inputChan2 := make(chan *FileJob, 1000)
inputChan2 <- fj2
inputChan2 <- fj1
close(inputChan2)
res2 := toCSV(inputChan2)

Files = false

if res1 != res2 {
t.Error("Should be sorted to be the same")
}
}

func TestToOpenMetricsMultiple(t *testing.T) {
inputChan := make(chan *FileJob, 1000)
inputChan <- &FileJob{
Expand Down

0 comments on commit 640ce26

Please sign in to comment.