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