package repo import ( "time" "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/repository" ) //auto daily or manually func RepoStatisticAuto() { log.Info("", time.Now()) yesterday := time.Now().AddDate(0, 0, -1).Format("2006-01-02") RepoStatisticDaily(yesterday) } func RepoStatisticDaily(date string) { log.Info("%s", date) if err := models.DeleteRepoStatDaily(date); err != nil { log.Error("DeleteRepoStatDaily failed: %v", err.Error()) return } repos, err := models.GetAllRepositories() if err != nil { log.Error("GetAllRepositories failed: %v", err.Error()) return } for _, repo := range repos { log.Info("start statistic: %s", repo.Name) repoGitStat, err := models.GetRepoKPIStats(repo) if err != nil { log.Error("GetRepoKPIStats failed: %s", repo.Name) log.Error("failed statistic: %s", repo.Name) continue } var issueFixedRate float32 if repo.NumIssues != 0 { issueFixedRate = float32(repo.NumClosedIssues) / float32(repo.NumIssues) } numVersions, err := models.GetReleaseCountByRepoID(repo.ID, models.FindReleasesOptions{}) if err != nil { log.Error("GetReleaseCountByRepoID failed: %s", repo.Name) log.Error("failed statistic: %s", repo.Name) continue } datasetSize, err := getDatasetSize(repo) if err != nil { log.Error("getDatasetSize failed: %s", repo.Name) log.Error("failed statistic: %s", repo.Name) continue } numComments, err := models.GetCommentCountByRepoID(repo.ID) if err != nil { log.Error("GetCommentCountByRepoID failed: %s", repo.Name) log.Error("failed statistic: %s", repo.Name) continue } beginTime, endTime := getStatTime(date) numVisits, err := repository.AppointProjectView(repo.OwnerName, repo.Name, beginTime, endTime) if err != nil { log.Error("Get numVisits failed", err) numVisits = 0 } repoStat := models.RepoStatistic{ RepoID: repo.ID, Date: date, NumWatches: int64(repo.NumWatches), NumStars: int64(repo.NumStars), NumDownloads: repo.CloneCnt, NumComments: numComments, NumVisits: int64(numVisits), NumClosedIssues: int64(repo.NumClosedIssues), NumVersions: numVersions, NumDevMonths: repoGitStat.DevelopAge, RepoSize: repo.Size, DatasetSize: datasetSize, NumModels: 0, NumWikiViews: repoGitStat.WikiPages, NumCommits: repo.NumCommit, NumIssues: int64(repo.NumIssues), NumPulls: int64(repo.NumPulls), IssueFixedRate: issueFixedRate, NumContributor: repoGitStat.Contributors, NumKeyContributor: repoGitStat.KeyContributors, } if _, err = models.InsertRepoStat(&repoStat); err != nil { log.Error("InsertRepoStat failed: %s", repo.Name) log.Error("failed statistic: %s", repo.Name) continue } log.Info("finish statistic: %s", repo.Name) } } func getDatasetSize(repo *models.Repository) (int64, error) { dataset, err := models.GetDatasetByRepo(repo) if err != nil { return 0, err } return models.GetAttachmentSizeByDatasetID(dataset.ID) } func getStatTime(timeStr string) (string, string) { t, _ := time.Parse("2006-01-02", timeStr) timeNumber := t.Unix() beginTimeNumber := timeNumber - 8*60*60 endTimeNumber := timeNumber + 16*60*60 beginTime := time.Unix(beginTimeNumber, 0).Format(time.RFC3339) endTime := time.Unix(endTimeNumber, 0).Format(time.RFC3339) log.Info("%s, %s", beginTime, endTime) return beginTime, endTime }