2018-10-22 07:00:50 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-03-22 06:48:08 +00:00
|
|
|
module Gitlab
|
|
|
|
class ContributionsCalendar
|
2016-11-04 14:15:43 +00:00
|
|
|
attr_reader :contributor
|
|
|
|
attr_reader :current_user
|
|
|
|
attr_reader :projects
|
2015-03-22 06:48:08 +00:00
|
|
|
|
2016-11-04 14:15:43 +00:00
|
|
|
def initialize(contributor, current_user = nil)
|
|
|
|
@contributor = contributor
|
|
|
|
@current_user = current_user
|
2018-09-07 15:08:55 +00:00
|
|
|
@projects = if @contributor.include_private_contributions?
|
|
|
|
ContributedProjectsFinder.new(@contributor).execute(@contributor)
|
|
|
|
else
|
|
|
|
ContributedProjectsFinder.new(contributor).execute(current_user)
|
|
|
|
end
|
2015-03-22 06:48:08 +00:00
|
|
|
end
|
|
|
|
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-09-14 02:43:25 +00:00
|
|
|
def activity_dates
|
|
|
|
return @activity_dates if @activity_dates.present?
|
2015-03-22 06:48:08 +00:00
|
|
|
|
2016-11-04 14:15:43 +00:00
|
|
|
# Can't use Event.contributions here because we need to check 3 different
|
|
|
|
# project_features for the (currently) 3 different contribution types
|
2015-03-22 06:48:08 +00:00
|
|
|
date_from = 1.year.ago
|
2017-06-21 13:48:12 +00:00
|
|
|
repo_events = event_counts(date_from, :repository)
|
|
|
|
.having(action: Event::PUSHED)
|
|
|
|
issue_events = event_counts(date_from, :issues)
|
|
|
|
.having(action: [Event::CREATED, Event::CLOSED], target_type: "Issue")
|
|
|
|
mr_events = event_counts(date_from, :merge_requests)
|
|
|
|
.having(action: [Event::MERGED, Event::CREATED, Event::CLOSED], target_type: "MergeRequest")
|
|
|
|
note_events = event_counts(date_from, :merge_requests)
|
2018-03-07 10:16:38 +00:00
|
|
|
.having(action: [Event::COMMENTED])
|
2015-03-22 06:48:08 +00:00
|
|
|
|
2018-09-11 15:31:34 +00:00
|
|
|
events = Event
|
|
|
|
.from_union([repo_events, issue_events, mr_events, note_events])
|
|
|
|
.map(&:attributes)
|
2015-03-22 06:48:08 +00:00
|
|
|
|
2017-06-07 13:29:49 +00:00
|
|
|
@activity_dates = events.each_with_object(Hash.new {|h, k| h[k] = 0 }) do |event, activities|
|
2016-11-04 14:15:43 +00:00
|
|
|
activities[event["date"]] += event["total_amount"]
|
2015-03-22 06:48:08 +00:00
|
|
|
end
|
|
|
|
end
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2015-03-22 06:48:08 +00:00
|
|
|
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2015-03-22 06:48:08 +00:00
|
|
|
def events_by_date(date)
|
2017-12-11 14:21:06 +00:00
|
|
|
return Event.none unless can_read_cross_project?
|
|
|
|
|
2018-09-07 15:08:55 +00:00
|
|
|
Event.contributions.where(author_id: contributor.id)
|
2017-06-21 13:48:12 +00:00
|
|
|
.where(created_at: date.beginning_of_day..date.end_of_day)
|
|
|
|
.where(project_id: projects)
|
2019-03-01 18:12:50 +00:00
|
|
|
.with_associations
|
2015-03-22 06:48:08 +00:00
|
|
|
end
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2015-03-22 06:48:08 +00:00
|
|
|
|
|
|
|
def starting_year
|
2016-01-02 03:11:34 +00:00
|
|
|
1.year.ago.year
|
2015-03-22 06:48:08 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def starting_month
|
2017-07-29 05:36:54 +00:00
|
|
|
Date.current.month
|
2015-03-22 06:48:08 +00:00
|
|
|
end
|
2016-11-04 14:15:43 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-12-11 14:21:06 +00:00
|
|
|
def can_read_cross_project?
|
|
|
|
Ability.allowed?(current_user, :read_cross_project)
|
|
|
|
end
|
|
|
|
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-11-04 14:15:43 +00:00
|
|
|
def event_counts(date_from, feature)
|
|
|
|
t = Event.arel_table
|
|
|
|
|
|
|
|
# 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
|
2018-10-26 16:19:28 +00:00
|
|
|
@contributed_project_ids ||= projects.distinct.pluck(:id)
|
2017-06-21 13:48:12 +00:00
|
|
|
authed_projects = Project.where(id: @contributed_project_ids)
|
|
|
|
.with_feature_available_for_user(feature, current_user)
|
|
|
|
.reorder(nil)
|
|
|
|
.select(:id)
|
2016-11-04 14:15:43 +00:00
|
|
|
|
2017-06-21 13:48:12 +00:00
|
|
|
conditions = t[:created_at].gteq(date_from.beginning_of_day)
|
2017-07-29 05:36:54 +00:00
|
|
|
.and(t[:created_at].lteq(Date.current.end_of_day))
|
2017-06-21 13:48:12 +00:00
|
|
|
.and(t[:author_id].eq(contributor.id))
|
2016-11-04 14:15:43 +00:00
|
|
|
|
2019-07-24 13:59:55 +00:00
|
|
|
date_interval = "INTERVAL '#{Time.zone.now.utc_offset} seconds'"
|
2017-07-29 06:50:17 +00:00
|
|
|
|
2017-06-21 13:48:12 +00:00
|
|
|
Event.reorder(nil)
|
2017-07-31 22:09:41 +00:00
|
|
|
.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})")
|
2017-06-21 13:48:12 +00:00
|
|
|
.where(conditions)
|
2018-05-29 18:20:43 +00:00
|
|
|
.where("events.project_id in (#{authed_projects.to_sql})") # rubocop:disable GitlabSecurity/SqlInjection
|
2016-11-04 14:15:43 +00:00
|
|
|
end
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2015-03-22 06:48:08 +00:00
|
|
|
end
|
|
|
|
end
|