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.6 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. NumWatches: int64(repo.NumWatches),
  77. NumStars: int64(repo.NumStars),
  78. NumDownloads: repo.CloneCnt,
  79. NumComments: numComments,
  80. NumVisits: int64(numVisits),
  81. NumClosedIssues: int64(repo.NumClosedIssues),
  82. NumVersions: numVersions,
  83. NumDevMonths: numDevMonths,
  84. RepoSize: repo.Size,
  85. DatasetSize: datasetSize,
  86. NumModels: 0,
  87. NumWikiViews: numWikiViews,
  88. NumCommits: repo.NumCommit,
  89. NumIssues: int64(repo.NumIssues),
  90. NumPulls: int64(repo.NumPulls),
  91. IssueFixedRate: issueFixedRate,
  92. NumContributor: numContributor,
  93. NumKeyContributor: numKeyContributor,
  94. NumCommitsGrowth: numCommitsGrowth,
  95. NumCommitLinesGrowth: numCommitLinesGrowth,
  96. NumContributorsGrowth: numContributorsGrowth,
  97. }
  98. dayBeforeDate := t.AddDate(0, 0, -1).Format("2006-01-02")
  99. repoStatisticsBefore, err := models.GetRepoStatisticByDate(dayBeforeDate)
  100. if err != nil {
  101. log.Error("get data of day before the date failed ", err)
  102. } else {
  103. if len(repoStatisticsBefore) > 0 {
  104. repoStatisticBefore := repoStatisticsBefore[0]
  105. repoStat.NumWatchesAdded = repoStat.NumWatches - repoStatisticBefore.NumWatches
  106. repoStat.NumStarsAdded = repoStat.NumStars - repoStatisticBefore.NumStars
  107. repoStat.NumForksAdded = repoStat.NumForks - repoStatisticBefore.NumForks
  108. repoStat.NumDownloadsAdded = repoStat.NumDownloads - repoStatisticBefore.NumDownloads
  109. repoStat.NumCommentsAdded = repoStat.NumComments - repoStatisticBefore.NumComments
  110. repoStat.NumClosedIssuesAdded = repoStat.NumClosedIssues - repoStatisticBefore.NumClosedIssues
  111. repoStat.NumCommitsAdded = repoStat.NumCommits - repoStatisticBefore.NumCommits
  112. repoStat.NumIssuesAdded = repoStat.NumIssues - repoStatisticBefore.NumIssues
  113. repoStat.NumPullsAdded = repoStat.NumPulls - repoStatisticBefore.NumPulls
  114. repoStat.NumContributorAdded = repoStat.NumContributor - repoStatisticBefore.NumContributor
  115. }
  116. }
  117. day4MonthsAgo := t.AddDate(0, -4, 0)
  118. repoStatisticFourMonthsAgo, err := models.GetOneRepoStatisticBeforeTime(day4MonthsAgo)
  119. if err != nil {
  120. log.Error("Get data of 4 moth ago failed.", err)
  121. } else {
  122. repoStat.NumCommentsGrowth = repoStat.NumComments - repoStatisticFourMonthsAgo.NumComments
  123. repoStat.NumIssuesGrowth = repoStat.NumIssues - repoStatisticFourMonthsAgo.NumIssues
  124. }
  125. if _, err = models.InsertRepoStat(&repoStat); err != nil {
  126. log.Error("InsertRepoStat failed(%s): %v", repo.Name, err)
  127. log.Error("failed statistic: %s", repo.Name)
  128. continue
  129. }
  130. tempRepoStat := models.RepoStatistic{
  131. RepoID: repoStat.RepoID,
  132. Date: repoStat.Date,
  133. Impact: normalization.GetImpactInitValue(repoStat.NumWatches, repoStat.NumStars, repoStat.NumForks, repoStat.NumDownloads, repoStat.NumComments, repoStat.NumVisits),
  134. Completeness: normalization.GetCompleteInitValue(repoStat.NumClosedIssues, repoStat.NumVersions, repoStat.NumDevMonths, repoStat.DatasetSize, repoStat.NumModels, repoStat.NumWikiViews),
  135. Liveness: normalization.GetLivenessInitValue(repoStat.NumCommits, repoStat.NumIssues, repoStat.NumPulls, repoStat.NumVisits),
  136. ProjectHealth: normalization.GetProjectHealthInitValue(repoStat.IssueFixedRate),
  137. TeamHealth: normalization.GetTeamHealthInitValue(repoStat.NumContributor, repoStat.NumKeyContributor, repoStat.NumContributorsGrowth),
  138. Growth: normalization.GetRepoGrowthInitValue(repoStat.NumCommitLinesGrowth, repoStat.NumIssuesGrowth, repoStat.NumCommitsGrowth, repoStat.NumContributorsGrowth, repoStat.NumCommentsGrowth),
  139. }
  140. reposRadar = append(reposRadar, &tempRepoStat)
  141. if i == 0 {
  142. minRepoRadar = tempRepoStat
  143. maxRepoRadar = tempRepoStat
  144. } else {
  145. if tempRepoStat.Impact < minRepoRadar.Impact {
  146. minRepoRadar.Impact = tempRepoStat.Impact
  147. }
  148. if tempRepoStat.Impact > maxRepoRadar.Impact {
  149. maxRepoRadar.Impact = tempRepoStat.Impact
  150. }
  151. if tempRepoStat.Completeness < minRepoRadar.Completeness {
  152. minRepoRadar.Completeness = tempRepoStat.Completeness
  153. }
  154. if tempRepoStat.Completeness > maxRepoRadar.Completeness {
  155. maxRepoRadar.Completeness = tempRepoStat.Completeness
  156. }
  157. if tempRepoStat.Liveness < minRepoRadar.Completeness {
  158. minRepoRadar.Liveness = tempRepoStat.Liveness
  159. }
  160. if tempRepoStat.Liveness > maxRepoRadar.Liveness {
  161. maxRepoRadar.Liveness = tempRepoStat.Liveness
  162. }
  163. if tempRepoStat.ProjectHealth < minRepoRadar.ProjectHealth {
  164. minRepoRadar.ProjectHealth = tempRepoStat.ProjectHealth
  165. }
  166. if tempRepoStat.ProjectHealth > maxRepoRadar.ProjectHealth {
  167. maxRepoRadar.ProjectHealth = tempRepoStat.ProjectHealth
  168. }
  169. if tempRepoStat.TeamHealth < minRepoRadar.TeamHealth {
  170. minRepoRadar.TeamHealth = tempRepoStat.TeamHealth
  171. }
  172. if tempRepoStat.TeamHealth > maxRepoRadar.TeamHealth {
  173. maxRepoRadar.TeamHealth = tempRepoStat.TeamHealth
  174. }
  175. if tempRepoStat.Growth < minRepoRadar.Growth {
  176. minRepoRadar.Growth = tempRepoStat.Growth
  177. }
  178. if tempRepoStat.Growth > maxRepoRadar.Growth {
  179. maxRepoRadar.Growth = tempRepoStat.Growth
  180. }
  181. }
  182. log.Info("finish statistic: %s", repo.Name)
  183. }
  184. //radar map
  185. log.Info("begin statistic radar")
  186. for _, radarInit := range reposRadar {
  187. radarInit.Impact = normalization.Normalization(radarInit.Impact, minRepoRadar.Impact, maxRepoRadar.Impact)
  188. radarInit.Completeness = normalization.Normalization(radarInit.Completeness, minRepoRadar.Completeness, maxRepoRadar.Completeness)
  189. radarInit.Liveness = normalization.Normalization(radarInit.Liveness, minRepoRadar.Liveness, maxRepoRadar.Liveness)
  190. radarInit.ProjectHealth = normalization.Normalization(radarInit.ProjectHealth, minRepoRadar.ProjectHealth, maxRepoRadar.ProjectHealth)
  191. radarInit.TeamHealth = normalization.Normalization(radarInit.TeamHealth, minRepoRadar.TeamHealth, maxRepoRadar.TeamHealth)
  192. radarInit.Growth = normalization.Normalization(radarInit.Growth, minRepoRadar.Growth, maxRepoRadar.Growth)
  193. radarInit.RadarTotal = normalization.GetRadarValue(radarInit.Impact, radarInit.Completeness, radarInit.Liveness, radarInit.ProjectHealth, radarInit.TeamHealth, radarInit.Growth)
  194. models.UpdateRepoStat(radarInit)
  195. }
  196. log.Info("finish statistic: radar")
  197. }
  198. func getDatasetSize(repo *models.Repository) (int64, error) {
  199. dataset, err := models.GetDatasetByRepo(repo)
  200. if err != nil {
  201. return 0, err
  202. }
  203. return models.GetAttachmentSizeByDatasetID(dataset.ID)
  204. }
  205. func getStatTime(timeStr string) (string, string) {
  206. t, _ := time.Parse("2006-01-02", timeStr)
  207. timeNumber := t.Unix()
  208. beginTimeNumber := timeNumber - 8*60*60
  209. endTimeNumber := timeNumber + 16*60*60
  210. beginTime := time.Unix(beginTimeNumber, 0).Format(time.RFC3339)
  211. endTime := time.Unix(endTimeNumber, 0).Format(time.RFC3339)
  212. log.Info("%s, %s", beginTime, endTime)
  213. return beginTime, endTime
  214. }