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

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