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 12 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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. isInitMinMaxRadar := false
  43. var error_projects = make([]string, 0)
  44. for _, repo := range repos {
  45. projectName := getDistinctProjectName(repo)
  46. log.Info("start statistic: %s", projectName)
  47. var numDevMonths, numWikiViews, numContributor, numKeyContributor, numCommitsGrowth, numCommitLinesGrowth, numContributorsGrowth, numCommits int64
  48. repoGitStat, err := models.GetRepoKPIStats(repo)
  49. if err != nil {
  50. log.Error("GetRepoKPIStats failed: %s", projectName)
  51. } else {
  52. numDevMonths = repoGitStat.DevelopAge
  53. numKeyContributor = repoGitStat.KeyContributors
  54. numWikiViews = repoGitStat.WikiPages
  55. numContributor = repoGitStat.Contributors
  56. numCommitsGrowth = repoGitStat.CommitsAdded
  57. numCommitLinesGrowth = repoGitStat.CommitLinesModified
  58. numContributorsGrowth = repoGitStat.ContributorsAdded
  59. numCommits = repoGitStat.Commits
  60. }
  61. var issueFixedRate float32
  62. if repo.NumIssues != 0 {
  63. issueFixedRate = float32(repo.NumClosedIssues) / float32(repo.NumIssues)
  64. } else {
  65. issueFixedRate = 1.0
  66. }
  67. var numVersions int64
  68. numVersions, err = models.GetReleaseCountByRepoID(repo.ID, models.FindReleasesOptions{})
  69. if err != nil {
  70. log.Error("GetReleaseCountByRepoID failed(%s): %v", projectName, err)
  71. }
  72. var datasetSize int64
  73. datasetSize, err = getDatasetSize(repo)
  74. if err != nil {
  75. log.Error("getDatasetSize failed(%s): %v", projectName, err)
  76. }
  77. var numComments int64
  78. numComments, err = models.GetCommentCountByRepoID(repo.ID)
  79. if err != nil {
  80. log.Error("GetCommentCountByRepoID failed(%s): %v", projectName, err)
  81. }
  82. beginTime, endTime := getStatTime(date)
  83. var numVisits int
  84. numVisits, err = repository.AppointProjectView(repo.OwnerName, repo.Name, beginTime, endTime)
  85. if err != nil {
  86. log.Error("AppointProjectView failed(%s): %v", projectName, err)
  87. }
  88. repoStat := models.RepoStatistic{
  89. RepoID: repo.ID,
  90. Date: date,
  91. Name: repo.Name,
  92. Alias: repo.Alias,
  93. IsPrivate: repo.IsPrivate,
  94. IsMirror: repo.IsMirror,
  95. OwnerName: repo.OwnerName,
  96. NumWatches: int64(repo.NumWatches),
  97. NumStars: int64(repo.NumStars),
  98. NumForks: int64(repo.NumForks),
  99. NumDownloads: repo.CloneCnt,
  100. NumComments: numComments,
  101. NumVisits: int64(numVisits),
  102. NumClosedIssues: int64(repo.NumClosedIssues),
  103. NumVersions: numVersions,
  104. NumDevMonths: numDevMonths,
  105. RepoSize: repo.Size,
  106. DatasetSize: datasetSize,
  107. NumModels: 0,
  108. NumWikiViews: numWikiViews,
  109. NumCommits: numCommits,
  110. NumIssues: int64(repo.NumIssues),
  111. NumPulls: int64(repo.NumPulls),
  112. IssueFixedRate: issueFixedRate,
  113. NumContributor: numContributor,
  114. NumKeyContributor: numKeyContributor,
  115. NumCommitsGrowth: numCommitsGrowth,
  116. NumCommitLinesGrowth: numCommitLinesGrowth,
  117. NumContributorsGrowth: numContributorsGrowth,
  118. }
  119. dayBeforeDate := t.AddDate(0, 0, -1).Format("2006-01-02")
  120. repoStatisticsBefore, err := models.GetRepoStatisticByDate(dayBeforeDate, repo.ID)
  121. if err != nil {
  122. log.Error("get data of day before the date failed ", err)
  123. } else {
  124. if len(repoStatisticsBefore) > 0 {
  125. repoStatisticBefore := repoStatisticsBefore[0]
  126. repoStat.NumWatchesAdded = repoStat.NumWatches - repoStatisticBefore.NumWatches
  127. repoStat.NumStarsAdded = repoStat.NumStars - repoStatisticBefore.NumStars
  128. repoStat.NumForksAdded = repoStat.NumForks - repoStatisticBefore.NumForks
  129. repoStat.NumDownloadsAdded = repoStat.NumDownloads - repoStatisticBefore.NumDownloads
  130. repoStat.NumCommentsAdded = repoStat.NumComments - repoStatisticBefore.NumComments
  131. repoStat.NumClosedIssuesAdded = repoStat.NumClosedIssues - repoStatisticBefore.NumClosedIssues
  132. repoStat.NumCommitsAdded = repoStat.NumCommits - repoStatisticBefore.NumCommits
  133. repoStat.NumIssuesAdded = repoStat.NumIssues - repoStatisticBefore.NumIssues
  134. repoStat.NumPullsAdded = repoStat.NumPulls - repoStatisticBefore.NumPulls
  135. repoStat.NumContributorAdded = repoStat.NumContributor - repoStatisticBefore.NumContributor
  136. }
  137. }
  138. day4MonthsAgo := t.AddDate(0, -4, 0)
  139. repoStatisticFourMonthsAgo, err := models.GetOneRepoStatisticBeforeTime(day4MonthsAgo)
  140. if err != nil {
  141. log.Error("Get data of 4 moth ago failed.", err)
  142. } else {
  143. repoStat.NumCommentsGrowth = repoStat.NumComments - repoStatisticFourMonthsAgo.NumComments
  144. repoStat.NumIssuesGrowth = repoStat.NumIssues - repoStatisticFourMonthsAgo.NumIssues
  145. }
  146. if _, err = models.InsertRepoStat(&repoStat); err != nil {
  147. log.Error("InsertRepoStat failed(%s): %v", projectName, err)
  148. log.Error("failed statistic: %s", projectName)
  149. error_projects = append(error_projects, projectName)
  150. continue
  151. }
  152. tempRepoStat := models.RepoStatistic{
  153. RepoID: repoStat.RepoID,
  154. Date: repoStat.Date,
  155. IsMirror: repoStat.IsMirror,
  156. Impact: normalization.GetImpactInitValue(repoStat.NumWatches, repoStat.NumStars, repoStat.NumForks, repoStat.NumDownloads, repoStat.NumComments, repoStat.NumVisits),
  157. Completeness: normalization.GetCompleteInitValue(repoStat.NumClosedIssues, repoStat.NumVersions, repoStat.NumDevMonths, repoStat.DatasetSize, repoStat.NumModels, repoStat.NumWikiViews),
  158. Liveness: normalization.GetLivenessInitValue(repoStat.NumCommits, repoStat.NumIssues, repoStat.NumPulls, repoStat.NumVisits),
  159. ProjectHealth: normalization.GetProjectHealthInitValue(repoStat.IssueFixedRate),
  160. TeamHealth: normalization.GetTeamHealthInitValue(repoStat.NumContributor, repoStat.NumKeyContributor, repoStat.NumContributorsGrowth),
  161. Growth: normalization.GetRepoGrowthInitValue(repoStat.NumCommitLinesGrowth, repoStat.NumIssuesGrowth, repoStat.NumCommitsGrowth, repoStat.NumContributorsGrowth, repoStat.NumCommentsGrowth),
  162. }
  163. reposRadar = append(reposRadar, &tempRepoStat)
  164. if !isInitMinMaxRadar {
  165. if !setting.RadarMap.IgnoreMirrorRepo || (setting.RadarMap.IgnoreMirrorRepo && !tempRepoStat.IsMirror) {
  166. minRepoRadar = tempRepoStat
  167. maxRepoRadar = tempRepoStat
  168. isInitMinMaxRadar = true
  169. }
  170. } else {
  171. if !setting.RadarMap.IgnoreMirrorRepo || (setting.RadarMap.IgnoreMirrorRepo && !tempRepoStat.IsMirror) {
  172. if tempRepoStat.Impact < minRepoRadar.Impact {
  173. minRepoRadar.Impact = tempRepoStat.Impact
  174. }
  175. if tempRepoStat.Impact > maxRepoRadar.Impact {
  176. maxRepoRadar.Impact = tempRepoStat.Impact
  177. }
  178. if tempRepoStat.Completeness < minRepoRadar.Completeness {
  179. minRepoRadar.Completeness = tempRepoStat.Completeness
  180. }
  181. if tempRepoStat.Completeness > maxRepoRadar.Completeness {
  182. maxRepoRadar.Completeness = tempRepoStat.Completeness
  183. }
  184. if tempRepoStat.Liveness < minRepoRadar.Liveness {
  185. minRepoRadar.Liveness = tempRepoStat.Liveness
  186. }
  187. if tempRepoStat.Liveness > maxRepoRadar.Liveness {
  188. maxRepoRadar.Liveness = tempRepoStat.Liveness
  189. }
  190. if tempRepoStat.ProjectHealth < minRepoRadar.ProjectHealth {
  191. minRepoRadar.ProjectHealth = tempRepoStat.ProjectHealth
  192. }
  193. if tempRepoStat.ProjectHealth > maxRepoRadar.ProjectHealth {
  194. maxRepoRadar.ProjectHealth = tempRepoStat.ProjectHealth
  195. }
  196. if tempRepoStat.TeamHealth < minRepoRadar.TeamHealth {
  197. minRepoRadar.TeamHealth = tempRepoStat.TeamHealth
  198. }
  199. if tempRepoStat.TeamHealth > maxRepoRadar.TeamHealth {
  200. maxRepoRadar.TeamHealth = tempRepoStat.TeamHealth
  201. }
  202. if tempRepoStat.Growth < minRepoRadar.Growth {
  203. minRepoRadar.Growth = tempRepoStat.Growth
  204. }
  205. if tempRepoStat.Growth > maxRepoRadar.Growth {
  206. maxRepoRadar.Growth = tempRepoStat.Growth
  207. }
  208. }
  209. }
  210. log.Info("finish statistic: %s", getDistinctProjectName(repo))
  211. }
  212. if len(error_projects) > 0 {
  213. mailer.SendWarnNotifyMail(setting.Warn_Notify_Mails, warnEmailMessage)
  214. }
  215. //radar map
  216. log.Info("begin statistic radar")
  217. for _, radarInit := range reposRadar {
  218. if radarInit.IsMirror && setting.RadarMap.IgnoreMirrorRepo {
  219. radarInit.Impact = 0
  220. radarInit.Completeness = 0
  221. radarInit.Liveness = 0
  222. radarInit.ProjectHealth = 0
  223. radarInit.TeamHealth = 0
  224. radarInit.Growth = 0
  225. radarInit.RadarTotal = 0
  226. } else {
  227. radarInit.Impact = normalization.Normalization(radarInit.Impact, minRepoRadar.Impact, maxRepoRadar.Impact)
  228. radarInit.Completeness = normalization.Normalization(radarInit.Completeness, minRepoRadar.Completeness, maxRepoRadar.Completeness)
  229. radarInit.Liveness = normalization.Normalization(radarInit.Liveness, minRepoRadar.Liveness, maxRepoRadar.Liveness)
  230. radarInit.ProjectHealth = normalization.Normalization(radarInit.ProjectHealth, minRepoRadar.ProjectHealth, maxRepoRadar.ProjectHealth)
  231. radarInit.TeamHealth = normalization.Normalization(radarInit.TeamHealth, minRepoRadar.TeamHealth, maxRepoRadar.TeamHealth)
  232. radarInit.Growth = normalization.Normalization(radarInit.Growth, minRepoRadar.Growth, maxRepoRadar.Growth)
  233. radarInit.RadarTotal = normalization.GetRadarValue(radarInit.Impact, radarInit.Completeness, radarInit.Liveness, radarInit.ProjectHealth, radarInit.TeamHealth, radarInit.Growth)
  234. }
  235. models.UpdateRepoStat(radarInit)
  236. }
  237. log.Info("finish statistic: radar")
  238. }
  239. func getDistinctProjectName(repo *models.Repository) string {
  240. return repo.OwnerName + "/" + repo.Alias
  241. }
  242. func getDatasetSize(repo *models.Repository) (int64, error) {
  243. dataset, err := models.GetDatasetByRepo(repo)
  244. if err != nil {
  245. return 0, err
  246. }
  247. return models.GetAttachmentSizeByDatasetID(dataset.ID)
  248. }
  249. func getStatTime(timeStr string) (string, string) {
  250. t, _ := time.Parse("2006-01-02", timeStr)
  251. timeNumber := t.Unix()
  252. beginTimeNumber := timeNumber - 8*60*60
  253. endTimeNumber := timeNumber + 16*60*60
  254. beginTime := time.Unix(beginTimeNumber, 0).Format(time.RFC3339)
  255. endTime := time.Unix(endTimeNumber, 0).Format(time.RFC3339)
  256. log.Info("%s, %s", beginTime, endTime)
  257. return beginTime, endTime
  258. }
  259. func UpdateRepoVisits(ctx *macaron.Context, repo *models.Repository, date string) error {
  260. beginTime, endTime := getStatTime(date)
  261. var numVisits int
  262. numVisits, err := repository.AppointProjectView(repo.OwnerName, repo.Name, beginTime, endTime)
  263. if err != nil {
  264. log.Error("AppointProjectView failed(%s): %v", getDistinctProjectName(repo), err)
  265. return err
  266. }
  267. repoStat, err := models.GetRepoStatisticByDate(date, repo.ID)
  268. if err != nil {
  269. log.Error("GetRepoStatisticByDate failed(%s): %v", getDistinctProjectName(repo), err)
  270. return err
  271. } else if len(repoStat) != 1 {
  272. log.Error("GetRepoStatisticByDate failed(%s): %v", getDistinctProjectName(repo), err)
  273. return errors.New("not find repo")
  274. }
  275. repoStat[0].NumVisits = int64(numVisits)
  276. if err = models.UpdateRepoStatVisits(repoStat[0]); err != nil {
  277. log.Error("UpdateRepoStatVisits failed(%s): %v", getDistinctProjectName(repo), err)
  278. return err
  279. }
  280. return nil
  281. }