summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames Lopez <james@jameslopez.es>2016-11-17 20:42:18 +0100
committerJames Lopez <james@jameslopez.es>2016-11-17 20:42:18 +0100
commit98773ef9745ae64da953a62fd4fbcbf290a37ea7 (patch)
tree3f3f41d27bee7c5f72b7666b5896d89b93b0c1b9
parentde96f295a2c08ee2888de91b910d714a15204fb2 (diff)
downloadgitlab-ce-98773ef9745ae64da953a62fd4fbcbf290a37ea7.tar.gz
preload ids or objects for users, merge request and issues
-rw-r--r--lib/gitlab/cycle_analytics/author_updater.rb27
-rw-r--r--lib/gitlab/cycle_analytics/base_event.rb24
-rw-r--r--lib/gitlab/cycle_analytics/code_event.rb6
-rw-r--r--lib/gitlab/cycle_analytics/issue_event.rb6
-rw-r--r--lib/gitlab/cycle_analytics/production_event.rb6
-rw-r--r--lib/gitlab/cycle_analytics/review_event.rb6
6 files changed, 56 insertions, 19 deletions
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