Browse Source

Merge branch 'repo-square' of openi.pcl.ac.cn:OpenI/aiforge into repo-square

tags/v1.22.12.1^2
chenshihai 3 years ago
parent
commit
475c7d2456
6 changed files with 84 additions and 15 deletions
  1. +9
    -1
      models/repo.go
  2. +14
    -0
      models/topic.go
  3. +9
    -0
      modules/redis/redis_key/repo_redis_key.go
  4. +14
    -2
      routers/api/v1/repo/topic.go
  5. +2
    -9
      routers/repo/repo_statistic.go
  6. +36
    -3
      services/repository/contributor.go

+ 9
- 1
models/repo.go View File

@@ -296,7 +296,6 @@ func (repo *Repository) ToCardFormat() *Repository4Card {
}

type ContributorInfo struct {
UserInfo *User // nil for contributor who is not a registered user
RelAvatarLink string
UserName string
Email string
@@ -2973,3 +2972,12 @@ func OperateRepoModelNum(repoId int64, amount int64, engines ...*xorm.Engine) er
func OperateRepoAITaskNum(repoId int64, amount int64, engines ...*xorm.Engine) error {
return operateRepoCol(repoId, "ai_task_cnt", amount, engines...)
}

func UpdateRepositoryLastFourMonthCommits(repoID int64, amount int64) error {
_, err := x.Exec("update repository set last_four_month_commits = ? where id = ?", amount, repoID)
return err
}
func UpdateRepositoryLastMonthVisits(repoID int64, amount int64) error {
_, err := x.Exec("update repository set last_month_visits = ? where id = ?", amount, repoID)
return err
}

+ 14
- 0
models/topic.go View File

@@ -9,6 +9,7 @@ import (
"regexp"
"strings"
"unicode/utf8"
"xorm.io/xorm"

"code.gitea.io/gitea/modules/timeutil"

@@ -337,3 +338,16 @@ func GetOrgTopics(orgId int64) ([]Topic, error) {

return result, nil
}

func UpdateRepoTopics(repoID int64, topicNames []string, sess ...*xorm.Engine) error {
e := x
if len(sess) > 0 {
e = sess[0]
}
if _, err := e.ID(repoID).Cols("topics").Update(&Repository{
Topics: topicNames,
}); err != nil {
return err
}
return nil
}

+ 9
- 0
modules/redis/redis_key/repo_redis_key.go View File

@@ -0,0 +1,9 @@
package redis_key

import "fmt"

const REPO_PREFIX = "repo"

func RepoTopNContributors(repoId int64, N int) string {
return KeyJoin(REPO_PREFIX, fmt.Sprint(repoId), fmt.Sprint(N), "top_n_contributor")
}

+ 14
- 2
routers/api/v1/repo/topic.go View File

@@ -177,13 +177,25 @@ func AddTopic(ctx *context.APIContext) {
return
}

_, err = models.AddTopic(ctx.Repo.Repository.ID, topicName)
topic, err := models.AddTopic(ctx.Repo.Repository.ID, topicName)
if err != nil {
log.Error("AddTopic failed: %v", err)
ctx.InternalServerError(err)
return
}

found := false
topicNames := make([]string, len(topics))
for i, t := range topics {
topicNames[i] = t.Name
if strings.EqualFold(topic.Name, t.Name) {
found = true
break
}
}
if !found && topic.Name != "" {
topicNames = append(topicNames, topic.Name)
}
models.UpdateRepoTopics(ctx.Repo.Repository.ID, topicNames)
ctx.Status(http.StatusNoContent)
}



+ 2
- 9
routers/repo/repo_statistic.go View File

@@ -287,20 +287,13 @@ func RepoStatisticDaily(date string) {
}

func SyncStatDataToRepo(repo *models.Repository) {
changed := false
//Save the visit number of repository in the last month
if lv, err := models.SumLastMonthNumVisits(repo.ID); err == nil {
repo.LastMonthVisits = lv
changed = true
models.UpdateRepositoryLastMonthVisits(repo.ID, lv)
}
//Save the commits number of repository in the last four month
if lc, err := models.SumLastFourMonthNumCommits(repo.ID); err == nil {
repo.LastFourMonthCommits = lc
changed = true
}

if changed {
models.UpdateRepository(repo, false)
models.UpdateRepositoryLastFourMonthCommits(repo.ID, lc)
}
}



+ 36
- 3
services/repository/contributor.go View File

@@ -3,14 +3,47 @@ package repository
import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/redis/redis_client"
"code.gitea.io/gitea/modules/redis/redis_key"
"encoding/json"
"github.com/patrickmn/go-cache"
"time"
)

var repoContributorCache = cache.New(5*time.Minute, 1*time.Minute)

type ContributorCacheVal struct {
Contributors []*models.ContributorInfo
Total int
}

func GetRepoTopNContributors(repo *models.Repository, N int) ([]*models.ContributorInfo, int) {
var contributorInfos []*models.ContributorInfo
val, _ := redis_client.Get(redis_key.RepoTopNContributors(repo.ID, N))
if val != "" {
log.Debug("Get RepoTopNContributors from redis,repo.ID = %d value = %v", repo.ID, val)
temp := &ContributorCacheVal{}
json.Unmarshal([]byte(val), temp)
return temp.Contributors, temp.Total
}

contributorInfos, total := getRepoTopNContributorsFromDisk(repo, N)
log.Debug("Get RepoTopNContributors from disk,repo.ID = %d ", repo.ID)
jsonVal, err := json.Marshal(&ContributorCacheVal{Contributors: contributorInfos, Total: total})
if err == nil {
redis_client.Setex(redis_key.RepoTopNContributors(repo.ID, N), string(jsonVal), 2*time.Minute)
}
return contributorInfos, total
}

func getRepoTopNContributorsFromDisk(repo *models.Repository, N int) ([]*models.ContributorInfo, int) {
contributorInfos := make([]*models.ContributorInfo, 0)

branchName := GetDefaultBranchName(repo)
if branchName == "" {
return contributorInfos, 0
}

contributors, err := git.GetContributors(repo.RepoPath(), branchName)
if err == nil && contributors != nil {
contributorInfoHash := make(map[string]*models.ContributorInfo)
@@ -31,7 +64,7 @@ func GetRepoTopNContributors(repo *models.Repository, N int) ([]*models.Contribu
} else {
// new committer info
var newContributor = &models.ContributorInfo{
user, user.RelAvatarLink(), user.Name, user.Email, c.CommitCnt,
user.RelAvatarLink(), user.Name, user.Email, c.CommitCnt,
}
contributorInfos = append(contributorInfos, newContributor)
contributorInfoHash[user.Email] = newContributor
@@ -43,7 +76,7 @@ func GetRepoTopNContributors(repo *models.Repository, N int) ([]*models.Contribu
existedContributorInfo.CommitCnt += c.CommitCnt
} else {
var newContributor = &models.ContributorInfo{
user, "", "", c.Email, c.CommitCnt,
"", "", c.Email, c.CommitCnt,
}
contributorInfos = append(contributorInfos, newContributor)
contributorInfoHash[c.Email] = newContributor


Loading…
Cancel
Save