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

3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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:"model"`
  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. NumDatasetFile int64 `xorm:"NOT NULL DEFAULT 0" json:"datasetFiles"`
  54. NumCloudbrain int64 `xorm:"NOT NULL DEFAULT 0" json:"cloudbrains"`
  55. NumModelConvert int64 `xorm:"NOT NULL DEFAULT 0" json:"modelConverts"`
  56. NumDatasetFileAdded int64 `xorm:"NOT NULL DEFAULT 0" json:"-"`
  57. NumCloudbrainAdded int64 `xorm:"NOT NULL DEFAULT 0" json:"-"`
  58. NumModelConvertAdded int64 `xorm:"NOT NULL DEFAULT 0" json:"- "`
  59. NumModelsAdded int64 `xorm:"NOT NULL DEFAULT 0" json:"- "`
  60. Impact float64 `xorm:"NOT NULL DEFAULT 0" json:"impact"`
  61. Completeness float64 `xorm:"NOT NULL DEFAULT 0" json:"completeness"`
  62. Liveness float64 `xorm:"NOT NULL DEFAULT 0" json:"liveness"`
  63. ProjectHealth float64 `xorm:"NOT NULL DEFAULT 0" json:"projectHealth"`
  64. TeamHealth float64 `xorm:"NOT NULL DEFAULT 0" json:"teamHealth"`
  65. Growth float64 `xorm:"NOT NULL DEFAULT 0" json:"growth"`
  66. RadarTotal float64 `xorm:"NOT NULL DEFAULT 0" json:"openi"`
  67. CreatedUnix timeutil.TimeStamp `xorm:"INDEX created" json:"-"`
  68. UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated" json:"-"`
  69. }
  70. func (repo *RepoStatistic) DisplayName() string {
  71. if repo.Alias == "" {
  72. return repo.Name
  73. }
  74. return repo.Alias
  75. }
  76. func getOpenIByRepoId(repoId int64) float64 {
  77. repoStatistic := new(RepoStatistic)
  78. has, err := xStatistic.Cols("radar_total").Where("repo_id=?", repoId).Desc("id").Limit(1).Get(repoStatistic)
  79. if !has || err != nil {
  80. return 0
  81. }
  82. return repoStatistic.RadarTotal
  83. }
  84. func DeleteRepoStatDaily(date string) error {
  85. sess := xStatistic.NewSession()
  86. defer sess.Close()
  87. if err := sess.Begin(); err != nil {
  88. return fmt.Errorf("Begin: %v", err)
  89. }
  90. if _, err := sess.Where("date = ?", date).Delete(&RepoStatistic{}); err != nil {
  91. return fmt.Errorf("Delete: %v", err)
  92. }
  93. if err := sess.Commit(); err != nil {
  94. sess.Close()
  95. return fmt.Errorf("Commit: %v", err)
  96. }
  97. sess.Close()
  98. return nil
  99. }
  100. func CountRepoStatByRawSql(sql string) (int64, error) {
  101. return xStatistic.SQL(sql).Count()
  102. }
  103. func GetRepoStatisticByRawSql(sql string) []*RepoStatistic {
  104. repoStatistics := make([]*RepoStatistic, 0)
  105. xStatistic.SQL(sql).Find(&repoStatistics)
  106. return repoStatistics
  107. }
  108. func GetRepoStatLastUpdatedTime(repoId ...string) (string, string, error) {
  109. repoStatistic := new(RepoStatistic)
  110. var has bool
  111. var err error
  112. if len(repoId) == 0 {
  113. has, err = xStatistic.Desc("created_unix").Limit(1).Cols("created_unix", "date").Get(repoStatistic)
  114. } else {
  115. has, err = xStatistic.Where("repo_id=?", repoId[0]).Desc("created_unix").Limit(1).Cols("created_unix", "date").Get(repoStatistic)
  116. }
  117. if err != nil {
  118. return "", "", err
  119. } else {
  120. if has {
  121. return repoStatistic.CreatedUnix.Format("2006-01-02 15:04:05"), repoStatistic.Date, nil
  122. } else {
  123. return "", "", fmt.Errorf("Can not get the latest record.")
  124. }
  125. }
  126. }
  127. func GetRepoStatisticByDateAndRepoId(date string, repoId int64) (*RepoStatistic, error) {
  128. repoStatistic := new(RepoStatistic)
  129. has, err := xStatistic.Where("date=? and repo_id=?", date, repoId).Get(repoStatistic)
  130. if err != nil {
  131. return nil, err
  132. } else {
  133. if has {
  134. return repoStatistic, nil
  135. } else {
  136. return nil, fmt.Errorf("The num of return records is 0.")
  137. }
  138. }
  139. }
  140. func GetRepoStatisticByDate(date string, repoId int64) ([]*RepoStatistic, error) {
  141. repoStatistics := make([]*RepoStatistic, 0)
  142. err := xStatistic.Where("date = ? and repo_id=?", date, repoId).Find(&repoStatistics)
  143. return repoStatistics, err
  144. }
  145. func GetOneRepoStatisticBeforeTime(time time.Time) (*RepoStatistic, error) {
  146. repoStatistics := make([]*RepoStatistic, 0)
  147. err := xStatistic.Where("created_unix >= ?", time.Unix()).OrderBy("created_unix").Limit(1).Find(&repoStatistics)
  148. if err != nil {
  149. return nil, err
  150. } else {
  151. if len(repoStatistics) == 0 {
  152. return nil, fmt.Errorf("the repo statistic record count is 0")
  153. } else {
  154. return repoStatistics[0], nil
  155. }
  156. }
  157. }
  158. func InsertRepoStat(repoStat *RepoStatistic) (int64, error) {
  159. return xStatistic.Insert(repoStat)
  160. }
  161. func RestoreRepoStatFork(numForks int64, repoId int64) error {
  162. sql := "update repo_statistic set num_forks=? where repo_id=?"
  163. _, err := xStatistic.Exec(sql, numForks, repoId)
  164. return err
  165. }
  166. func UpdateRepoStat(repoStat *RepoStatistic) error {
  167. sql := "update repo_statistic set impact=?,completeness=?,liveness=?,project_health=?,team_health=?,growth=?,radar_total=? where repo_id=? and date=?"
  168. _, err := xStatistic.Exec(sql, repoStat.Impact, repoStat.Completeness, repoStat.Liveness, repoStat.ProjectHealth, repoStat.TeamHealth, repoStat.Growth, repoStat.RadarTotal, repoStat.RepoID, repoStat.Date)
  169. return err
  170. }
  171. func UpdateRepoStatVisits(repoStat *RepoStatistic) error {
  172. sql := "update repo_statistic set num_visits=? where repo_id=? and date=?"
  173. _, err := xStatistic.Exec(sql, repoStat.NumVisits, repoStat.RepoID, repoStat.Date)
  174. return err
  175. }
  176. func SumRepoStatColumn(begin, end time.Time, repoId int64, columnName string) (int64, error) {
  177. res, err := xStatistic.Where("created_unix <= ? and created_unix >= ? and repo_id = ? ", end.Unix(), begin.Unix(), repoId).Sum(&RepoStatistic{}, columnName)
  178. if err != nil {
  179. return 0, err
  180. }
  181. return int64(res), nil
  182. }
  183. func SumLastMonthNumVisits(repoId int64) (int64, error) {
  184. end := time.Now()
  185. begin := end.AddDate(0, 0, -30)
  186. return SumRepoStatColumn(begin, end, repoId, "num_visits")
  187. }
  188. func SumLastFourMonthNumCommits(repoId int64) (int64, error) {
  189. end := time.Now()
  190. begin := end.AddDate(0, 0, -120)
  191. return SumRepoStatColumn(begin, end, repoId, "num_commits_added")
  192. }