You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

repo_statistic.go 12 kB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. package repo
  2. import (
  3. "errors"
  4. "time"
  5. "code.gitea.io/gitea/models"
  6. "code.gitea.io/gitea/modules/log"
  7. "code.gitea.io/gitea/modules/normalization"
  8. "code.gitea.io/gitea/modules/repository"
  9. "code.gitea.io/gitea/modules/setting"
  10. "code.gitea.io/gitea/services/mailer"
  11. "gitea.com/macaron/macaron"
  12. )
  13. func StatisticAuto() {
  14. RepoStatisticAuto()
  15. TimingCountData()
  16. }
  17. //auto daily
  18. func RepoStatisticAuto() {
  19. yesterday := time.Now().AddDate(0, 0, -1).Format("2006-01-02")
  20. setting.UpdateRadarMap()
  21. RepoStatisticDaily(yesterday)
  22. }
  23. func RepoStatisticDaily(date string) {
  24. log.Info("%s", date)
  25. log.Info("begin Repo Statistic")
  26. t, _ := time.Parse("2006-01-02", date)
  27. warnEmailMessage := "项目统计信息入库失败,请尽快定位。"
  28. if err := models.DeleteRepoStatDaily(date); err != nil {
  29. log.Error("DeleteRepoStatDaily failed: %v", err.Error())
  30. mailer.SendWarnNotifyMail(setting.Warn_Notify_Mails, warnEmailMessage)
  31. return
  32. }
  33. repos, err := models.GetAllRepositories()
  34. if err != nil {
  35. log.Error("GetAllRepositories failed: %v", err.Error())
  36. mailer.SendWarnNotifyMail(setting.Warn_Notify_Mails, warnEmailMessage)
  37. return
  38. }
  39. var reposRadar = make([]*models.RepoStatistic, 0)
  40. var minRepoRadar models.RepoStatistic
  41. var maxRepoRadar models.RepoStatistic
  42. isInitMinMaxRadar := false
  43. var error_projects = make([]string, 0)
  44. for _, repo := range repos {
  45. projectName := getDistinctProjectName(repo)
  46. log.Info("start statistic: %s", projectName)
  47. var numDevMonths, numWikiViews, numContributor, numKeyContributor, numCommitsGrowth, numCommitLinesGrowth, numContributorsGrowth, numCommits int64
  48. repoGitStat, err := models.GetRepoKPIStats(repo)
  49. if err != nil {
  50. log.Error("GetRepoKPIStats failed: %s", projectName)
  51. } else {
  52. numDevMonths = repoGitStat.DevelopAge
  53. numKeyContributor = repoGitStat.KeyContributors
  54. numWikiViews = repoGitStat.WikiPages
  55. numContributor = repoGitStat.Contributors
  56. numCommitsGrowth = repoGitStat.CommitsAdded
  57. numCommitLinesGrowth = repoGitStat.CommitLinesModified
  58. numContributorsGrowth = repoGitStat.ContributorsAdded
  59. numCommits = repoGitStat.Commits
  60. }
  61. var issueFixedRate float32
  62. if repo.NumIssues != 0 {
  63. issueFixedRate = float32(repo.NumClosedIssues) / float32(repo.NumIssues)
  64. } else {
  65. issueFixedRate = 1.0
  66. }
  67. var numVersions int64
  68. numVersions, err = models.GetReleaseCountByRepoID(repo.ID, models.FindReleasesOptions{})
  69. if err != nil {
  70. log.Error("GetReleaseCountByRepoID failed(%s): %v", projectName, err)
  71. }
  72. var datasetSize int64
  73. datasetSize, err = getDatasetSize(repo)
  74. if err != nil {
  75. log.Error("getDatasetSize failed(%s): %v", projectName, err)
  76. }
  77. var numComments int64
  78. numComments, err = models.GetCommentCountByRepoID(repo.ID)
  79. if err != nil {
  80. log.Error("GetCommentCountByRepoID failed(%s): %v", projectName, err)
  81. }
  82. beginTime, endTime := getStatTime(date)
  83. var numVisits int
  84. numVisits, err = repository.AppointProjectView(repo.OwnerName, repo.Name, beginTime, endTime)
  85. if err != nil {
  86. log.Error("AppointProjectView failed(%s): %v", projectName, err)
  87. }
  88. repoStat := models.RepoStatistic{
  89. RepoID: repo.ID,
  90. Date: date,
  91. Name: repo.Name,
  92. Alias: repo.Alias,
  93. IsPrivate: repo.IsPrivate,
  94. IsMirror: repo.IsMirror,
  95. IsFork: repo.IsFork,
  96. RepoCreatedUnix: repo.CreatedUnix,
  97. OwnerName: repo.OwnerName,
  98. NumWatches: int64(repo.NumWatches),
  99. NumStars: int64(repo.NumStars),
  100. NumForks: int64(repo.NumForks),
  101. NumDownloads: repo.CloneCnt,
  102. NumComments: numComments,
  103. NumVisits: int64(numVisits),
  104. NumClosedIssues: int64(repo.NumClosedIssues),
  105. NumVersions: numVersions,
  106. NumDevMonths: numDevMonths,
  107. RepoSize: repo.Size,
  108. DatasetSize: datasetSize,
  109. NumModels: 0,
  110. NumWikiViews: numWikiViews,
  111. NumCommits: numCommits,
  112. NumIssues: int64(repo.NumIssues),
  113. NumPulls: int64(repo.NumPulls),
  114. IssueFixedRate: issueFixedRate,
  115. NumContributor: numContributor,
  116. NumKeyContributor: numKeyContributor,
  117. NumCommitsGrowth: numCommitsGrowth,
  118. NumCommitLinesGrowth: numCommitLinesGrowth,
  119. NumContributorsGrowth: numContributorsGrowth,
  120. }
  121. dayBeforeDate := t.AddDate(0, 0, -1).Format("2006-01-02")
  122. repoStatisticsBefore, err := models.GetRepoStatisticByDate(dayBeforeDate, repo.ID)
  123. if err != nil {
  124. log.Error("get data of day before the date failed ", err)
  125. } else {
  126. if len(repoStatisticsBefore) > 0 {
  127. repoStatisticBefore := repoStatisticsBefore[0]
  128. repoStat.NumWatchesAdded = repoStat.NumWatches - repoStatisticBefore.NumWatches
  129. repoStat.NumStarsAdded = repoStat.NumStars - repoStatisticBefore.NumStars
  130. repoStat.NumForksAdded = repoStat.NumForks - repoStatisticBefore.NumForks
  131. repoStat.NumDownloadsAdded = repoStat.NumDownloads - repoStatisticBefore.NumDownloads
  132. repoStat.NumCommentsAdded = repoStat.NumComments - repoStatisticBefore.NumComments
  133. repoStat.NumClosedIssuesAdded = repoStat.NumClosedIssues - repoStatisticBefore.NumClosedIssues
  134. repoStat.NumCommitsAdded = repoStat.NumCommits - repoStatisticBefore.NumCommits
  135. repoStat.NumIssuesAdded = repoStat.NumIssues - repoStatisticBefore.NumIssues
  136. repoStat.NumPullsAdded = repoStat.NumPulls - repoStatisticBefore.NumPulls
  137. repoStat.NumContributorAdded = repoStat.NumContributor - repoStatisticBefore.NumContributor
  138. }
  139. }
  140. day4MonthsAgo := t.AddDate(0, -4, 0)
  141. repoStatisticFourMonthsAgo, err := models.GetOneRepoStatisticBeforeTime(day4MonthsAgo)
  142. if err != nil {
  143. log.Error("Get data of 4 moth ago failed.", err)
  144. } else {
  145. repoStat.NumCommentsGrowth = repoStat.NumComments - repoStatisticFourMonthsAgo.NumComments
  146. repoStat.NumIssuesGrowth = repoStat.NumIssues - repoStatisticFourMonthsAgo.NumIssues
  147. }
  148. models.SyncStatDataToRepo(repo)
  149. if _, err = models.InsertRepoStat(&repoStat); err != nil {
  150. log.Error("InsertRepoStat failed(%s): %v", projectName, err)
  151. log.Error("failed statistic: %s", projectName)
  152. error_projects = append(error_projects, projectName)
  153. continue
  154. }
  155. tempRepoStat := models.RepoStatistic{
  156. RepoID: repoStat.RepoID,
  157. Date: repoStat.Date,
  158. IsMirror: repoStat.IsMirror,
  159. Impact: normalization.GetImpactInitValue(repoStat.NumWatches, repoStat.NumStars, repoStat.NumForks, repoStat.NumDownloads, repoStat.NumComments, repoStat.NumVisits),
  160. Completeness: normalization.GetCompleteInitValue(repoStat.NumClosedIssues, repoStat.NumVersions, repoStat.NumDevMonths, repoStat.DatasetSize, repoStat.NumModels, repoStat.NumWikiViews),
  161. Liveness: normalization.GetLivenessInitValue(repoStat.NumCommits, repoStat.NumIssues, repoStat.NumPulls, repoStat.NumVisits),
  162. ProjectHealth: normalization.GetProjectHealthInitValue(repoStat.IssueFixedRate),
  163. TeamHealth: normalization.GetTeamHealthInitValue(repoStat.NumContributor, repoStat.NumKeyContributor, repoStat.NumContributorsGrowth),
  164. Growth: normalization.GetRepoGrowthInitValue(repoStat.NumCommitLinesGrowth, repoStat.NumIssuesGrowth, repoStat.NumCommitsGrowth, repoStat.NumContributorsGrowth, repoStat.NumCommentsGrowth),
  165. }
  166. reposRadar = append(reposRadar, &tempRepoStat)
  167. if !isInitMinMaxRadar {
  168. if !setting.RadarMap.IgnoreMirrorRepo || (setting.RadarMap.IgnoreMirrorRepo && !tempRepoStat.IsMirror) {
  169. minRepoRadar = tempRepoStat
  170. maxRepoRadar = tempRepoStat
  171. isInitMinMaxRadar = true
  172. }
  173. } else {
  174. if !setting.RadarMap.IgnoreMirrorRepo || (setting.RadarMap.IgnoreMirrorRepo && !tempRepoStat.IsMirror) {
  175. if tempRepoStat.Impact < minRepoRadar.Impact {
  176. minRepoRadar.Impact = tempRepoStat.Impact
  177. }
  178. if tempRepoStat.Impact > maxRepoRadar.Impact {
  179. maxRepoRadar.Impact = tempRepoStat.Impact
  180. }
  181. if tempRepoStat.Completeness < minRepoRadar.Completeness {
  182. minRepoRadar.Completeness = tempRepoStat.Completeness
  183. }
  184. if tempRepoStat.Completeness > maxRepoRadar.Completeness {
  185. maxRepoRadar.Completeness = tempRepoStat.Completeness
  186. }
  187. if tempRepoStat.Liveness < minRepoRadar.Liveness {
  188. minRepoRadar.Liveness = tempRepoStat.Liveness
  189. }
  190. if tempRepoStat.Liveness > maxRepoRadar.Liveness {
  191. maxRepoRadar.Liveness = tempRepoStat.Liveness
  192. }
  193. if tempRepoStat.ProjectHealth < minRepoRadar.ProjectHealth {
  194. minRepoRadar.ProjectHealth = tempRepoStat.ProjectHealth
  195. }
  196. if tempRepoStat.ProjectHealth > maxRepoRadar.ProjectHealth {
  197. maxRepoRadar.ProjectHealth = tempRepoStat.ProjectHealth
  198. }
  199. if tempRepoStat.TeamHealth < minRepoRadar.TeamHealth {
  200. minRepoRadar.TeamHealth = tempRepoStat.TeamHealth
  201. }
  202. if tempRepoStat.TeamHealth > maxRepoRadar.TeamHealth {
  203. maxRepoRadar.TeamHealth = tempRepoStat.TeamHealth
  204. }
  205. if tempRepoStat.Growth < minRepoRadar.Growth {
  206. minRepoRadar.Growth = tempRepoStat.Growth
  207. }
  208. if tempRepoStat.Growth > maxRepoRadar.Growth {
  209. maxRepoRadar.Growth = tempRepoStat.Growth
  210. }
  211. }
  212. }
  213. log.Info("finish statistic: %s", getDistinctProjectName(repo))
  214. }
  215. if len(error_projects) > 0 {
  216. mailer.SendWarnNotifyMail(setting.Warn_Notify_Mails, warnEmailMessage)
  217. }
  218. //radar map
  219. log.Info("begin statistic radar")
  220. for _, radarInit := range reposRadar {
  221. if radarInit.IsMirror && setting.RadarMap.IgnoreMirrorRepo {
  222. radarInit.Impact = 0
  223. radarInit.Completeness = 0
  224. radarInit.Liveness = 0
  225. radarInit.ProjectHealth = 0
  226. radarInit.TeamHealth = 0
  227. radarInit.Growth = 0
  228. radarInit.RadarTotal = 0
  229. } else {
  230. radarInit.Impact = normalization.Normalization(radarInit.Impact, minRepoRadar.Impact, maxRepoRadar.Impact)
  231. radarInit.Completeness = normalization.Normalization(radarInit.Completeness, minRepoRadar.Completeness, maxRepoRadar.Completeness)
  232. radarInit.Liveness = normalization.Normalization(radarInit.Liveness, minRepoRadar.Liveness, maxRepoRadar.Liveness)
  233. radarInit.ProjectHealth = normalization.Normalization(radarInit.ProjectHealth, minRepoRadar.ProjectHealth, maxRepoRadar.ProjectHealth)
  234. radarInit.TeamHealth = normalization.Normalization(radarInit.TeamHealth, minRepoRadar.TeamHealth, maxRepoRadar.TeamHealth)
  235. radarInit.Growth = normalization.Normalization(radarInit.Growth, minRepoRadar.Growth, maxRepoRadar.Growth)
  236. radarInit.RadarTotal = normalization.GetRadarValue(radarInit.Impact, radarInit.Completeness, radarInit.Liveness, radarInit.ProjectHealth, radarInit.TeamHealth, radarInit.Growth)
  237. }
  238. models.UpdateRepoStat(radarInit)
  239. }
  240. log.Info("finish statistic: radar")
  241. }
  242. func getDistinctProjectName(repo *models.Repository) string {
  243. return repo.OwnerName + "/" + repo.Alias
  244. }
  245. func getDatasetSize(repo *models.Repository) (int64, error) {
  246. dataset, err := models.GetDatasetByRepo(repo)
  247. if err != nil {
  248. return 0, err
  249. }
  250. return models.GetAttachmentSizeByDatasetID(dataset.ID)
  251. }
  252. func getStatTime(timeStr string) (string, string) {
  253. t, _ := time.Parse("2006-01-02", timeStr)
  254. timeNumber := t.Unix()
  255. beginTimeNumber := timeNumber - 8*60*60
  256. endTimeNumber := timeNumber + 16*60*60
  257. beginTime := time.Unix(beginTimeNumber, 0).Format(time.RFC3339)
  258. endTime := time.Unix(endTimeNumber, 0).Format(time.RFC3339)
  259. log.Info("%s, %s", beginTime, endTime)
  260. return beginTime, endTime
  261. }
  262. func UpdateRepoVisits(ctx *macaron.Context, repo *models.Repository, date string) error {
  263. beginTime, endTime := getStatTime(date)
  264. var numVisits int
  265. numVisits, err := repository.AppointProjectView(repo.OwnerName, repo.Name, beginTime, endTime)
  266. if err != nil {
  267. log.Error("AppointProjectView failed(%s): %v", getDistinctProjectName(repo), err)
  268. return err
  269. }
  270. repoStat, err := models.GetRepoStatisticByDate(date, repo.ID)
  271. if err != nil {
  272. log.Error("GetRepoStatisticByDate failed(%s): %v", getDistinctProjectName(repo), err)
  273. return err
  274. } else if len(repoStat) != 1 {
  275. log.Error("GetRepoStatisticByDate failed(%s): %v", getDistinctProjectName(repo), err)
  276. return errors.New("not find repo")
  277. }
  278. repoStat[0].NumVisits = int64(numVisits)
  279. if err = models.UpdateRepoStatVisits(repoStat[0]); err != nil {
  280. log.Error("UpdateRepoStatVisits failed(%s): %v", getDistinctProjectName(repo), err)
  281. return err
  282. }
  283. return nil
  284. }