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 6.6 kB

3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package models
  2. import (
  3. "fmt"
  4. "time"
  5. "code.gitea.io/gitea/modules/timeutil"
  6. )
  7. // RepoStatistic statistic info of all repository
  8. type RepoStatistic struct {
  9. ID int64 `xorm:"pk autoincr" json:"-"`
  10. RepoID int64 `xorm:"unique(s) NOT NULL" json:"repo_id"`
  11. Name string `xorm:"INDEX" json:"name"`
  12. IsPrivate bool `json:"isPrivate"`
  13. Date string `xorm:"unique(s) NOT NULL" json:"date"`
  14. NumWatches int64 `xorm:"NOT NULL DEFAULT 0" json:"watch"`
  15. NumWatchesAdded int64 `xorm:"NOT NULL DEFAULT 0" json:"-"`
  16. NumStars int64 `xorm:"NOT NULL DEFAULT 0" json:"star"`
  17. NumStarsAdded int64 `xorm:"NOT NULL DEFAULT 0" json:"-"`
  18. NumForks int64 `xorm:"NOT NULL DEFAULT 0" json:"fork"`
  19. NumForksAdded int64 `xorm:"NOT NULL DEFAULT 0" json:"-"`
  20. NumDownloads int64 `xorm:"NOT NULL DEFAULT 0" json:"download"`
  21. NumDownloadsAdded int64 `xorm:"NOT NULL DEFAULT 0" json:"-"`
  22. NumComments int64 `xorm:"NOT NULL DEFAULT 0" json:"comment"`
  23. NumCommentsAdded int64 `xorm:"NOT NULL DEFAULT 0" json:"-"`
  24. NumVisits int64 `xorm:"NOT NULL DEFAULT 0" json:"view"`
  25. NumClosedIssues int64 `xorm:"NOT NULL DEFAULT 0" json:"issueClosed"`
  26. NumClosedIssuesAdded int64 `xorm:"NOT NULL DEFAULT 0" json:"-"`
  27. NumVersions int64 `xorm:"NOT NULL DEFAULT 0" json:"-"`
  28. NumDevMonths int64 `xorm:"NOT NULL DEFAULT 0" json:"-"`
  29. RepoSize int64 `xorm:"NOT NULL DEFAULT 0" json:"-"`
  30. DatasetSize int64 `xorm:"NOT NULL DEFAULT 0" json:"-"`
  31. NumModels int64 `xorm:"NOT NULL DEFAULT 0" json:"-"`
  32. NumWikiViews int64 `xorm:"NOT NULL DEFAULT 0" json:"-"`
  33. NumCommits int64 `xorm:"NOT NULL DEFAULT 0" json:"commit"`
  34. NumCommitsAdded int64 `xorm:"NOT NULL DEFAULT 0" json:"-"`
  35. NumIssues int64 `xorm:"NOT NULL DEFAULT 0" json:"issue"`
  36. NumIssuesAdded int64 `xorm:"NOT NULL DEFAULT 0" json:"-"`
  37. NumPulls int64 `xorm:"NOT NULL DEFAULT 0" json:"pr"`
  38. NumPullsAdded int64 `xorm:"NOT NULL DEFAULT 0" json:"-"`
  39. IssueFixedRate float32 `xorm:"NOT NULL" json:"issueClosedRatio"`
  40. NumContributor int64 `xorm:"NOT NULL DEFAULT 0" json:"contributor"`
  41. NumContributorAdded int64 `xorm:"NOT NULL DEFAULT 0" json:"-"`
  42. NumKeyContributor int64 `xorm:"NOT NULL DEFAULT 0" json:"-"`
  43. NumContributorsGrowth int64 `xorm:"NOT NULL DEFAULT 0" json:"-"`
  44. NumCommitsGrowth int64 `xorm:"NOT NULL DEFAULT 0" json:"-"`
  45. NumCommitLinesGrowth int64 `xorm:"NOT NULL DEFAULT 0" json:"-"`
  46. NumIssuesGrowth int64 `xorm:"NOT NULL DEFAULT 0" json:"-"`
  47. NumCommentsGrowth int64 `xorm:"NOT NULL DEFAULT 0" json:"-"`
  48. Impact float64 `xorm:"NOT NULL DEFAULT 0" json:"impact"`
  49. Completeness float64 `xorm:"NOT NULL DEFAULT 0" json:"completeness"`
  50. Liveness float64 `xorm:"NOT NULL DEFAULT 0" json:"liveness"`
  51. ProjectHealth float64 `xorm:"NOT NULL DEFAULT 0" json:"projectHealth"`
  52. TeamHealth float64 `xorm:"NOT NULL DEFAULT 0" json:"teamHealth"`
  53. Growth float64 `xorm:"NOT NULL DEFAULT 0" json:"growth"`
  54. RadarTotal float64 `xorm:"NOT NULL DEFAULT 0" json:"openi"`
  55. CreatedUnix timeutil.TimeStamp `xorm:"INDEX created" json:"-"`
  56. UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated" json:"-"`
  57. }
  58. func DeleteRepoStatDaily(date string) error {
  59. sess := xStatistic.NewSession()
  60. defer sess.Close()
  61. if err := sess.Begin(); err != nil {
  62. return fmt.Errorf("Begin: %v", err)
  63. }
  64. if _, err := sess.Where("date = ?", date).Delete(&RepoStatistic{}); err != nil {
  65. return fmt.Errorf("Delete: %v", err)
  66. }
  67. if err := sess.Commit(); err != nil {
  68. sess.Close()
  69. return fmt.Errorf("Commit: %v", err)
  70. }
  71. sess.Close()
  72. return nil
  73. }
  74. func CountRepoStatByRawSql(sql string) (int64, error) {
  75. return xStatistic.SQL(sql).Count()
  76. }
  77. func GetRepoStatisticByRawSql(sql string) []*RepoStatistic {
  78. repoStatistics := make([]*RepoStatistic, 0)
  79. xStatistic.SQL(sql).Find(&repoStatistics)
  80. return repoStatistics
  81. }
  82. func GetRepoStatLastUpdatedTime(repoId ...string) (string, string, error) {
  83. repoStatistic := new(RepoStatistic)
  84. var has bool
  85. var err error
  86. if len(repoId) == 0 {
  87. has, err = xStatistic.Desc("created_unix").Limit(1).Cols("created_unix", "date").Get(repoStatistic)
  88. } else {
  89. has, err = xStatistic.Where("repo_id=?", repoId[0]).Desc("created_unix").Limit(1).Cols("created_unix", "date").Get(repoStatistic)
  90. }
  91. if err != nil {
  92. return "", "", err
  93. } else {
  94. if has {
  95. return repoStatistic.CreatedUnix.Format("2006-01-02 15:04:05"), repoStatistic.Date, nil
  96. } else {
  97. return "", "", fmt.Errorf("Can not get the latest record.")
  98. }
  99. }
  100. }
  101. func GetRepoStatisticByDateAndRepoId(date string, repoId int64) (*RepoStatistic, error) {
  102. repoStatistic := new(RepoStatistic)
  103. has, err := xStatistic.Where("date=? and repo_id=?", date, repoId).Get(repoStatistic)
  104. if err != nil {
  105. return nil, err
  106. } else {
  107. if has {
  108. return repoStatistic, nil
  109. } else {
  110. return nil, fmt.Errorf("The num of return records is 0.")
  111. }
  112. }
  113. }
  114. func GetRepoStatisticByDate(date string, repoId int64) ([]*RepoStatistic, error) {
  115. repoStatistics := make([]*RepoStatistic, 0)
  116. err := xStatistic.Where("date = ? and repo_id=?", date, repoId).Find(&repoStatistics)
  117. return repoStatistics, err
  118. }
  119. func GetOneRepoStatisticBeforeTime(time time.Time) (*RepoStatistic, error) {
  120. repoStatistics := make([]*RepoStatistic, 0)
  121. err := xStatistic.Where("created_unix >= ?", time.Unix()).OrderBy("created_unix").Limit(1).Find(&repoStatistics)
  122. if err != nil {
  123. return nil, err
  124. } else {
  125. if len(repoStatistics) == 0 {
  126. return nil, fmt.Errorf("the repo statistic record count is 0")
  127. } else {
  128. return repoStatistics[0], nil
  129. }
  130. }
  131. }
  132. func InsertRepoStat(repoStat *RepoStatistic) (int64, error) {
  133. return xStatistic.Insert(repoStat)
  134. }
  135. func RestoreRepoStatFork(numForks int64, repoId int64) error {
  136. sql := "update repo_statistic set num_forks=? where repo_id=?"
  137. _, err := xStatistic.Exec(sql, numForks, repoId)
  138. return err
  139. }
  140. func UpdateRepoStat(repoStat *RepoStatistic) error {
  141. sql := "update repo_statistic set impact=?,completeness=?,liveness=?,project_health=?,team_health=?,growth=?,radar_total=? where repo_id=? and date=?"
  142. _, err := xStatistic.Exec(sql, repoStat.Impact, repoStat.Completeness, repoStat.Liveness, repoStat.ProjectHealth, repoStat.TeamHealth, repoStat.Growth, repoStat.RadarTotal, repoStat.RepoID, repoStat.Date)
  143. return err
  144. }