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

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