gitea源码

repo.go 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2017 The Gitea Authors. All rights reserved.
  3. // SPDX-License-Identifier: MIT
  4. package models
  5. import (
  6. "context"
  7. "strconv"
  8. _ "image/jpeg" // Needed for jpeg support
  9. "code.gitea.io/gitea/models/db"
  10. issues_model "code.gitea.io/gitea/models/issues"
  11. repo_model "code.gitea.io/gitea/models/repo"
  12. "code.gitea.io/gitea/models/unit"
  13. user_model "code.gitea.io/gitea/models/user"
  14. "code.gitea.io/gitea/modules/log"
  15. "xorm.io/builder"
  16. )
  17. // Init initialize model
  18. func Init(ctx context.Context) error {
  19. return unit.LoadUnitConfig()
  20. }
  21. type repoChecker struct {
  22. querySQL func(ctx context.Context) ([]int64, error)
  23. correctSQL func(ctx context.Context, id int64) error
  24. desc string
  25. }
  26. func repoStatsCheck(ctx context.Context, checker *repoChecker) {
  27. results, err := checker.querySQL(ctx)
  28. if err != nil {
  29. log.Error("Select %s: %v", checker.desc, err)
  30. return
  31. }
  32. for _, id := range results {
  33. select {
  34. case <-ctx.Done():
  35. log.Warn("CheckRepoStats: Cancelled before checking %s for with id=%d", checker.desc, id)
  36. return
  37. default:
  38. }
  39. log.Trace("Updating %s: %d", checker.desc, id)
  40. err = checker.correctSQL(ctx, id)
  41. if err != nil {
  42. log.Error("Update %s[%d]: %v", checker.desc, id, err)
  43. }
  44. }
  45. }
  46. func StatsCorrectSQL(ctx context.Context, sql any, ids ...any) error {
  47. args := []any{sql}
  48. args = append(args, ids...)
  49. _, err := db.GetEngine(ctx).Exec(args...)
  50. return err
  51. }
  52. func repoStatsCorrectNumWatches(ctx context.Context, id int64) error {
  53. return StatsCorrectSQL(ctx, "UPDATE `repository` SET num_watches=(SELECT COUNT(*) FROM `watch` WHERE repo_id=? AND mode<>2) WHERE id=?", id, id)
  54. }
  55. func repoStatsCorrectNumStars(ctx context.Context, id int64) error {
  56. return StatsCorrectSQL(ctx, "UPDATE `repository` SET num_stars=(SELECT COUNT(*) FROM `star` WHERE repo_id=?) WHERE id=?", id, id)
  57. }
  58. func labelStatsCorrectNumIssues(ctx context.Context, id int64) error {
  59. return StatsCorrectSQL(ctx, "UPDATE `label` SET num_issues=(SELECT COUNT(*) FROM `issue_label` WHERE label_id=?) WHERE id=?", id, id)
  60. }
  61. func labelStatsCorrectNumIssuesRepo(ctx context.Context, id int64) error {
  62. _, err := db.GetEngine(ctx).Exec("UPDATE `label` SET num_issues=(SELECT COUNT(*) FROM `issue_label` WHERE label_id=id) WHERE repo_id=?", id)
  63. return err
  64. }
  65. func labelStatsCorrectNumClosedIssues(ctx context.Context, id int64) error {
  66. _, err := db.GetEngine(ctx).Exec("UPDATE `label` SET num_closed_issues=(SELECT COUNT(*) FROM `issue_label`,`issue` WHERE `issue_label`.label_id=`label`.id AND `issue_label`.issue_id=`issue`.id AND `issue`.is_closed=?) WHERE `label`.id=?", true, id)
  67. return err
  68. }
  69. func labelStatsCorrectNumClosedIssuesRepo(ctx context.Context, id int64) error {
  70. _, err := db.GetEngine(ctx).Exec("UPDATE `label` SET num_closed_issues=(SELECT COUNT(*) FROM `issue_label`,`issue` WHERE `issue_label`.label_id=`label`.id AND `issue_label`.issue_id=`issue`.id AND `issue`.is_closed=?) WHERE `label`.repo_id=?", true, id)
  71. return err
  72. }
  73. var milestoneStatsQueryNumIssues = "SELECT `milestone`.id FROM `milestone` WHERE `milestone`.num_closed_issues!=(SELECT COUNT(*) FROM `issue` WHERE `issue`.milestone_id=`milestone`.id AND `issue`.is_closed=?) OR `milestone`.num_issues!=(SELECT COUNT(*) FROM `issue` WHERE `issue`.milestone_id=`milestone`.id)"
  74. func milestoneStatsCorrectNumIssuesRepo(ctx context.Context, id int64) error {
  75. e := db.GetEngine(ctx)
  76. results, err := e.Query(milestoneStatsQueryNumIssues+" AND `milestone`.repo_id = ?", true, id)
  77. if err != nil {
  78. return err
  79. }
  80. for _, result := range results {
  81. id, _ := strconv.ParseInt(string(result["id"]), 10, 64)
  82. err = issues_model.UpdateMilestoneCounters(ctx, id)
  83. if err != nil {
  84. return err
  85. }
  86. }
  87. return nil
  88. }
  89. func userStatsCorrectNumRepos(ctx context.Context, id int64) error {
  90. return StatsCorrectSQL(ctx, "UPDATE `user` SET num_repos=(SELECT COUNT(*) FROM `repository` WHERE owner_id=?) WHERE id=?", id, id)
  91. }
  92. func repoStatsCorrectIssueNumComments(ctx context.Context, id int64) error {
  93. return StatsCorrectSQL(ctx, issues_model.UpdateIssueNumCommentsBuilder(id))
  94. }
  95. func repoStatsCorrectNumIssues(ctx context.Context, id int64) error {
  96. return repo_model.UpdateRepoIssueNumbers(ctx, id, false, false)
  97. }
  98. func repoStatsCorrectNumPulls(ctx context.Context, id int64) error {
  99. return repo_model.UpdateRepoIssueNumbers(ctx, id, true, false)
  100. }
  101. func repoStatsCorrectNumClosedIssues(ctx context.Context, id int64) error {
  102. return repo_model.UpdateRepoIssueNumbers(ctx, id, false, true)
  103. }
  104. func repoStatsCorrectNumClosedPulls(ctx context.Context, id int64) error {
  105. return repo_model.UpdateRepoIssueNumbers(ctx, id, true, true)
  106. }
  107. // statsQuery returns a function that queries the database for a list of IDs
  108. // sql could be a string or a *builder.Builder
  109. func statsQuery(sql any, args ...any) func(context.Context) ([]int64, error) {
  110. return func(ctx context.Context) ([]int64, error) {
  111. var ids []int64
  112. return ids, db.GetEngine(ctx).SQL(sql, args...).Find(&ids)
  113. }
  114. }
  115. // CheckRepoStats checks the repository stats
  116. func CheckRepoStats(ctx context.Context) error {
  117. log.Trace("Doing: CheckRepoStats")
  118. checkers := []*repoChecker{
  119. // Repository.NumWatches
  120. {
  121. statsQuery("SELECT repo.id FROM `repository` repo WHERE repo.num_watches!=(SELECT COUNT(*) FROM `watch` WHERE repo_id=repo.id AND mode<>2)"),
  122. repoStatsCorrectNumWatches,
  123. "repository count 'num_watches'",
  124. },
  125. // Repository.NumStars
  126. {
  127. statsQuery("SELECT repo.id FROM `repository` repo WHERE repo.num_stars!=(SELECT COUNT(*) FROM `star` WHERE repo_id=repo.id)"),
  128. repoStatsCorrectNumStars,
  129. "repository count 'num_stars'",
  130. },
  131. // Repository.NumIssues
  132. {
  133. statsQuery("SELECT repo.id FROM `repository` repo WHERE repo.num_issues!=(SELECT COUNT(*) FROM `issue` WHERE repo_id=repo.id AND is_pull=?)", false),
  134. repoStatsCorrectNumIssues,
  135. "repository count 'num_issues'",
  136. },
  137. // Repository.NumClosedIssues
  138. {
  139. statsQuery("SELECT repo.id FROM `repository` repo WHERE repo.num_closed_issues!=(SELECT COUNT(*) FROM `issue` WHERE repo_id=repo.id AND is_closed=? AND is_pull=?)", true, false),
  140. repoStatsCorrectNumClosedIssues,
  141. "repository count 'num_closed_issues'",
  142. },
  143. // Repository.NumPulls
  144. {
  145. statsQuery("SELECT repo.id FROM `repository` repo WHERE repo.num_pulls!=(SELECT COUNT(*) FROM `issue` WHERE repo_id=repo.id AND is_pull=?)", true),
  146. repoStatsCorrectNumPulls,
  147. "repository count 'num_pulls'",
  148. },
  149. // Repository.NumClosedPulls
  150. {
  151. statsQuery("SELECT repo.id FROM `repository` repo WHERE repo.num_closed_pulls!=(SELECT COUNT(*) FROM `issue` WHERE repo_id=repo.id AND is_closed=? AND is_pull=?)", true, true),
  152. repoStatsCorrectNumClosedPulls,
  153. "repository count 'num_closed_pulls'",
  154. },
  155. // Label.NumIssues
  156. {
  157. statsQuery("SELECT label.id FROM `label` WHERE label.num_issues!=(SELECT COUNT(*) FROM `issue_label` WHERE label_id=label.id)"),
  158. labelStatsCorrectNumIssues,
  159. "label count 'num_issues'",
  160. },
  161. // Label.NumClosedIssues
  162. {
  163. statsQuery("SELECT `label`.id FROM `label` WHERE `label`.num_closed_issues!=(SELECT COUNT(*) FROM `issue_label`,`issue` WHERE `issue_label`.label_id=`label`.id AND `issue_label`.issue_id=`issue`.id AND `issue`.is_closed=?)", true),
  164. labelStatsCorrectNumClosedIssues,
  165. "label count 'num_closed_issues'",
  166. },
  167. // Milestone.Num{,Closed}Issues
  168. {
  169. statsQuery(milestoneStatsQueryNumIssues, true),
  170. issues_model.UpdateMilestoneCounters,
  171. "milestone count 'num_closed_issues' and 'num_issues'",
  172. },
  173. // User.NumRepos
  174. {
  175. statsQuery("SELECT `user`.id FROM `user` WHERE `user`.num_repos!=(SELECT COUNT(*) FROM `repository` WHERE owner_id=`user`.id)"),
  176. userStatsCorrectNumRepos,
  177. "user count 'num_repos'",
  178. },
  179. // Issue.NumComments
  180. {
  181. statsQuery(builder.Select("`issue`.id").From("`issue`").Where(
  182. builder.Neq{
  183. "`issue`.num_comments": builder.Select("COUNT(*)").From("`comment`").Where(
  184. builder.Expr("issue_id = `issue`.id").And(
  185. builder.In("type", issues_model.ConversationCountedCommentType()),
  186. ),
  187. ),
  188. },
  189. ),
  190. ),
  191. repoStatsCorrectIssueNumComments,
  192. "issue count 'num_comments'",
  193. },
  194. }
  195. for _, checker := range checkers {
  196. select {
  197. case <-ctx.Done():
  198. log.Warn("CheckRepoStats: Cancelled before %s", checker.desc)
  199. return db.ErrCancelledf("before checking %s", checker.desc)
  200. default:
  201. repoStatsCheck(ctx, checker)
  202. }
  203. }
  204. // FIXME: use checker when stop supporting old fork repo format.
  205. // ***** START: Repository.NumForks *****
  206. e := db.GetEngine(ctx)
  207. results, err := e.Query("SELECT repo.id FROM `repository` repo WHERE repo.num_forks!=(SELECT COUNT(*) FROM `repository` WHERE fork_id=repo.id)")
  208. if err != nil {
  209. log.Error("Select repository count 'num_forks': %v", err)
  210. } else {
  211. for _, result := range results {
  212. id, _ := strconv.ParseInt(string(result["id"]), 10, 64)
  213. select {
  214. case <-ctx.Done():
  215. log.Warn("CheckRepoStats: Cancelled")
  216. return db.ErrCancelledf("during repository count 'num_fork' for repo ID %d", id)
  217. default:
  218. }
  219. log.Trace("Updating repository count 'num_forks': %d", id)
  220. repo, err := repo_model.GetRepositoryByID(ctx, id)
  221. if err != nil {
  222. log.Error("repo_model.GetRepositoryByID[%d]: %v", id, err)
  223. continue
  224. }
  225. _, err = e.SQL("SELECT COUNT(*) FROM `repository` WHERE fork_id=?", repo.ID).Get(&repo.NumForks)
  226. if err != nil {
  227. log.Error("Select count of forks[%d]: %v", repo.ID, err)
  228. continue
  229. }
  230. if _, err = e.ID(repo.ID).Cols("num_forks").Update(repo); err != nil {
  231. log.Error("UpdateRepository[%d]: %v", id, err)
  232. continue
  233. }
  234. }
  235. }
  236. // ***** END: Repository.NumForks *****
  237. return nil
  238. }
  239. func UpdateRepoStats(ctx context.Context, id int64) error {
  240. var err error
  241. for _, f := range []func(ctx context.Context, id int64) error{
  242. repoStatsCorrectNumWatches,
  243. repoStatsCorrectNumStars,
  244. repoStatsCorrectNumIssues,
  245. repoStatsCorrectNumPulls,
  246. repoStatsCorrectNumClosedIssues,
  247. repoStatsCorrectNumClosedPulls,
  248. labelStatsCorrectNumIssuesRepo,
  249. labelStatsCorrectNumClosedIssuesRepo,
  250. milestoneStatsCorrectNumIssuesRepo,
  251. } {
  252. err = f(ctx, id)
  253. if err != nil {
  254. return err
  255. }
  256. }
  257. return nil
  258. }
  259. func updateUserStarNumbers(ctx context.Context, users []user_model.User) error {
  260. return db.WithTx(ctx, func(ctx context.Context) error {
  261. for _, user := range users {
  262. if _, err := db.Exec(ctx, "UPDATE `user` SET num_stars=(SELECT COUNT(*) FROM `star` WHERE uid=?) WHERE id=?", user.ID, user.ID); err != nil {
  263. return err
  264. }
  265. }
  266. return nil
  267. })
  268. }
  269. // DoctorUserStarNum recalculate Stars number for all user
  270. func DoctorUserStarNum(ctx context.Context) (err error) {
  271. const batchSize = 100
  272. for start := 0; ; start += batchSize {
  273. users := make([]user_model.User, 0, batchSize)
  274. if err = db.GetEngine(ctx).Limit(batchSize, start).Where("type = ?", 0).Cols("id").Find(&users); err != nil {
  275. return err
  276. }
  277. if len(users) == 0 {
  278. break
  279. }
  280. if err = updateUserStarNumbers(ctx, users); err != nil {
  281. return err
  282. }
  283. }
  284. log.Debug("recalculate Stars number for all user finished")
  285. return err
  286. }