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

3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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"`
  10. RepoID int64 `xorm:"unique(s) NOT NULL"`
  11. Name string `xorm:"INDEX"`
  12. IsPrivate bool
  13. Date string `xorm:"unique(s) NOT NULL"`
  14. NumWatches int64 `xorm:"NOT NULL DEFAULT 0"`
  15. NumWatchesAdded int64 `xorm:"NOT NULL DEFAULT 0"`
  16. NumStars int64 `xorm:"NOT NULL DEFAULT 0"`
  17. NumStarsAdded int64 `xorm:"NOT NULL DEFAULT 0"`
  18. NumForks int64 `xorm:"NOT NULL DEFAULT 0"`
  19. NumForksAdded int64 `xorm:"NOT NULL DEFAULT 0"`
  20. NumDownloads int64 `xorm:"NOT NULL DEFAULT 0"`
  21. NumDownloadsAdded int64 `xorm:"NOT NULL DEFAULT 0"`
  22. NumComments int64 `xorm:"NOT NULL DEFAULT 0"`
  23. NumCommentsAdded int64 `xorm:"NOT NULL DEFAULT 0"`
  24. NumVisits int64 `xorm:"NOT NULL DEFAULT 0"`
  25. NumClosedIssues int64 `xorm:"NOT NULL DEFAULT 0"`
  26. NumClosedIssuesAdded int64 `xorm:"NOT NULL DEFAULT 0"`
  27. NumVersions int64 `xorm:"NOT NULL DEFAULT 0"`
  28. NumDevMonths int64 `xorm:"NOT NULL DEFAULT 0"`
  29. RepoSize int64 `xorm:"NOT NULL DEFAULT 0"`
  30. DatasetSize int64 `xorm:"NOT NULL DEFAULT 0"`
  31. NumModels int64 `xorm:"NOT NULL DEFAULT 0"`
  32. NumWikiViews int64 `xorm:"NOT NULL DEFAULT 0"`
  33. NumCommits int64 `xorm:"NOT NULL DEFAULT 0"`
  34. NumCommitsAdded int64 `xorm:"NOT NULL DEFAULT 0"`
  35. NumIssues int64 `xorm:"NOT NULL DEFAULT 0"`
  36. NumIssuesAdded int64 `xorm:"NOT NULL DEFAULT 0"`
  37. NumPulls int64 `xorm:"NOT NULL DEFAULT 0"`
  38. NumPullsAdded int64 `xorm:"NOT NULL DEFAULT 0"`
  39. IssueFixedRate float32 `xorm:"NOT NULL"`
  40. NumContributor int64 `xorm:"NOT NULL DEFAULT 0"`
  41. NumContributorAdded int64 `xorm:"NOT NULL DEFAULT 0"`
  42. NumKeyContributor int64 `xorm:"NOT NULL DEFAULT 0"`
  43. NumContributorsGrowth int64 `xorm:"NOT NULL DEFAULT 0"`
  44. NumCommitsGrowth int64 `xorm:"NOT NULL DEFAULT 0"`
  45. NumCommitLinesGrowth int64 `xorm:"NOT NULL DEFAULT 0"`
  46. NumIssuesGrowth int64 `xorm:"NOT NULL DEFAULT 0"`
  47. NumCommentsGrowth int64 `xorm:"NOT NULL DEFAULT 0"`
  48. Impact float64 `xorm:"NOT NULL DEFAULT 0"`
  49. Completeness float64 `xorm:"NOT NULL DEFAULT 0"`
  50. Liveness float64 `xorm:"NOT NULL DEFAULT 0"`
  51. ProjectHealth float64 `xorm:"NOT NULL DEFAULT 0"`
  52. TeamHealth float64 `xorm:"NOT NULL DEFAULT 0"`
  53. Growth float64 `xorm:"NOT NULL DEFAULT 0"`
  54. RadarTotal float64 `xorm:"NOT NULL DEFAULT 0"`
  55. CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
  56. UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
  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 GetRepoStatisticByDate(date string) ([]*RepoStatistic, error) {
  75. repoStatistics := make([]*RepoStatistic, 0)
  76. err := xStatistic.Where("date = ?", date).Find(&repoStatistics)
  77. return repoStatistics, err
  78. }
  79. func GetOneRepoStatisticBeforeTime(time time.Time) (*RepoStatistic, error) {
  80. repoStatistics := make([]*RepoStatistic, 0)
  81. err := xStatistic.Where("created_unix >= ?", time.Unix()).OrderBy("created_unix").Limit(1).Find(&repoStatistics)
  82. if err != nil {
  83. return nil, err
  84. } else {
  85. if len(repoStatistics) == 0 {
  86. return nil, fmt.Errorf("the repo statistic record count is 0")
  87. } else {
  88. return repoStatistics[0], nil
  89. }
  90. }
  91. }
  92. func InsertRepoStat(repoStat *RepoStatistic) (int64, error) {
  93. return xStatistic.Insert(repoStat)
  94. }
  95. func UpdateRepoStat(repoStat *RepoStatistic) error {
  96. sql := "update repo_statistic set impact=?,completeness=?,liveness=?,project_health=?,team_health=?,growth=?,radar_total=? where repo_id=? and date=?"
  97. _, err := xStatistic.Exec(sql, repoStat.Impact, repoStat.Completeness, repoStat.Liveness, repoStat.ProjectHealth, repoStat.TeamHealth, repoStat.Growth, repoStat.RadarTotal, repoStat.RepoID, repoStat.Date)
  98. return err
  99. }