Added bento :)
This commit is contained in:
+58
-32
@@ -4,8 +4,8 @@ import (
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
"github.com/atridad/gitea-wrapped/pkg/gitea"
|
||||
"github.com/atridad/gitea-wrapped/pkg/models"
|
||||
"github.com/atridad/wrapped-cli/pkg/gitea"
|
||||
"github.com/atridad/wrapped-cli/pkg/models"
|
||||
)
|
||||
|
||||
type Analyzer struct {
|
||||
@@ -28,17 +28,48 @@ func (a *Analyzer) AddCommits(commits []models.Commit) {
|
||||
a.commits = append(a.commits, commits...)
|
||||
}
|
||||
|
||||
func (a *Analyzer) inferPrimaryEmail(username string) string {
|
||||
emailCount := make(map[string]int)
|
||||
|
||||
for _, c := range a.commits {
|
||||
if c.AuthorEmail != "" {
|
||||
emailCount[c.AuthorEmail]++
|
||||
}
|
||||
}
|
||||
|
||||
var bestEmail string
|
||||
max := 0
|
||||
|
||||
for email, count := range emailCount {
|
||||
if count > max {
|
||||
max = count
|
||||
bestEmail = email
|
||||
}
|
||||
}
|
||||
|
||||
return bestEmail
|
||||
}
|
||||
|
||||
func (a *Analyzer) Generate(username string) *models.UserStats {
|
||||
primaryEmail := a.inferPrimaryEmail(username)
|
||||
|
||||
var filteredCommits []models.Commit
|
||||
for _, commit := range a.commits {
|
||||
if primaryEmail != "" && commit.AuthorEmail == primaryEmail {
|
||||
filteredCommits = append(filteredCommits, commit)
|
||||
}
|
||||
}
|
||||
|
||||
stats := &models.UserStats{
|
||||
Username: username,
|
||||
TotalCommits: len(a.commits),
|
||||
TotalCommits: len(filteredCommits),
|
||||
TotalRepositories: len(a.repos),
|
||||
}
|
||||
|
||||
stats.Languages = a.generateLanguageStats()
|
||||
stats.CommitsByWeekday = a.generateWeekdayStats()
|
||||
stats.CommitsByMonth = a.generateMonthStats()
|
||||
stats.AverageCommitsPerDay = a.calculateAverageCommitsPerDay()
|
||||
stats.CommitsByWeekday = a.generateWeekdayStats(filteredCommits)
|
||||
stats.CommitsByMonth = a.generateMonthStats(filteredCommits)
|
||||
stats.AverageCommitsPerDay = a.calculateAverageCommitsPerDay(filteredCommits)
|
||||
|
||||
if len(stats.CommitsByMonth) > 0 {
|
||||
stats.MostActiveMonth = stats.CommitsByMonth[0].Date.Format("January")
|
||||
@@ -47,12 +78,11 @@ func (a *Analyzer) Generate(username string) *models.UserStats {
|
||||
stats.MostActiveDay = stats.CommitsByWeekday[0].Day
|
||||
}
|
||||
|
||||
stats.TopRepositories = a.getTopRepositories(5)
|
||||
stats.TopRepositories = a.getTopRepositories(filteredCommits, 5)
|
||||
|
||||
return stats
|
||||
}
|
||||
|
||||
// generateLanguageStats creates language statistics
|
||||
func (a *Analyzer) generateLanguageStats() []models.LanguageStats {
|
||||
langCount := make(map[string]int)
|
||||
|
||||
@@ -82,12 +112,11 @@ func (a *Analyzer) generateLanguageStats() []models.LanguageStats {
|
||||
return stats
|
||||
}
|
||||
|
||||
// generateWeekdayStats creates weekday statistics for commits
|
||||
func (a *Analyzer) generateWeekdayStats() []models.DateStats {
|
||||
func (a *Analyzer) generateWeekdayStats(commits []models.Commit) []models.DateStats {
|
||||
dayCount := make(map[time.Weekday]int)
|
||||
dayNames := [7]string{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}
|
||||
|
||||
for _, commit := range a.commits {
|
||||
for _, commit := range commits {
|
||||
day := commit.Timestamp.Weekday()
|
||||
dayCount[day]++
|
||||
}
|
||||
@@ -108,19 +137,17 @@ func (a *Analyzer) generateWeekdayStats() []models.DateStats {
|
||||
return stats
|
||||
}
|
||||
|
||||
// generateMonthStats creates monthly statistics for commits
|
||||
func (a *Analyzer) generateMonthStats() []models.DateStats {
|
||||
func (a *Analyzer) generateMonthStats(commits []models.Commit) []models.DateStats {
|
||||
monthCount := make(map[string]int)
|
||||
monthKeys := []string{}
|
||||
monthMap := make(map[string]time.Time)
|
||||
|
||||
for _, commit := range a.commits {
|
||||
for _, commit := range commits {
|
||||
monthStr := commit.Timestamp.Format("2006-01")
|
||||
monthCount[monthStr]++
|
||||
if _, exists := monthMap[monthStr]; !exists {
|
||||
monthKeys = append(monthKeys, monthStr)
|
||||
monthMap[monthStr] = commit.Timestamp
|
||||
}
|
||||
}
|
||||
|
||||
for k := range monthCount {
|
||||
monthKeys = append(monthKeys, k)
|
||||
}
|
||||
|
||||
var stats []models.DateStats
|
||||
@@ -139,37 +166,35 @@ func (a *Analyzer) generateMonthStats() []models.DateStats {
|
||||
return stats
|
||||
}
|
||||
|
||||
// calculateAverageCommitsPerDay calculates average commits per calendar day
|
||||
func (a *Analyzer) calculateAverageCommitsPerDay() float64 {
|
||||
if len(a.commits) == 0 {
|
||||
func (a *Analyzer) calculateAverageCommitsPerDay(commits []models.Commit) float64 {
|
||||
if len(commits) == 0 {
|
||||
return 0
|
||||
}
|
||||
|
||||
if len(a.commits) < 2 {
|
||||
return float64(len(a.commits))
|
||||
if len(commits) < 2 {
|
||||
return float64(len(commits))
|
||||
}
|
||||
|
||||
sort.Slice(a.commits, func(i, j int) bool {
|
||||
return a.commits[i].Timestamp.Before(a.commits[j].Timestamp)
|
||||
sort.Slice(commits, func(i, j int) bool {
|
||||
return commits[i].Timestamp.Before(commits[j].Timestamp)
|
||||
})
|
||||
|
||||
firstDay := a.commits[0].Timestamp
|
||||
lastDay := a.commits[len(a.commits)-1].Timestamp
|
||||
firstDay := commits[0].Timestamp
|
||||
lastDay := commits[len(commits)-1].Timestamp
|
||||
|
||||
daysDiff := lastDay.Sub(firstDay).Hours() / 24
|
||||
if daysDiff == 0 {
|
||||
daysDiff = 1
|
||||
}
|
||||
|
||||
return float64(len(a.commits)) / daysDiff
|
||||
return float64(len(commits)) / daysDiff
|
||||
}
|
||||
|
||||
// getTopRepositories returns the top N repositories by commit count
|
||||
func (a *Analyzer) getTopRepositories(n int) []models.Repository {
|
||||
func (a *Analyzer) getTopRepositories(commits []models.Commit, n int) []models.Repository {
|
||||
repoCommitCount := make(map[string]int)
|
||||
repoMap := make(map[string]models.Repository)
|
||||
|
||||
for _, commit := range a.commits {
|
||||
for _, commit := range commits {
|
||||
repoCommitCount[commit.RepoName]++
|
||||
}
|
||||
|
||||
@@ -188,6 +213,7 @@ func (a *Analyzer) getTopRepositories(n int) []models.Repository {
|
||||
repo models.Repository
|
||||
count int
|
||||
}
|
||||
|
||||
var repos []repoWithCount
|
||||
for name, count := range repoCommitCount {
|
||||
if repo, exists := repoMap[name]; exists {
|
||||
|
||||
Reference in New Issue
Block a user