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.

97 lines
2.6 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. ctx.HTML(200, tplActivity)
  53. }
  54. // ActivityAuthors renders JSON with top commit authors for given time period over all branches
  55. func ActivityAuthors(ctx *context.Context) {
  56. timeUntil := time.Now()
  57. var timeFrom time.Time
  58. switch ctx.Params("period") {
  59. case "daily":
  60. timeFrom = timeUntil.Add(-time.Hour * 24)
  61. case "halfweekly":
  62. timeFrom = timeUntil.Add(-time.Hour * 72)
  63. case "weekly":
  64. timeFrom = timeUntil.Add(-time.Hour * 168)
  65. case "monthly":
  66. timeFrom = timeUntil.AddDate(0, -1, 0)
  67. case "quarterly":
  68. timeFrom = timeUntil.AddDate(0, -3, 0)
  69. case "semiyearly":
  70. timeFrom = timeUntil.AddDate(0, -6, 0)
  71. case "yearly":
  72. timeFrom = timeUntil.AddDate(-1, 0, 0)
  73. default:
  74. timeFrom = timeUntil.Add(-time.Hour * 168)
  75. }
  76. var err error
  77. authors, err := models.GetActivityStatsTopAuthors(ctx.Repo.Repository, timeFrom, 10)
  78. if err != nil {
  79. ctx.ServerError("GetActivityStatsTopAuthors", err)
  80. return
  81. }
  82. ctx.JSON(200, authors)
  83. }