Commit 2b0da92b authored by Stan Hu's avatar Stan Hu

Merge branch 'index-for-groups-analytic-controller-4899' into 'master'

Solve a N+1 issue in Groups::AnalyticsController

See merge request gitlab-org/gitlab-ee!4508
parents 74c4c035 40956c78
...@@ -22,3 +22,5 @@ class PushEventPayload < ActiveRecord::Base ...@@ -22,3 +22,5 @@ class PushEventPayload < ActiveRecord::Base
tag: 1 tag: 1
} }
end end
PushEventPayload.prepend(EE::PushEventPayload)
# frozen_string_literal: true
module EE
module PushEventPayload
extend ActiveSupport::Concern
class_methods do
def commit_count_for(events)
where(event_id: events).sum(:commit_count)
end
end
end
end
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
%h3 Push %h3 Push
- code_push_count = @events.code_push.count - code_push_count = @events.code_push.count
- commits_count = @events.code_push.map(&:commits_count).sum # rubocop: disable CodeReuse/ActiveRecord - commits_count = PushEventPayload.commit_count_for(@events.code_push)
- person_count = @events.code_push.pluck(:author_id).uniq.count # rubocop: disable CodeReuse/ActiveRecord - person_count = @events.code_push.pluck(:author_id).uniq.count # rubocop: disable CodeReuse/ActiveRecord
- person_count_string = pluralize person_count, 'person' - person_count_string = pluralize person_count, 'person'
- pushes_string = _('<strong>%{pushes}</strong> pushes, more than <strong>%{commits}</strong> commits by <strong>%{people}</strong> contributors.').html_safe % { pushes: code_push_count, commits: commits_count , people: person_count_string } - pushes_string = _('<strong>%{pushes}</strong> pushes, more than <strong>%{commits}</strong> commits by <strong>%{people}</strong> contributors.').html_safe % { pushes: code_push_count, commits: commits_count , people: person_count_string }
......
---
title: Solve a N+1 issue in Groups::AnalyticsController
merge_request: 4508
author:
type: performance
...@@ -111,14 +111,13 @@ describe Groups::AnalyticsController do ...@@ -111,14 +111,13 @@ describe Groups::AnalyticsController do
render_views render_views
it 'avoids a N+1 query in #show' do it 'avoids a N+1 query in #show' do
control_count = ActiveRecord::QueryRecorder.new { get :show, params: { group_id: group.path } }.count # Warm the cache
get :show, params: { group_id: group.path }
# Clear out controller state to force a refresh of the group control_queries = ActiveRecord::QueryRecorder.new { get :show, params: { group_id: group.path } }
controller.instance_variable_set(:@group, nil) create_push_event(user, project)
user4 = create(:user)
group.add_user(user4, GroupMember::DEVELOPER)
expect { get :show, params: { group_id: group.path } }.not_to exceed_query_limit(control_count) expect { get :show, params: { group_id: group.path } }.not_to exceed_query_limit(control_queries)
end 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