summaryrefslogtreecommitdiff
path: root/lib/gitlab/contributions_calendar.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/gitlab/contributions_calendar.rb')
-rw-r--r--lib/gitlab/contributions_calendar.rb72
1 files changed, 40 insertions, 32 deletions
diff --git a/lib/gitlab/contributions_calendar.rb b/lib/gitlab/contributions_calendar.rb
index deaaab953aa..f48ba27888c 100644
--- a/lib/gitlab/contributions_calendar.rb
+++ b/lib/gitlab/contributions_calendar.rb
@@ -10,7 +10,7 @@ module Gitlab
def initialize(contributor, current_user = nil)
@contributor = contributor
- @contributor_time_instance = local_time_instance(contributor.timezone)
+ @contributor_time_instance = local_timezone_instance(contributor.timezone).now
@current_user = current_user
@projects = if @contributor.include_private_contributions?
ContributedProjectsFinder.new(@contributor).execute(@contributor)
@@ -21,27 +21,33 @@ module Gitlab
# rubocop: disable CodeReuse/ActiveRecord
def activity_dates
+ return {} if @projects.empty?
return @activity_dates if @activity_dates.present?
+ start_time = @contributor_time_instance.years_ago(1).beginning_of_day
+ end_time = @contributor_time_instance.end_of_day
+
+ date_interval = "INTERVAL '#{@contributor_time_instance.utc_offset} seconds'"
+
# Can't use Event.contributions here because we need to check 3 different
# project_features for the (currently) 3 different contribution types
- date_from = @contributor_time_instance.now.years_ago(1)
- repo_events = event_counts(date_from, :repository)
- .having(action: :pushed)
- issue_events = event_counts(date_from, :issues)
- .having(action: [:created, :closed], target_type: "Issue")
- mr_events = event_counts(date_from, :merge_requests)
- .having(action: [:merged, :created, :closed], target_type: "MergeRequest")
- note_events = event_counts(date_from, :merge_requests)
- .having(action: :commented)
+ repo_events = events_created_between(start_time, end_time, :repository)
+ .where(action: :pushed)
+ issue_events = events_created_between(start_time, end_time, :issues)
+ .where(action: [:created, :closed], target_type: "Issue")
+ mr_events = events_created_between(start_time, end_time, :merge_requests)
+ .where(action: [:merged, :created, :closed], target_type: "MergeRequest")
+ note_events = events_created_between(start_time, end_time, :merge_requests)
+ .where(action: :commented)
events = Event
- .select(:project_id, :target_type, :action, :date, :total_amount)
- .from_union([repo_events, issue_events, mr_events, note_events])
+ .select("date(created_at + #{date_interval}) AS date", 'COUNT(*) AS num_events')
+ .from_union([repo_events, issue_events, mr_events, note_events], remove_duplicates: false)
+ .group(:date)
.map(&:attributes)
@activity_dates = events.each_with_object(Hash.new {|h, k| h[k] = 0 }) do |event, activities|
- activities[event["date"]] += event["total_amount"]
+ activities[event["date"]] += event["num_events"]
end
end
# rubocop: enable CodeReuse/ActiveRecord
@@ -50,7 +56,7 @@ module Gitlab
def events_by_date(date)
return Event.none unless can_read_cross_project?
- date_in_time_zone = date.in_time_zone(@contributor_time_instance)
+ date_in_time_zone = date.in_time_zone(@contributor_time_instance.time_zone)
Event.contributions.where(author_id: contributor.id)
.where(created_at: date_in_time_zone.beginning_of_day..date_in_time_zone.end_of_day)
@@ -60,11 +66,11 @@ module Gitlab
# rubocop: enable CodeReuse/ActiveRecord
def starting_year
- @contributor_time_instance.now.years_ago(1).year
+ @contributor_time_instance.years_ago(1).year
end
def starting_month
- @contributor_time_instance.today.month
+ @contributor_time_instance.month
end
private
@@ -74,29 +80,31 @@ module Gitlab
end
# rubocop: disable CodeReuse/ActiveRecord
- def event_counts(date_from, feature)
- t = Event.arel_table
-
+ def events_created_between(start_time, end_time, feature)
# re-running the contributed projects query in each union is expensive, so
# use IN(project_ids...) instead. It's the intersection of two users so
# the list will be (relatively) short
@contributed_project_ids ||= projects.distinct.pluck(:id)
- authed_projects = Project.where(id: @contributed_project_ids)
- .with_feature_available_for_user(feature, current_user)
- .reorder(nil)
- .select(:id)
-
- conditions = t[:created_at].gteq(date_from.beginning_of_day)
- .and(t[:created_at].lteq(@contributor_time_instance.today.end_of_day))
- .and(t[:author_id].eq(contributor.id))
- date_interval = "INTERVAL '#{@contributor_time_instance.now.utc_offset} seconds'"
+ # no need to check feature access of current user, if the contributor opted-in
+ # to show all private events anyway - otherwise they would get filtered out again
+ authed_projects = if @contributor.include_private_contributions?
+ @contributed_project_ids
+ else
+ ProjectFeature
+ .with_feature_available_for_user(feature, current_user)
+ .where(project_id: @contributed_project_ids)
+ .reorder(nil)
+ .select(:project_id)
+ end
Event.reorder(nil)
- .select(t[:project_id], t[:target_type], t[:action], "date(created_at + #{date_interval}) AS date", 'count(id) as total_amount')
- .group(t[:project_id], t[:target_type], t[:action], "date(created_at + #{date_interval})")
- .where(conditions)
- .where("events.project_id in (#{authed_projects.to_sql})") # rubocop:disable GitlabSecurity/SqlInjection
+ .select(:created_at)
+ .where(
+ author_id: contributor.id,
+ created_at: start_time..end_time,
+ events: { project_id: authed_projects }
+ )
end
# rubocop: enable CodeReuse/ActiveRecord
end