-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatistics.go
More file actions
129 lines (114 loc) · 3.93 KB
/
statistics.go
File metadata and controls
129 lines (114 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package cmd
import (
"fmt"
"sort"
"github.com/google/go-github/github"
)
// Statistics type which holds all the business logic
type Statistics struct {
weeks int
sort string
commitActivities []map[string]int
}
// SortedCommit type has key - day, value - no of commits.
// Use to store the commits based on no of commits order.
type SortedCommit struct {
Key string
Value int
}
// NewStatistics function instantiates new Statistics based on the args
func NewStatistics(repoPath string, weeks int, sort string) (*Statistics, error) {
githubAPI := NewGithubAPI(repoPath)
rawCommitActivities, err := githubAPI.FetchCommits()
if err != nil {
return nil, err
}
lastCommitActivities := FilterLastNRecords(rawCommitActivities, weeks)
commitActivities := make([]map[string]int, len(lastCommitActivities))
for i, val := range lastCommitActivities {
commitActivities[i] = ParseCommitActivity(val)
}
return &Statistics{
weeks: weeks,
commitActivities: commitActivities,
sort: sort,
}, nil
}
var daysOfWeek = []string {"sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"}
// FilterLastNRecords - Used to filter the records for the last n weeks given
func FilterLastNRecords(commits []*github.WeeklyCommitActivity, weeks int) []*github.WeeklyCommitActivity {
var lastCommitActivities []*github.WeeklyCommitActivity
maxValue := len(commits)
if weeks < maxValue {
fromWeeks := maxValue - weeks
lastCommitActivities = commits[fromWeeks:maxValue]
} else {
lastCommitActivities = commits
}
return lastCommitActivities
}
// ActiveDayInRepo - Returns the most no of commits made for the day of week
func (s *Statistics) ActiveDayInRepo() string {
aggregatedCommitActivities := AggregateCommitActivities(s.commitActivities, s.weeks)
maxCommitDay, maxCommit := FindMostCommitsDay(aggregatedCommitActivities)
return fmt.Sprintf("%s %d", maxCommitDay, maxCommit)
}
// AggregateCommitActivities - Returns all the commit aggregated for each day
func AggregateCommitActivities(commits []map[string]int, totalWeeks int) map[string]int {
aggregatedCommits := make(map[string]int)
for _, commit := range commits {
for _, day := range daysOfWeek {
aggregatedCommits[day] += commit[day]
}
}
for day, count := range aggregatedCommits {
aggregatedCommits[day] = count / totalWeeks
}
return aggregatedCommits
}
// FindMostCommitsDay - Returns the most no of commits along with the day of the week
func FindMostCommitsDay(commits map[string]int) (string, int) {
var maxDay string
maxCommits := 0
for day, noOfCommits := range commits {
if maxCommits < noOfCommits {
maxCommits = noOfCommits
maxDay = day
}
}
return maxDay, maxCommits
}
// ParseCommitActivity - Converts object WeeklyCommitActivity into a map of { day => no of commits }
func ParseCommitActivity(ca *github.WeeklyCommitActivity) map[string]int {
commitActivity := make(map[string]int)
commitsPerDay := ca.Days
for i, day := range daysOfWeek {
commitActivity[day] = commitsPerDay[i]
}
return commitActivity
}
// AverageCommitPerDay - Returns the average no of commits made for each day of the week
func (s *Statistics) AverageCommitPerDay() {
commits := AggregateCommitActivities(s.commitActivities, s.weeks)
sortedCommits := SortCommitsPerDay(commits, s.sort)
for _, commit := range sortedCommits {
fmt.Printf("%s has avg of %d commits\n", commit.Key, commit.Value)
}
}
// SortCommitsPerDay - Sorts the commits based on the no of commits either in ascending or descending order
func SortCommitsPerDay(commits map[string]int, sortBy string) []SortedCommit {
var sortedCommits []SortedCommit
for k, v := range commits {
sortedCommits = append(sortedCommits, SortedCommit{k, v})
}
if sortBy == "asc" {
sort.Slice(sortedCommits, func(i, j int) bool {
return sortedCommits[i].Value < sortedCommits[j].Value
})
} else {
sort.Slice(sortedCommits, func(i, j int) bool {
return sortedCommits[i].Value > sortedCommits[j].Value
})
}
return sortedCommits
}