PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/gitlab/contributions_calendar.rb

https://gitlab.com/wolfgang42/gitlab-ce
Ruby | 97 lines | 68 code | 17 blank | 12 comment | 2 complexity | 73e52d14f9ad34c22ece2e46f086440a MD5 | raw file
  1. # frozen_string_literal: true
  2. module Gitlab
  3. class ContributionsCalendar
  4. attr_reader :contributor
  5. attr_reader :current_user
  6. attr_reader :projects
  7. def initialize(contributor, current_user = nil)
  8. @contributor = contributor
  9. @current_user = current_user
  10. @projects = if @contributor.include_private_contributions?
  11. ContributedProjectsFinder.new(@contributor).execute(@contributor)
  12. else
  13. ContributedProjectsFinder.new(contributor).execute(current_user)
  14. end
  15. end
  16. # rubocop: disable CodeReuse/ActiveRecord
  17. def activity_dates
  18. return @activity_dates if @activity_dates.present?
  19. # Can't use Event.contributions here because we need to check 3 different
  20. # project_features for the (currently) 3 different contribution types
  21. date_from = 1.year.ago
  22. repo_events = event_counts(date_from, :repository)
  23. .having(action: Event::PUSHED)
  24. issue_events = event_counts(date_from, :issues)
  25. .having(action: [Event::CREATED, Event::CLOSED], target_type: "Issue")
  26. mr_events = event_counts(date_from, :merge_requests)
  27. .having(action: [Event::MERGED, Event::CREATED, Event::CLOSED], target_type: "MergeRequest")
  28. note_events = event_counts(date_from, :merge_requests)
  29. .having(action: [Event::COMMENTED])
  30. events = Event
  31. .from_union([repo_events, issue_events, mr_events, note_events])
  32. .map(&:attributes)
  33. @activity_dates = events.each_with_object(Hash.new {|h, k| h[k] = 0 }) do |event, activities|
  34. activities[event["date"]] += event["total_amount"]
  35. end
  36. end
  37. # rubocop: enable CodeReuse/ActiveRecord
  38. # rubocop: disable CodeReuse/ActiveRecord
  39. def events_by_date(date)
  40. return Event.none unless can_read_cross_project?
  41. Event.contributions.where(author_id: contributor.id)
  42. .where(created_at: date.beginning_of_day..date.end_of_day)
  43. .where(project_id: projects)
  44. .with_associations
  45. end
  46. # rubocop: enable CodeReuse/ActiveRecord
  47. def starting_year
  48. 1.year.ago.year
  49. end
  50. def starting_month
  51. Date.current.month
  52. end
  53. private
  54. def can_read_cross_project?
  55. Ability.allowed?(current_user, :read_cross_project)
  56. end
  57. # rubocop: disable CodeReuse/ActiveRecord
  58. def event_counts(date_from, feature)
  59. t = Event.arel_table
  60. # re-running the contributed projects query in each union is expensive, so
  61. # use IN(project_ids...) instead. It's the intersection of two users so
  62. # the list will be (relatively) short
  63. @contributed_project_ids ||= projects.distinct.pluck(:id)
  64. authed_projects = Project.where(id: @contributed_project_ids)
  65. .with_feature_available_for_user(feature, current_user)
  66. .reorder(nil)
  67. .select(:id)
  68. conditions = t[:created_at].gteq(date_from.beginning_of_day)
  69. .and(t[:created_at].lteq(Date.current.end_of_day))
  70. .and(t[:author_id].eq(contributor.id))
  71. date_interval = "INTERVAL '#{Time.zone.now.utc_offset} seconds'"
  72. Event.reorder(nil)
  73. .select(t[:project_id], t[:target_type], t[:action], "date(created_at + #{date_interval}) AS date", 'count(id) as total_amount')
  74. .group(t[:project_id], t[:target_type], t[:action], "date(created_at + #{date_interval})")
  75. .where(conditions)
  76. .where("events.project_id in (#{authed_projects.to_sql})") # rubocop:disable GitlabSecurity/SqlInjection
  77. end
  78. # rubocop: enable CodeReuse/ActiveRecord
  79. end
  80. end