Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Léo-Paul Géneau
gitlab-ce
Commits
d8e504a8
Commit
d8e504a8
authored
Oct 11, 2017
by
Bob Van Landuyt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use Arel to build preloaded counts
parent
18907efb
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
45 additions
and
48 deletions
+45
-48
app/models/concerns/loaded_in_group_list.rb
app/models/concerns/loaded_in_group_list.rb
+45
-48
No files found.
app/models/concerns/loaded_in_group_list.rb
View file @
d8e504a8
module
LoadedInGroupList
module
LoadedInGroupList
extend
ActiveSupport
::
Concern
extend
ActiveSupport
::
Concern
PROJECT_COUNT_SQL
=
<<~
PROJECTCOUNT
.
freeze
SELECT COUNT(*) AS preloaded_project_count
FROM projects
WHERE projects.namespace_id = namespaces.id
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
COUNT_SELECTS
=
[
'namespaces.*'
,
SUBGROUP_COUNT_SQL
,
MEMBER_COUNT_SQL
].
freeze
module
ClassMethods
module
ClassMethods
def
with_counts
(
archived
:)
def
with_counts
(
archived
:)
selects
=
COUNT_SELECTS
.
dup
<<
project_count
(
archived
)
selects_including_counts
=
[
select
(
selects
)
'namespaces.*'
,
"(
#{
project_count_sql
(
archived
).
to_sql
}
)"
,
"(
#{
subgroup_count_sql
.
to_sql
}
)"
,
"(
#{
member_count_sql
.
to_sql
}
)"
]
select
(
selects_including_counts
)
end
end
def
with_selects_for_list
(
archived:
nil
)
def
with_selects_for_list
(
archived:
nil
)
with_route
.
with_counts
(
archived:
archived
)
with_route
.
with_counts
(
archived:
archived
)
end
end
def
project_count
(
archived
)
private
project_count
=
if
archived
==
'only'
PROJECT_COUNT_SQL
+
'AND projects.archived IS true'
def
project_count_sql
(
archived
=
nil
)
elsif
Gitlab
::
Utils
.
to_boolean
(
archived
)
projects
=
Project
.
arel_table
PROJECT_COUNT_SQL
namespaces
=
Namespace
.
arel_table
else
PROJECT_COUNT_SQL
+
'AND projects.archived IS NOT true'
base_count
=
projects
.
project
(
Arel
.
star
.
count
.
as
(
'preloaded_project_count'
))
end
.
where
(
projects
[
:namespace_id
].
eq
(
namespaces
[
:id
]))
"(
#{
project_count
}
)"
if
archived
==
'only'
base_count
.
where
(
projects
[
:archived
].
eq
(
true
))
elsif
Gitlab
::
Utils
.
to_boolean
(
archived
)
base_count
else
base_count
.
where
(
projects
[
:archived
].
not_eq
(
true
))
end
end
def
subgroup_count_sql
namespaces
=
Namespace
.
arel_table
children
=
namespaces
.
alias
(
'children'
)
namespaces
.
project
(
Arel
.
star
.
count
.
as
(
'preloaded_subgroup_count'
))
.
from
(
children
)
.
where
(
children
[
:parent_id
].
eq
(
namespaces
[
:id
]))
end
def
member_count_sql
members
=
Member
.
arel_table
namespaces
=
Namespace
.
arel_table
members
.
project
(
Arel
.
star
.
count
.
as
(
'preloaded_member_count'
))
.
where
(
members
[
:source_type
].
eq
(
Namespace
.
name
))
.
where
(
members
[
:source_id
].
eq
(
namespaces
[
:id
]))
.
where
(
members
[
:requested_at
].
eq
(
nil
))
end
end
end
end
...
@@ -50,26 +59,14 @@ module LoadedInGroupList
...
@@ -50,26 +59,14 @@ module LoadedInGroupList
end
end
def
project_count
def
project_count
@project_count
||=
if
respond_to?
(
:preloaded_project_count
)
@project_count
||=
try
(
:preloaded_project_count
)
||
projects
.
non_archived
.
count
preloaded_project_count
else
projects
.
non_archived
.
count
end
end
end
def
subgroup_count
def
subgroup_count
@subgroup_count
||=
if
respond_to?
(
:preloaded_subgroup_count
)
@subgroup_count
||=
try
(
:preloaded_subgroup_count
)
||
children
.
count
preloaded_subgroup_count
else
children
.
count
end
end
end
def
member_count
def
member_count
@member_count
||=
if
respond_to?
(
:preloaded_member_count
)
@member_count
||=
try
(
:preloaded_member_count
)
||
users
.
count
preloaded_member_count
else
users
.
count
end
end
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment