Skip to content

Commit

Permalink
Add top author stats to activity page (#9615)
Browse files Browse the repository at this point in the history
  • Loading branch information
lafriks committed Jan 20, 2020
1 parent 7d7ab1e commit 81cfe24
Show file tree
Hide file tree
Showing 13 changed files with 578 additions and 960 deletions.
22 changes: 16 additions & 6 deletions models/repo_activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type ActivityAuthorData struct {
Name string `json:"name"`
Login string `json:"login"`
AvatarLink string `json:"avatar_link"`
HomeLink string `json:"home_link"`
Commits int64 `json:"commits"`
}

Expand Down Expand Up @@ -91,12 +92,20 @@ func GetActivityStatsTopAuthors(repo *Repository, timeFrom time.Time, count int)
return nil, nil
}
users := make(map[int64]*ActivityAuthorData)
for k, v := range code.Authors {
if len(k) == 0 {
var unknownUserID int64
unknownUserAvatarLink := NewGhostUser().AvatarLink()
for _, v := range code.Authors {
if len(v.Email) == 0 {
continue
}
u, err := GetUserByEmail(k)
u, err := GetUserByEmail(v.Email)
if u == nil || IsErrUserNotExist(err) {
unknownUserID--
users[unknownUserID] = &ActivityAuthorData{
Name: v.Name,
AvatarLink: unknownUserAvatarLink,
Commits: v.Commits,
}
continue
}
if err != nil {
Expand All @@ -107,10 +116,11 @@ func GetActivityStatsTopAuthors(repo *Repository, timeFrom time.Time, count int)
Name: u.DisplayName(),
Login: u.LowerName,
AvatarLink: u.AvatarLink(),
Commits: v,
HomeLink: u.HomeLink(),
Commits: v.Commits,
}
} else {
user.Commits += v
user.Commits += v.Commits
}
}
v := make([]*ActivityAuthorData, 0)
Expand All @@ -119,7 +129,7 @@ func GetActivityStatsTopAuthors(repo *Repository, timeFrom time.Time, count int)
}

sort.Slice(v, func(i, j int) bool {
return v[i].Commits < v[j].Commits
return v[i].Commits > v[j].Commits
})

cnt := count
Expand Down
36 changes: 31 additions & 5 deletions modules/git/repo_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"bufio"
"bytes"
"fmt"
"sort"
"strconv"
"strings"
"time"
Expand All @@ -21,7 +22,14 @@ type CodeActivityStats struct {
Additions int64
Deletions int64
CommitCountInAllBranches int64
Authors map[string]int64
Authors []*CodeActivityAuthor
}

// CodeActivityAuthor represents git statistics data for commit authors
type CodeActivityAuthor struct {
Name string
Email string
Commits int64
}

// GetCodeActivityStats returns code statistics for acitivity page
Expand Down Expand Up @@ -58,8 +66,9 @@ func (repo *Repository) GetCodeActivityStats(fromTime time.Time, branch string)
stats.CommitCount = 0
stats.Additions = 0
stats.Deletions = 0
authors := make(map[string]int64)
authors := make(map[string]*CodeActivityAuthor)
files := make(map[string]bool)
var author string
p := 0
for scanner.Scan() {
l := strings.TrimSpace(scanner.Text())
Expand All @@ -78,10 +87,17 @@ func (repo *Repository) GetCodeActivityStats(fromTime time.Time, branch string)
case 2: // Commit sha-1
stats.CommitCount++
case 3: // Author
author = l
case 4: // E-mail
email := strings.ToLower(l)
i := authors[email]
authors[email] = i + 1
if _, ok := authors[email]; !ok {
authors[email] = &CodeActivityAuthor{
Name: author,
Email: email,
Commits: 0,
}
}
authors[email].Commits++
default: // Changed file
if parts := strings.Fields(l); len(parts) >= 3 {
if parts[0] != "-" {
Expand All @@ -100,9 +116,19 @@ func (repo *Repository) GetCodeActivityStats(fromTime time.Time, branch string)
}
}
}

a := make([]*CodeActivityAuthor, 0, len(authors))
for _, v := range authors {
a = append(a, v)
}
// Sort authors descending depending on commit count
sort.Slice(a, func(i, j int) bool {
return a[i].Commits > a[j].Commits
})

stats.AuthorCount = int64(len(authors))
stats.ChangedFiles = int64(len(files))
stats.Authors = authors
stats.Authors = a

return stats, nil
}
6 changes: 3 additions & 3 deletions modules/git/repo_stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestRepository_GetCodeActivityStats(t *testing.T) {
assert.EqualValues(t, 10, code.Additions)
assert.EqualValues(t, 1, code.Deletions)
assert.Len(t, code.Authors, 3)
assert.Contains(t, code.Authors, "[email protected]")
assert.EqualValues(t, 3, code.Authors["[email protected]"])
assert.EqualValues(t, 5, code.Authors[""])
assert.EqualValues(t, "[email protected]", code.Authors[1].Email)
assert.EqualValues(t, 3, code.Authors[1].Commits)
assert.EqualValues(t, 5, code.Authors[0].Commits)
}
7 changes: 7 additions & 0 deletions modules/templates/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,13 @@ func NewFuncMap() []template.FuncMap {
}
return path
},
"Json": func(in interface{}) string {
out, err := json.Marshal(in)
if err != nil {
return ""
}
return string(out)
},
"JsonPrettyPrint": func(in string) string {
var out bytes.Buffer
err := json.Indent(&out, []byte(in), "", " ")
Expand Down
Loading

0 comments on commit 81cfe24

Please sign in to comment.