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