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 9.8 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. package repo
  2. import (
  3. "time"
  4. "code.gitea.io/gitea/modules/setting"
  5. "code.gitea.io/gitea/modules/normalization"
  6. "code.gitea.io/gitea/models"
  7. "code.gitea.io/gitea/modules/log"
  8. "code.gitea.io/gitea/modules/repository"
  9. )
  10. //auto daily or manually
  11. func RepoStatisticAuto() {
  12. log.Info("", time.Now())
  13. yesterday := time.Now().AddDate(0, 0, -1).Format("2006-01-02")
  14. setting.UpdateRadarMap()
  15. RepoStatisticDaily(yesterday)
  16. }
  17. func RepoStatisticDaily(date string) {
  18. log.Info("%s", date)
  19. log.Info("begin Repo Statistic")
  20. t, _ := time.Parse("2006-01-02", date)
  21. if err := models.DeleteRepoStatDaily(date); err != nil {
  22. log.Error("DeleteRepoStatDaily failed: %v", err.Error())
  23. return
  24. }
  25. repos, err := models.GetAllRepositories()
  26. if err != nil {
  27. log.Error("GetAllRepositories failed: %v", err.Error())
  28. return
  29. }
  30. var reposRadar = make([]*models.RepoStatistic, 0)
  31. var minRepoRadar models.RepoStatistic
  32. var maxRepoRadar models.RepoStatistic
  33. for i, repo := range repos {
  34. log.Info("start statistic: %s", repo.Name)
  35. var numDevMonths, numWikiViews, numContributor, numKeyContributor, numCommitsGrowth, numCommitLinesGrowth, numContributorsGrowth int64
  36. repoGitStat, err := models.GetRepoKPIStats(repo)
  37. if err != nil {
  38. log.Error("GetRepoKPIStats failed: %s", repo.Name)
  39. } else {
  40. numDevMonths = repoGitStat.DevelopAge
  41. numKeyContributor = repoGitStat.KeyContributors
  42. numWikiViews = repoGitStat.WikiPages
  43. numContributor = repoGitStat.Contributors
  44. numCommitsGrowth = repoGitStat.CommitsAdded
  45. numCommitLinesGrowth = repoGitStat.CommitLinesModified
  46. numContributorsGrowth = repoGitStat.ContributorsAdded
  47. }
  48. var issueFixedRate float32
  49. if repo.NumIssues != 0 {
  50. issueFixedRate = float32(repo.NumClosedIssues) / float32(repo.NumIssues)
  51. }
  52. var numVersions int64
  53. numVersions, err = models.GetReleaseCountByRepoID(repo.ID, models.FindReleasesOptions{})
  54. if err != nil {
  55. log.Error("GetReleaseCountByRepoID failed(%s): %v", repo.Name, err)
  56. }
  57. var datasetSize int64
  58. datasetSize, err = getDatasetSize(repo)
  59. if err != nil {
  60. log.Error("getDatasetSize failed(%s): %v", repo.Name, err)
  61. }
  62. var numComments int64
  63. numComments, err = models.GetCommentCountByRepoID(repo.ID)
  64. if err != nil {
  65. log.Error("GetCommentCountByRepoID failed(%s): %v", repo.Name, err)
  66. }
  67. beginTime, endTime := getStatTime(date)
  68. var numVisits int
  69. numVisits, err = repository.AppointProjectView(repo.OwnerName, repo.Name, beginTime, endTime)
  70. if err != nil {
  71. log.Error("AppointProjectView failed(%s): %v", repo.Name, err)
  72. }
  73. repoStat := models.RepoStatistic{
  74. RepoID: repo.ID,
  75. Date: date,
  76. Name: repo.Name,
  77. IsPrivate: repo.IsPrivate,
  78. NumWatches: int64(repo.NumWatches),
  79. NumStars: int64(repo.NumStars),
  80. NumForks: int64(repo.NumForks),
  81. NumDownloads: repo.CloneCnt,
  82. NumComments: numComments,
  83. NumVisits: int64(numVisits),
  84. NumClosedIssues: int64(repo.NumClosedIssues),
  85. NumVersions: numVersions,
  86. NumDevMonths: numDevMonths,
  87. RepoSize: repo.Size,
  88. DatasetSize: datasetSize,
  89. NumModels: 0,
  90. NumWikiViews: numWikiViews,
  91. NumCommits: repo.NumCommit,
  92. NumIssues: int64(repo.NumIssues),
  93. NumPulls: int64(repo.NumPulls),
  94. IssueFixedRate: issueFixedRate,
  95. NumContributor: numContributor,
  96. NumKeyContributor: numKeyContributor,
  97. NumCommitsGrowth: numCommitsGrowth,
  98. NumCommitLinesGrowth: numCommitLinesGrowth,
  99. NumContributorsGrowth: numContributorsGrowth,
  100. }
  101. dayBeforeDate := t.AddDate(0, 0, -1).Format("2006-01-02")
  102. repoStatisticsBefore, err := models.GetRepoStatisticByDate(dayBeforeDate, repo.ID)
  103. if err != nil {
  104. log.Error("get data of day before the date failed ", err)
  105. } else {
  106. if len(repoStatisticsBefore) > 0 {
  107. repoStatisticBefore := repoStatisticsBefore[0]
  108. repoStat.NumWatchesAdded = repoStat.NumWatches - repoStatisticBefore.NumWatches
  109. repoStat.NumStarsAdded = repoStat.NumStars - repoStatisticBefore.NumStars
  110. repoStat.NumForksAdded = repoStat.NumForks - repoStatisticBefore.NumForks
  111. repoStat.NumDownloadsAdded = repoStat.NumDownloads - repoStatisticBefore.NumDownloads
  112. repoStat.NumCommentsAdded = repoStat.NumComments - repoStatisticBefore.NumComments
  113. repoStat.NumClosedIssuesAdded = repoStat.NumClosedIssues - repoStatisticBefore.NumClosedIssues
  114. repoStat.NumCommitsAdded = repoStat.NumCommits - repoStatisticBefore.NumCommits
  115. repoStat.NumIssuesAdded = repoStat.NumIssues - repoStatisticBefore.NumIssues
  116. repoStat.NumPullsAdded = repoStat.NumPulls - repoStatisticBefore.NumPulls
  117. repoStat.NumContributorAdded = repoStat.NumContributor - repoStatisticBefore.NumContributor
  118. }
  119. }
  120. day4MonthsAgo := t.AddDate(0, -4, 0)
  121. repoStatisticFourMonthsAgo, err := models.GetOneRepoStatisticBeforeTime(day4MonthsAgo)
  122. if err != nil {
  123. log.Error("Get data of 4 moth ago failed.", err)
  124. } else {
  125. repoStat.NumCommentsGrowth = repoStat.NumComments - repoStatisticFourMonthsAgo.NumComments
  126. repoStat.NumIssuesGrowth = repoStat.NumIssues - repoStatisticFourMonthsAgo.NumIssues
  127. }
  128. if _, err = models.InsertRepoStat(&repoStat); err != nil {
  129. log.Error("InsertRepoStat failed(%s): %v", repo.Name, err)
  130. log.Error("failed statistic: %s", repo.Name)
  131. continue
  132. }
  133. tempRepoStat := models.RepoStatistic{
  134. RepoID: repoStat.RepoID,
  135. Date: repoStat.Date,
  136. Impact: normalization.GetImpactInitValue(repoStat.NumWatches, repoStat.NumStars, repoStat.NumForks, repoStat.NumDownloads, repoStat.NumComments, repoStat.NumVisits),
  137. Completeness: normalization.GetCompleteInitValue(repoStat.NumClosedIssues, repoStat.NumVersions, repoStat.NumDevMonths, repoStat.DatasetSize, repoStat.NumModels, repoStat.NumWikiViews),
  138. Liveness: normalization.GetLivenessInitValue(repoStat.NumCommits, repoStat.NumIssues, repoStat.NumPulls, repoStat.NumVisits),
  139. ProjectHealth: normalization.GetProjectHealthInitValue(repoStat.IssueFixedRate),
  140. TeamHealth: normalization.GetTeamHealthInitValue(repoStat.NumContributor, repoStat.NumKeyContributor, repoStat.NumContributorsGrowth),
  141. Growth: normalization.GetRepoGrowthInitValue(repoStat.NumCommitLinesGrowth, repoStat.NumIssuesGrowth, repoStat.NumCommitsGrowth, repoStat.NumContributorsGrowth, repoStat.NumCommentsGrowth),
  142. }
  143. reposRadar = append(reposRadar, &tempRepoStat)
  144. if i == 0 {
  145. minRepoRadar = tempRepoStat
  146. maxRepoRadar = tempRepoStat
  147. } else {
  148. if tempRepoStat.Impact < minRepoRadar.Impact {
  149. minRepoRadar.Impact = tempRepoStat.Impact
  150. }
  151. if tempRepoStat.Impact > maxRepoRadar.Impact {
  152. maxRepoRadar.Impact = tempRepoStat.Impact
  153. }
  154. if tempRepoStat.Completeness < minRepoRadar.Completeness {
  155. minRepoRadar.Completeness = tempRepoStat.Completeness
  156. }
  157. if tempRepoStat.Completeness > maxRepoRadar.Completeness {
  158. maxRepoRadar.Completeness = tempRepoStat.Completeness
  159. }
  160. if tempRepoStat.Liveness < minRepoRadar.Completeness {
  161. minRepoRadar.Liveness = tempRepoStat.Liveness
  162. }
  163. if tempRepoStat.Liveness > maxRepoRadar.Liveness {
  164. maxRepoRadar.Liveness = tempRepoStat.Liveness
  165. }
  166. if tempRepoStat.ProjectHealth < minRepoRadar.ProjectHealth {
  167. minRepoRadar.ProjectHealth = tempRepoStat.ProjectHealth
  168. }
  169. if tempRepoStat.ProjectHealth > maxRepoRadar.ProjectHealth {
  170. maxRepoRadar.ProjectHealth = tempRepoStat.ProjectHealth
  171. }
  172. if tempRepoStat.TeamHealth < minRepoRadar.TeamHealth {
  173. minRepoRadar.TeamHealth = tempRepoStat.TeamHealth
  174. }
  175. if tempRepoStat.TeamHealth > maxRepoRadar.TeamHealth {
  176. maxRepoRadar.TeamHealth = tempRepoStat.TeamHealth
  177. }
  178. if tempRepoStat.Growth < minRepoRadar.Growth {
  179. minRepoRadar.Growth = tempRepoStat.Growth
  180. }
  181. if tempRepoStat.Growth > maxRepoRadar.Growth {
  182. maxRepoRadar.Growth = tempRepoStat.Growth
  183. }
  184. }
  185. log.Info("finish statistic: %s", repo.Name)
  186. }
  187. //radar map
  188. log.Info("begin statistic radar")
  189. for _, radarInit := range reposRadar {
  190. radarInit.Impact = normalization.Normalization(radarInit.Impact, minRepoRadar.Impact, maxRepoRadar.Impact)
  191. radarInit.Completeness = normalization.Normalization(radarInit.Completeness, minRepoRadar.Completeness, maxRepoRadar.Completeness)
  192. radarInit.Liveness = normalization.Normalization(radarInit.Liveness, minRepoRadar.Liveness, maxRepoRadar.Liveness)
  193. radarInit.ProjectHealth = normalization.Normalization(radarInit.ProjectHealth, minRepoRadar.ProjectHealth, maxRepoRadar.ProjectHealth)
  194. radarInit.TeamHealth = normalization.Normalization(radarInit.TeamHealth, minRepoRadar.TeamHealth, maxRepoRadar.TeamHealth)
  195. radarInit.Growth = normalization.Normalization(radarInit.Growth, minRepoRadar.Growth, maxRepoRadar.Growth)
  196. radarInit.RadarTotal = normalization.GetRadarValue(radarInit.Impact, radarInit.Completeness, radarInit.Liveness, radarInit.ProjectHealth, radarInit.TeamHealth, radarInit.Growth)
  197. models.UpdateRepoStat(radarInit)
  198. }
  199. log.Info("finish statistic: radar")
  200. }
  201. func getDatasetSize(repo *models.Repository) (int64, error) {
  202. dataset, err := models.GetDatasetByRepo(repo)
  203. if err != nil {
  204. return 0, err
  205. }
  206. return models.GetAttachmentSizeByDatasetID(dataset.ID)
  207. }
  208. func getStatTime(timeStr string) (string, string) {
  209. t, _ := time.Parse("2006-01-02", timeStr)
  210. timeNumber := t.Unix()
  211. beginTimeNumber := timeNumber - 8*60*60
  212. endTimeNumber := timeNumber + 16*60*60
  213. beginTime := time.Unix(beginTimeNumber, 0).Format(time.RFC3339)
  214. endTime := time.Unix(endTimeNumber, 0).Format(time.RFC3339)
  215. log.Info("%s, %s", beginTime, endTime)
  216. return beginTime, endTime
  217. }