diff --git a/models/cloudbrain.go b/models/cloudbrain.go index b8384f746..f9c197e00 100755 --- a/models/cloudbrain.go +++ b/models/cloudbrain.go @@ -1861,7 +1861,7 @@ func CreateCloudbrain(cloudbrain *Cloudbrain) (err error) { session.Commit() go IncreaseDatasetUseCount(cloudbrain.Uuid) - go ResetRepoAITaskNum(cloudbrain.RepoID) + go OperateRepoAITaskNum(cloudbrain.RepoID, 1) return nil } @@ -2030,7 +2030,7 @@ func updateAITaskNumWhenDeleteJob(job *Cloudbrain) { } if repoId > 0 { - ResetRepoAITaskNum(repoId) + go OperateRepoAITaskNum(repoId, -1) } } @@ -2159,7 +2159,6 @@ func RestartCloudbrain(old *Cloudbrain, new *Cloudbrain) (err error) { } go IncreaseDatasetUseCount(new.Uuid) - go ResetRepoAITaskNum(new.RepoID) return nil } func CloudbrainAll(opts *CloudbrainsOptions) ([]*CloudbrainInfo, int64, error) { diff --git a/models/repo.go b/models/repo.go index b35b56a6f..a390b74e4 100755 --- a/models/repo.go +++ b/models/repo.go @@ -244,23 +244,25 @@ type Repository struct { // Repository4Card format for front display type Repository4Card struct { - ID int64 - OwnerID int64 - OwnerName string - LowerName string - Name string - NumWatches int - NumStars int - NumForks int - Description string - Topics []string - AiTaskCnt int64 - ModelCnt int64 - DatasetCnt int64 - CreatedUnix timeutil.TimeStamp - UpdatedUnix timeutil.TimeStamp - PrimaryLanguage *LanguageStat - RelAvatarLink string + ID int64 + OwnerID int64 + OwnerName string + LowerName string + Name string + NumWatches int + NumStars int + NumForks int + Description string + Topics []string + AiTaskCnt int64 + ModelCnt int64 + DatasetCnt int64 + CreatedUnix timeutil.TimeStamp + UpdatedUnix timeutil.TimeStamp + PrimaryLanguage *LanguageStat + RelAvatarLink string + Contributors []*ContributorInfo + TotalContributorCount int } type RepositoryShow struct { @@ -293,6 +295,14 @@ func (repo *Repository) ToCardFormat() *Repository4Card { return result } +type ContributorInfo struct { + UserInfo *User // nil for contributor who is not a registered user + RelAvatarLink string + UserName string + Email string + CommitCnt int +} + // SanitizedOriginalURL returns a sanitized OriginalURL func (repo *Repository) SanitizedOriginalURL() string { if repo.OriginalURL == "" { @@ -2930,3 +2940,36 @@ func ResetRepoModelNum(repoId int64) error { _, err = x.Cols("model_cnt").Where("id = ?", repoId).Update(&r) return err } + +func operateRepoCol(repoId int64, colName string, amount int64, engines ...*xorm.Engine) error { + var err error + + if amount == 0 { + return nil + } + var ee *xorm.Engine + if len(engines) == 0 { + ee = x + } else { + ee = engines[0] + } + if amount > 0 { + _, err = ee.Exec(fmt.Sprintf("update repository set %s = %s + ? where id = ?", colName, colName), amount, repoId) + } else { + _, err = ee.Exec(fmt.Sprintf("update repository set %s = %s - ? where id = ?", colName, colName), -1*amount, repoId) + } + + return err +} + +func OperateRepoDatasetNum(repoId int64, amount int64, engines ...*xorm.Engine) error { + return operateRepoCol(repoId, "dataset_cnt", amount, engines...) +} + +func OperateRepoModelNum(repoId int64, amount int64, engines ...*xorm.Engine) error { + return operateRepoCol(repoId, "model_cnt", amount, engines...) +} + +func OperateRepoAITaskNum(repoId int64, amount int64, engines ...*xorm.Engine) error { + return operateRepoCol(repoId, "ai_task_cnt", amount, engines...) +} diff --git a/modules/setting/setting.go b/modules/setting/setting.go index 0d623eb3b..ee6d0a13d 100755 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -666,7 +666,6 @@ var ( //repo square config IncubationSourceOrgId int64 - PaperSourceOwnerId int64 //nginx proxy PROXYURL string @@ -1563,7 +1562,6 @@ func NewContext() { sec = Cfg.Section("repo-square") IncubationSourceOrgId = sec.Key("INCUBATION_ORG_ID").MustInt64(9) - PaperSourceOwnerId = sec.Key("PAPER_OWNER_ID").MustInt64(36008) sec = Cfg.Section("point") CloudBrainPaySwitch = sec.Key("CLOUDBRAIN_PAY_SWITCH").MustBool(false) diff --git a/routers/repo/ai_model_manage.go b/routers/repo/ai_model_manage.go index 4eb04e772..65b6f2ee2 100644 --- a/routers/repo/ai_model_manage.go +++ b/routers/repo/ai_model_manage.go @@ -122,7 +122,7 @@ func saveModelByParameters(jobId string, versionName string, name string, versio return "", err } if modelSize > 0 { - go repository.ResetRepoModelNum(aiTask.RepoID) + go repository.IncreaseRepoModelNum(aiTask.RepoID) } if len(lastNewModelId) > 0 { //udpate status and version count @@ -330,7 +330,7 @@ func UpdateModelSize(modeluuid string) { } } if model.Size == 0 && size > 0 { - repository.ResetRepoModelNum(model.RepoId) + go repository.IncreaseRepoModelNum(model.RepoId) } } else { log.Info("not found model,uuid=" + modeluuid) @@ -491,7 +491,7 @@ func DeleteModelFile(ctx *context.Context) { } } if (model.Size - totalSize) <= 0 { - repository.ResetRepoModelNum(model.RepoId) + go repository.DecreaseRepoModelNum(model.RepoId) } } ctx.JSON(200, map[string]string{ @@ -562,7 +562,7 @@ func deleteModelByID(ctx *context.Context, id string) error { } } if model.Size > 0 { - go repository.ResetRepoModelNum(model.RepoId) + go repository.DecreaseRepoModelNum(model.RepoId) } } } diff --git a/routers/repo/attachment.go b/routers/repo/attachment.go index 8ae8e387f..e1de71345 100755 --- a/routers/repo/attachment.go +++ b/routers/repo/attachment.go @@ -181,7 +181,7 @@ func DeleteAttachment(ctx *context.Context) { ctx.Error(500, fmt.Sprintf("DeleteAttachment: %v", err)) return } - go repo_service.ResetRepoDatasetNumByDatasetId(attach.DatasetID) + go repo_service.DecreaseRepoDatasetNum(attach.DatasetID) attachjson, _ := json.Marshal(attach) labelmsg.SendDeleteAttachToLabelSys(string(attachjson)) @@ -896,7 +896,7 @@ func CompleteMultipart(ctx *context.Context) { return } attachment.UpdateDatasetUpdateUnix() - go repo_service.ResetRepoDatasetNumByDatasetId(dataset.ID) + go repo_service.IncreaseRepoDatasetNum(dataset.ID) repository, _ := models.GetRepositoryByID(dataset.RepoID) notification.NotifyOtherTask(ctx.User, repository, fmt.Sprint(repository.IsPrivate, attachment.IsPrivate), attachment.Name, models.ActionUploadAttachment) if attachment.DatasetID != 0 { diff --git a/services/repository/contributor.go b/services/repository/contributor.go new file mode 100644 index 000000000..896bc682d --- /dev/null +++ b/services/repository/contributor.go @@ -0,0 +1,55 @@ +package repository + +import ( + "code.gitea.io/gitea/models" + "code.gitea.io/gitea/modules/git" +) + +func GetRepoTopNContributors(repo *models.Repository, N int) ([]*models.ContributorInfo, int) { + var contributorInfos []*models.ContributorInfo + 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) + for _, c := range contributors { + if len(contributorInfos) >= N { + break + } + if c.Email == "" { + continue + } + // get user info from committer email + user, err := models.GetUserByActivateEmail(c.Email) + if err == nil { + // committer is system user, get info through user's primary email + if existedContributorInfo, ok := contributorInfoHash[user.Email]; ok { + // existed: same primary email, different committer name + existedContributorInfo.CommitCnt += c.CommitCnt + } else { + // new committer info + var newContributor = &models.ContributorInfo{ + user, user.RelAvatarLink(), user.Name, user.Email, c.CommitCnt, + } + contributorInfos = append(contributorInfos, newContributor) + contributorInfoHash[user.Email] = newContributor + } + } else { + // committer is not system user + if existedContributorInfo, ok := contributorInfoHash[c.Email]; ok { + // existed: same primary email, different committer name + existedContributorInfo.CommitCnt += c.CommitCnt + } else { + var newContributor = &models.ContributorInfo{ + user, "", "", c.Email, c.CommitCnt, + } + contributorInfos = append(contributorInfos, newContributor) + contributorInfoHash[c.Email] = newContributor + } + } + } + } + return contributorInfos, len(contributors) +} diff --git a/services/repository/repository.go b/services/repository/repository.go index 02928d855..fd9dfd3de 100644 --- a/services/repository/repository.go +++ b/services/repository/repository.go @@ -6,6 +6,7 @@ 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/notification" repo_module "code.gitea.io/gitea/modules/repository" @@ -16,6 +17,7 @@ import ( "net/http" "os" "strings" + "xorm.io/xorm" ) const SHELL_FLAG_ON = 1 @@ -332,18 +334,42 @@ func IsUploadFileInvalidErr(err error) bool { return ok } -func ResetRepoDatasetNumByDatasetId(datasetID int64) error { +func IncreaseRepoDatasetNum(datasetID int64, engines ...*xorm.Engine) error { dataset, err := models.GetDatasetByID(datasetID) if err != nil { return err } - return models.ResetRepoDatasetNum(dataset.RepoID) + return models.OperateRepoDatasetNum(dataset.RepoID, 1, engines...) } -func ResetRepoModelNum(repoId int64) error { - return models.ResetRepoModelNum(repoId) +func IncreaseRepoModelNum(repoId int64, engines ...*xorm.Engine) error { + return models.OperateRepoModelNum(repoId, 1, engines...) } -func ResetRepoAITaskNum(repoId int64) error { - return models.ResetRepoAITaskNum(repoId) +func DecreaseRepoDatasetNum(datasetID int64, engines ...*xorm.Engine) error { + dataset, err := models.GetDatasetByID(datasetID) + if err != nil { + return err + } + return models.OperateRepoDatasetNum(dataset.RepoID, -1, engines...) +} + +func DecreaseRepoModelNum(repoId int64, engines ...*xorm.Engine) error { + return models.OperateRepoModelNum(repoId, -1, engines...) +} + +func GetDefaultBranchName(repo *models.Repository) string { + gitRepo, err := git.OpenRepository(repo.RepoPath()) + if err != nil { + return "" + } + defer gitRepo.Close() + if len(repo.DefaultBranch) > 0 && gitRepo.IsBranchExist(repo.DefaultBranch) { + return repo.DefaultBranch + } + brs, _, err := gitRepo.GetBranches(0, 0) + if len(brs) > 0 { + return brs[0] + } + return "" } diff --git a/services/repository/square.go b/services/repository/square.go index 7ff36c5af..626134fd7 100644 --- a/services/repository/square.go +++ b/services/repository/square.go @@ -134,7 +134,11 @@ func FindRepos(opts FindReposOptions) (*models.FindReposResponse, error) { } result := make([]*models.Repository4Card, len(repos)) for i, r := range repos { - result[i] = r.ToCardFormat() + t := r.ToCardFormat() + contributors, n := GetRepoTopNContributors(r, 6) + t.Contributors = contributors + t.TotalContributorCount = n + result[i] = t } return &models.FindReposResponse{