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.9 kB

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