refactor: Stats

• Remove unused parameters from functions
• Decompose functions where possible

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2021-12-11 17:25:19 +01:00
parent 0dbfd6e849
commit 98de5e4c17
No known key found for this signature in database
GPG key ID: 332171FADF1DB90B

View file

@ -2,6 +2,7 @@ package cmd
import ( import (
"fmt" "fmt"
"sort"
"strings" "strings"
"code.gitea.io/sdk/gitea" "code.gitea.io/sdk/gitea"
@ -29,37 +30,53 @@ var (
}) })
core.ExitOnError("Couldn't list the pull requests", err) core.ExitOnError("Couldn't list the pull requests", err)
GetStats(client, milestone, prs) GetStats(prs)
}, },
} }
) )
func GetStats(client *gitea.Client, milestone *gitea.Milestone, prs []*gitea.PullRequest) { func ParseHistogram(prs []*gitea.PullRequest) map[string]int {
graded := 0
histogram := make(map[string]int) histogram := make(map[string]int)
for _, pr := range prs { for _, pr := range prs {
for _, label := range pr.Labels { for _, label := range pr.Labels {
if strings.HasPrefix(label.Name, "grade:") { if strings.HasPrefix(label.Name, "grade:") {
grade := strings.Split(label.Name, ":")[1] grade := strings.Split(label.Name, ":")[1]
histogram[grade] += 1 histogram[grade] += 1
graded += 1
break break
} }
} }
} }
for grade, count := range histogram { return histogram
}
func SortedKeys(histogram map[string]int) []string {
grades := make([]string, 0, len(histogram))
for grade := range histogram {
grades = append(grades, grade)
}
sort.Strings(grades)
return grades
}
func GetStats(prs []*gitea.PullRequest) {
histogram := ParseHistogram(prs)
grades := SortedKeys(histogram)
graded := 0
for _, grade := range grades {
count := histogram[grade]
if (count == 0) { if (count == 0) {
continue continue
} }
fmt.Printf("%s: %d\n", grade, count) graded += count
fmt.Printf(" %s: %2d\n", grade, count)
} }
if (len(prs) - graded > 0) { if (len(prs) - graded > 0) {
fmt.Printf("ungraded: %d\n", len(prs) - graded) fmt.Println("--------")
fmt.Printf("TODO: %2d\n", len(prs) - graded)
} }
} }