You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
2.8 KiB

  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package repo
  5. import (
  6. "time"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/base"
  9. "code.gitea.io/gitea/modules/context"
  10. )
  11. const (
  12. tplActivity base.TplName = "repo/activity"
  13. )
  14. // Activity render the page to show repository latest changes
  15. func Activity(ctx *context.Context) {
  16. ctx.Data["Title"] = ctx.Tr("repo.activity")
  17. ctx.Data["PageIsActivity"] = true
  18. ctx.Data["Period"] = ctx.Params("period")
  19. timeUntil := time.Now()
  20. var timeFrom time.Time
  21. switch ctx.Data["Period"] {
  22. case "daily":
  23. timeFrom = timeUntil.Add(-time.Hour * 24)
  24. case "halfweekly":
  25. timeFrom = timeUntil.Add(-time.Hour * 72)
  26. case "weekly":
  27. timeFrom = timeUntil.Add(-time.Hour * 168)
  28. case "monthly":
  29. timeFrom = timeUntil.AddDate(0, -1, 0)
  30. case "quarterly":
  31. timeFrom = timeUntil.AddDate(0, -3, 0)
  32. case "semiyearly":
  33. timeFrom = timeUntil.AddDate(0, -6, 0)
  34. case "yearly":
  35. timeFrom = timeUntil.AddDate(-1, 0, 0)
  36. default:
  37. ctx.Data["Period"] = "weekly"
  38. timeFrom = timeUntil.Add(-time.Hour * 168)
  39. }
  40. ctx.Data["DateFrom"] = timeFrom.Format("January 2, 2006")
  41. ctx.Data["DateUntil"] = timeUntil.Format("January 2, 2006")
  42. ctx.Data["PeriodText"] = ctx.Tr("repo.activity.period." + ctx.Data["Period"].(string))
  43. var err error
  44. if ctx.Data["Activity"], err = models.GetActivityStats(ctx.Repo.Repository, timeFrom,
  45. ctx.Repo.CanRead(models.UnitTypeReleases),
  46. ctx.Repo.CanRead(models.UnitTypeIssues),
  47. ctx.Repo.CanRead(models.UnitTypePullRequests),
  48. ctx.Repo.CanRead(models.UnitTypeCode)); err != nil {
  49. ctx.ServerError("GetActivityStats", err)
  50. return
  51. }
  52. if ctx.Data["ActivityTopAuthors"], err = models.GetActivityStatsTopAuthors(ctx.Repo.Repository, timeFrom, 10); err != nil {
  53. ctx.ServerError("GetActivityStatsTopAuthors", err)
  54. return
  55. }
  56. ctx.HTML(200, tplActivity)
  57. }
  58. // ActivityAuthors renders JSON with top commit authors for given time period over all branches
  59. func ActivityAuthors(ctx *context.Context) {
  60. timeUntil := time.Now()
  61. var timeFrom time.Time
  62. switch ctx.Params("period") {
  63. case "daily":
  64. timeFrom = timeUntil.Add(-time.Hour * 24)
  65. case "halfweekly":
  66. timeFrom = timeUntil.Add(-time.Hour * 72)
  67. case "weekly":
  68. timeFrom = timeUntil.Add(-time.Hour * 168)
  69. case "monthly":
  70. timeFrom = timeUntil.AddDate(0, -1, 0)
  71. case "quarterly":
  72. timeFrom = timeUntil.AddDate(0, -3, 0)
  73. case "semiyearly":
  74. timeFrom = timeUntil.AddDate(0, -6, 0)
  75. case "yearly":
  76. timeFrom = timeUntil.AddDate(-1, 0, 0)
  77. default:
  78. timeFrom = timeUntil.Add(-time.Hour * 168)
  79. }
  80. var err error
  81. authors, err := models.GetActivityStatsTopAuthors(ctx.Repo.Repository, timeFrom, 10)
  82. if err != nil {
  83. ctx.ServerError("GetActivityStatsTopAuthors", err)
  84. return
  85. }
  86. ctx.JSON(200, authors)
  87. }