|
- package models
-
- import (
- "fmt"
- "strings"
- "time"
-
- "code.gitea.io/gitea/modules/git"
- )
-
- func GetRepoKPIStats(repo *Repository) (*git.RepoKPIStats, error) {
- wikiPath := ""
- if repo.HasWiki() {
- wikiPath = repo.WikiPath()
- }
- return getRepoKPIStats(repo.RepoPath(), wikiPath)
- }
-
- func getRepoKPIStats(repoPath string, wikiPath string) (*git.RepoKPIStats, error) {
- stats := &git.RepoKPIStats{}
-
- contributors, err := git.GetContributors(repoPath)
- if err != nil {
- return nil, err
- }
- timeUntil := time.Now()
- fourMonthAgo := timeUntil.AddDate(0, -4, 0)
- recentlyContributors, err := git.GetContributorsDetail(repoPath, fourMonthAgo)
- newContributersDict := make(map[string]struct{})
- if err != nil {
- return nil, err
- }
-
- if contributors != nil {
- contributorDistinctDict := make(map[string]int, 0)
- keyContributorsDict := make(map[string]struct{}, 0)
-
- for _, contributor := range contributors {
- if strings.Compare(contributor.Email, "") == 0 {
- continue
- }
-
- user, err := GetUserByActivateEmail(contributor.Email)
- if err == nil {
- value, ok := contributorDistinctDict[user.Email]
- if !ok {
- contributorDistinctDict[user.Email] = contributor.CommitCnt
- } else {
- contributorDistinctDict[user.Email] = value + contributor.CommitCnt
- }
- setKeyContributerDict(contributorDistinctDict, user.Email, keyContributorsDict)
-
- } else {
- value, ok := contributorDistinctDict[contributor.Email]
- if !ok {
- contributorDistinctDict[contributor.Email] = contributor.CommitCnt
- } else {
- contributorDistinctDict[contributor.Email] = value + contributor.CommitCnt
- }
- setKeyContributerDict(contributorDistinctDict, contributor.Email, keyContributorsDict)
- }
-
- }
-
- if recentlyContributors != nil {
- for _, recentlyContributor := range recentlyContributors {
-
- user, err := GetUserByActivateEmail(recentlyContributor.Email)
- var ok bool
- if err == nil {
- _, ok = contributorDistinctDict[user.Email]
- } else {
- _, ok = contributorDistinctDict[recentlyContributor.Email]
- }
-
- if !ok {
- stats.ContributorsAdded++
- newContributersDict[recentlyContributor.Email] = struct{}{}
- }
-
- }
- }
-
- stats.Contributors = int64(len(contributorDistinctDict))
- stats.KeyContributors = int64(len(keyContributorsDict))
-
- }
-
- err = git.SetDevelopAge(repoPath, stats)
- if err != nil {
- return nil, fmt.Errorf("FillFromGit: %v", err)
- }
- err = git.SetRepoKPIStats(repoPath, fourMonthAgo, stats, newContributersDict)
-
- if err != nil {
- return nil, fmt.Errorf("FillFromGit: %v", err)
- }
-
- git.SetWikiPages(wikiPath, stats)
- return stats, nil
-
- }
-
- func setKeyContributerDict(contributorDistinctDict map[string]int, email string, keyContributorsDict map[string]struct{}) {
- if contributorDistinctDict[email] >= 3 {
- _, ok := keyContributorsDict[email]
- if !ok {
- keyContributorsDict[email] = struct{}{}
-
- }
-
- }
- }
-
- func GetAllUserKPIStats() (map[string]*git.UserKPIStats, error) {
- authors := make(map[string]*git.UserKPIStats)
- repositorys, err := GetAllRepositoriesByFilterCols("owner_name", "name")
- if err != nil {
- return nil, err
- }
-
- for _, repository := range repositorys {
- authorsOneRepo, err1 := git.GetUserKPIStats(repository.RepoPath())
- if err1 != nil {
- return nil, err
- }
-
- for key, value := range authorsOneRepo {
- if _, ok := authors[key]; !ok {
- authors[key] = &git.UserKPIStats{
-
- Name: value.Name,
- Email: value.Email,
- Commits: 0,
- CommitLines: 0,
- }
- }
- authors[key].Commits += value.Commits
- authors[key].CommitLines += value.CommitLines
-
- }
-
- }
- return authors, nil
- }
|