Commit 98773ef9 authored by James Lopez's avatar James Lopez

preload ids or objects for users, merge request and issues

parent de96f295
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
......@@ -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
......
......@@ -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
......
......@@ -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
......
......@@ -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
......
......@@ -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
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment