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