preload ids or objects for users, merge request and issues

This commit is contained in:
James Lopez 2016-11-17 20:42:18 +01:00
parent de96f295a2
commit 98773ef974
6 changed files with 56 additions and 19 deletions

View file

@ -0,0 +1,27 @@
module Gitlab
module CycleAnalytics
class AuthorUpdater
def self.update!(*args)
new(*args).update!
end
def initialize(event_result)
@event_result = event_result
end
def update!
@event_result.each do |event|
event['author'] = users[event.delete('author_id').to_i].first
end
end
def user_ids
@event_result.map { |event| event['author_id'] }
end
def users
@users ||= User.find(user_ids).group_by { |user| user['id'] }
end
end
end
end

View file

@ -12,7 +12,9 @@ module Gitlab
end
def fetch
@query.execute(self).map do |event|
update_author! if event_result.first['author_id']
event_result.map do |event|
serialize(event) if has_permission?(event['id'])
end
end
@ -25,12 +27,28 @@ module Gitlab
private
def update_author!
AuthorUpdater.update!(event_result)
end
def event_result
@event_result ||= @query.execute(self).to_a
end
def serialize(_event)
raise NotImplementedError.new("Expected #{self.name} to implement serialize(event)")
end
def has_permission?(_id)
true
def has_permission?(id)
allowed_ids.nil? || allowed_ids.include?(id.to_i)
end
def allowed_ids
nil
end
def event_result_ids
event_result.map { |event| event['id'] }
end
end
end

View file

@ -19,13 +19,11 @@ module Gitlab
private
def serialize(event)
event['author'] = User.find(event.delete('author_id'))
AnalyticsMergeRequestSerializer.new(project: @project).represent(event).as_json
end
def has_permission?(id)
@options[:current_user].can?(:read_merge_request, MergeRequest.find(id))
def allowed_ids
@allowed_ids ||= MergeRequestsFinder.new(@options[:current_user], project_id: @project.id).execute.where(id: event_result_ids).pluck(:id)
end
end
end

View file

@ -18,13 +18,11 @@ module Gitlab
private
def serialize(event)
event['author'] = User.find(event.delete('author_id'))
AnalyticsIssueSerializer.new(project: @project).represent(event).as_json
end
def has_permission?(id)
@options[:current_user].can?(:read_issue, Issue.find(id))
def allowed_ids
@allowed_ids ||= IssuesFinder.new(@options[:current_user], project_id: @project.id).execute.where(id: event_result_ids).pluck(:id)
end
end
end

View file

@ -17,13 +17,11 @@ module Gitlab
private
def serialize(event)
event['author'] = User.find(event.delete('author_id'))
AnalyticsIssueSerializer.new(project: @project).represent(event).as_json
end
def has_permission?(id)
@options[:current_user].can?(:read_issue, Issue.find(id))
def allowed_ids
@allowed_ids ||= IssuesFinder.new(@options[:current_user], project_id: @project.id).execute.where(id: event_result_ids).pluck(:id)
end
end
end

View file

@ -16,13 +16,11 @@ module Gitlab
end
def serialize(event)
event['author'] = User.find(event.delete('author_id'))
AnalyticsMergeRequestSerializer.new(project: @project).represent(event).as_json
end
def has_permission?(id)
@options[:current_user].can?(:read_merge_request, MergeRequest.find(id))
def allowed_ids
@allowed_ids ||= MergeRequestsFinder.new(@options[:current_user], project_id: @project.id).execute.where(id: event_result_ids).pluck(:id)
end
end
end