From 98773ef9745ae64da953a62fd4fbcbf290a37ea7 Mon Sep 17 00:00:00 2001 From: James Lopez Date: Thu, 17 Nov 2016 20:42:18 +0100 Subject: preload ids or objects for users, merge request and issues --- lib/gitlab/cycle_analytics/author_updater.rb | 27 ++++++++++++++++++++++++++ lib/gitlab/cycle_analytics/base_event.rb | 24 ++++++++++++++++++++--- lib/gitlab/cycle_analytics/code_event.rb | 6 ++---- lib/gitlab/cycle_analytics/issue_event.rb | 6 ++---- lib/gitlab/cycle_analytics/production_event.rb | 6 ++---- lib/gitlab/cycle_analytics/review_event.rb | 6 ++---- 6 files changed, 56 insertions(+), 19 deletions(-) create mode 100644 lib/gitlab/cycle_analytics/author_updater.rb diff --git a/lib/gitlab/cycle_analytics/author_updater.rb b/lib/gitlab/cycle_analytics/author_updater.rb new file mode 100644 index 00000000000..ab18f292bc3 --- /dev/null +++ b/lib/gitlab/cycle_analytics/author_updater.rb @@ -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 diff --git a/lib/gitlab/cycle_analytics/base_event.rb b/lib/gitlab/cycle_analytics/base_event.rb index 1e76fbec855..7395561a3fc 100644 --- a/lib/gitlab/cycle_analytics/base_event.rb +++ b/lib/gitlab/cycle_analytics/base_event.rb @@ -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 diff --git a/lib/gitlab/cycle_analytics/code_event.rb b/lib/gitlab/cycle_analytics/code_event.rb index 29f62cb22aa..02a3a44d544 100644 --- a/lib/gitlab/cycle_analytics/code_event.rb +++ b/lib/gitlab/cycle_analytics/code_event.rb @@ -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 diff --git a/lib/gitlab/cycle_analytics/issue_event.rb b/lib/gitlab/cycle_analytics/issue_event.rb index 70c015df419..36a990d6222 100644 --- a/lib/gitlab/cycle_analytics/issue_event.rb +++ b/lib/gitlab/cycle_analytics/issue_event.rb @@ -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 diff --git a/lib/gitlab/cycle_analytics/production_event.rb b/lib/gitlab/cycle_analytics/production_event.rb index 80c0d08c039..fcf2dbe3490 100644 --- a/lib/gitlab/cycle_analytics/production_event.rb +++ b/lib/gitlab/cycle_analytics/production_event.rb @@ -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 diff --git a/lib/gitlab/cycle_analytics/review_event.rb b/lib/gitlab/cycle_analytics/review_event.rb index cc89ef68be0..30650537afe 100644 --- a/lib/gitlab/cycle_analytics/review_event.rb +++ b/lib/gitlab/cycle_analytics/review_event.rb @@ -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 -- cgit v1.2.1