diff options
author | Bob Van Landuyt <bob@vanlanduyt.co> | 2017-10-02 11:12:57 +0300 |
---|---|---|
committer | Bob Van Landuyt <bob@vanlanduyt.co> | 2017-10-04 23:49:42 +0300 |
commit | cb8e67c292260f9be3f24e81f76a5a172fbe6a2f (patch) | |
tree | b992cc37be64277cc2bee4411592f5ad46824d71 /app/finders | |
parent | af0b8e0558f529cd79a9dd061dc54ae3bfa9d1dd (diff) |
Improve count queries and move them to contants
Diffstat (limited to 'app/finders')
-rw-r--r-- | app/finders/group_descendants_finder.rb | 55 |
1 files changed, 28 insertions, 27 deletions
diff --git a/app/finders/group_descendants_finder.rb b/app/finders/group_descendants_finder.rb index d921e59d16f..534facd4179 100644 --- a/app/finders/group_descendants_finder.rb +++ b/app/finders/group_descendants_finder.rb @@ -3,6 +3,30 @@ class GroupDescendantsFinder attr_reader :current_user, :parent_group, :params + PROJECT_COUNT_SQL = <<~PROJECTCOUNT.freeze + (SELECT COUNT(*) AS preloaded_project_count + FROM projects + WHERE projects.namespace_id = namespaces.id + AND projects.archived IS NOT true) + PROJECTCOUNT + SUBGROUP_COUNT_SQL = <<~SUBGROUPCOUNT.freeze + (SELECT COUNT(*) AS preloaded_subgroup_count + FROM namespaces children + WHERE children.parent_id = namespaces.id) + SUBGROUPCOUNT + MEMBER_COUNT_SQL = <<~MEMBERCOUNT.freeze + (SELECT COUNT(*) AS preloaded_member_count + FROM members + WHERE members.source_type = 'Namespace' + AND members.source_id = namespaces.id + AND members.requested_at IS NULL) + MEMBERCOUNT + + GROUP_SELECTS = ['namespaces.*', + PROJECT_COUNT_SQL, + SUBGROUP_COUNT_SQL, + MEMBER_COUNT_SQL].freeze + def initialize(current_user: nil, parent_group:, params: {}) @current_user = current_user @parent_group = parent_group @@ -31,39 +55,16 @@ class GroupDescendantsFinder def children return @children if @children - projects_count = <<~PROJECTCOUNT - (SELECT COUNT(projects.id) AS preloaded_project_count - FROM projects WHERE projects.namespace_id = namespaces.id) - PROJECTCOUNT - subgroup_count = <<~SUBGROUPCOUNT - (SELECT COUNT(children.id) AS preloaded_subgroup_count - FROM namespaces children - WHERE children.parent_id = namespaces.id) - SUBGROUPCOUNT - member_count = <<~MEMBERCOUNT - (SELECT COUNT(members.user_id) AS preloaded_member_count - FROM members - WHERE members.source_type = 'Namespace' - AND members.source_id = namespaces.id - AND members.requested_at IS NULL) - MEMBERCOUNT - group_selects = [ - 'namespaces.*', - projects_count, - subgroup_count, - member_count - ] - - subgroups_with_counts = subgroups.with_route.page(params[:page]).per(per_page).select(group_selects) - group_page_count = subgroups_with_counts.total_pages - subgroup_page = subgroups_with_counts.current_page + subgroups_with_counts = subgroups.with_route + .page(params[:page]).per(per_page) + .select(GROUP_SELECTS) paginated_projects = projects.with_route.page(subgroup_page - group_page_count) .per(per_page - subgroups_with_counts.size) if params[:filter] ancestors_for_project_search = ancestors_for_groups(Group.where(id: paginated_projects.select(:namespace_id))) - subgroups_with_counts = ancestors_for_project_search.with_route.select(group_selects) | subgroups_with_counts + subgroups_with_counts = ancestors_for_project_search.with_route.select(GROUP_SELECTS) | subgroups_with_counts end @children = subgroups_with_counts + paginated_projects |