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