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