debian-mirror-gitlab/lib/gitlab/contributions_calendar.rb

112 lines
4.2 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
2015-04-26 12:48:37 +05:30
module Gitlab
class ContributionsCalendar
2021-12-11 22:18:48 +05:30
include TimeZoneHelper
2016-11-24 13:41:30 +05:30
attr_reader :contributor
attr_reader :current_user
attr_reader :projects
2015-04-26 12:48:37 +05:30
2016-11-24 13:41:30 +05:30
def initialize(contributor, current_user = nil)
@contributor = contributor
2022-01-26 12:08:38 +05:30
@contributor_time_instance = local_timezone_instance(contributor.timezone).now
2016-11-24 13:41:30 +05:30
@current_user = current_user
2018-11-20 20:47:30 +05:30
@projects = if @contributor.include_private_contributions?
ContributedProjectsFinder.new(@contributor).execute(@contributor)
else
ContributedProjectsFinder.new(contributor).execute(current_user)
end
2015-04-26 12:48:37 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2016-09-29 09:46:39 +05:30
def activity_dates
2022-01-26 12:08:38 +05:30
return {} if @projects.empty?
2016-09-29 09:46:39 +05:30
return @activity_dates if @activity_dates.present?
2015-04-26 12:48:37 +05:30
2022-01-26 12:08:38 +05:30
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'"
2016-11-24 13:41:30 +05:30
# Can't use Event.contributions here because we need to check 3 different
# project_features for the (currently) 3 different contribution types
2022-01-26 12:08:38 +05:30
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)
2015-04-26 12:48:37 +05:30
2018-12-05 23:21:45 +05:30
events = Event
2022-01-26 12:08:38 +05:30
.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)
2018-12-05 23:21:45 +05:30
.map(&:attributes)
2015-04-26 12:48:37 +05:30
2017-09-10 17:25:29 +05:30
@activity_dates = events.each_with_object(Hash.new {|h, k| h[k] = 0 }) do |event, activities|
2022-01-26 12:08:38 +05:30
activities[event["date"]] += event["num_events"]
2015-04-26 12:48:37 +05:30
end
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2015-04-26 12:48:37 +05:30
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2015-04-26 12:48:37 +05:30
def events_by_date(date)
2018-03-27 19:54:05 +05:30
return Event.none unless can_read_cross_project?
2022-01-26 12:08:38 +05:30
date_in_time_zone = date.in_time_zone(@contributor_time_instance.time_zone)
2021-12-11 22:18:48 +05:30
2018-11-20 20:47:30 +05:30
Event.contributions.where(author_id: contributor.id)
2021-12-11 22:18:48 +05:30
.where(created_at: date_in_time_zone.beginning_of_day..date_in_time_zone.end_of_day)
2017-09-10 17:25:29 +05:30
.where(project_id: projects)
2019-07-07 11:18:12 +05:30
.with_associations
2015-04-26 12:48:37 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2015-04-26 12:48:37 +05:30
def starting_year
2022-01-26 12:08:38 +05:30
@contributor_time_instance.years_ago(1).year
2015-04-26 12:48:37 +05:30
end
def starting_month
2022-01-26 12:08:38 +05:30
@contributor_time_instance.month
2015-04-26 12:48:37 +05:30
end
2016-11-24 13:41:30 +05:30
private
2018-03-27 19:54:05 +05:30
def can_read_cross_project?
Ability.allowed?(current_user, :read_cross_project)
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2022-01-26 12:08:38 +05:30
def events_created_between(start_time, end_time, feature)
2016-11-24 13:41:30 +05:30
# 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-12-13 13:39:08 +05:30
@contributed_project_ids ||= projects.distinct.pluck(:id)
2016-11-24 13:41:30 +05:30
2022-01-26 12:08:38 +05:30
# 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
2017-09-10 17:25:29 +05:30
Event.reorder(nil)
2022-01-26 12:08:38 +05:30
.select(:created_at)
.where(
author_id: contributor.id,
created_at: start_time..end_time,
events: { project_id: authed_projects }
)
2016-11-24 13:41:30 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2015-04-26 12:48:37 +05:30
end
end