Commit fff065ed authored by Sean McGivern's avatar Sean McGivern

Merge branch 'issue_8744' into 'master'

Add deepest epic relation level to usage data

Closes #8744

See merge request gitlab-org/gitlab-ee!8927
parents ea178142 0584442d
...@@ -156,6 +156,45 @@ module EE ...@@ -156,6 +156,45 @@ module EE
) )
end end
end end
# Return the deepest relation level for an epic.
# Example 1:
# epic1 - parent: nil
# epic2 - parent: epic1
# epic3 - parent: epic 2
# Returns: 3
# ------------
# Example 2:
# epic1 - parent: nil
# epic2 - parent: epic1
# Returns: 2
def deepest_relationship_level
return unless ::Group.supports_nested_objects?
result =
ActiveRecord::Base.connection.execute(
<<-SQL
WITH RECURSIVE descendants AS (
SELECT id, 1 depth
FROM epics
WHERE parent_id IS NOT NULL
UNION
SELECT e.id, d.depth + 1
FROM epics e
INNER JOIN descendants d
ON e.parent_id = d.id
)
SELECT MAX(depth) as deepest_level FROM descendants
SQL
)
deepest_level = result.first["deepest_level"] || 0
# For performance reason epics without a parent_id are being
# ignored in the query.
# So we sum 1 to the result to take into account first parent.
deepest_level + 1
end
end end
def assignees def assignees
......
---
title: Gather deepest epic relationship data
merge_request:
author:
type: other
...@@ -105,8 +105,17 @@ module EE ...@@ -105,8 +105,17 @@ module EE
projects_with_tracing_enabled: count(ProjectTracingSetting) projects_with_tracing_enabled: count(ProjectTracingSetting)
}).merge(service_desk_counts).merge(security_products_usage) }).merge(service_desk_counts).merge(security_products_usage)
# MySql does not support recursive queries so we can't retrieve epics relationship depth
if ::Group.supports_nested_objects?
usage_data[:counts] = usage_data[:counts].merge(epics_deepest_relationship_level)
end
usage_data usage_data
end end
def epics_deepest_relationship_level
{ epics_deepest_relationship_level: ::Epic.deepest_relationship_level }
end
end end
end end
end end
...@@ -73,6 +73,12 @@ describe Gitlab::UsageData do ...@@ -73,6 +73,12 @@ describe Gitlab::UsageData do
expect(count_data[:projects_with_packages]).to eq(2) expect(count_data[:projects_with_packages]).to eq(2)
end end
it 'gathers deepest epic relationship level', :postgresql do
count_data = subject[:counts]
expect(count_data.keys).to include(:epics_deepest_relationship_level)
end
it 'gathers security products usage data' do it 'gathers security products usage data' do
count_data = subject[:counts] count_data = subject[:counts]
......
...@@ -443,6 +443,27 @@ describe Epic do ...@@ -443,6 +443,27 @@ describe Epic do
end end
end end
describe '.deepest_relationship_level', :postgresql do
it 'returns the deepest relationship level between epics' do
group_1 = create(:group)
group_2 = create(:group)
# No relationship
create(:epic, group: group_1)
# Two levels relationship
group_1_epic_1 = create(:epic, group: group_1)
create(:epic, group: group_1, parent: group_1_epic_1)
# Three levels relationship
group_2_epic_1 = create(:epic, group: group_2)
group_2_epic_2 = create(:epic, group: group_2, parent: group_2_epic_1)
create(:epic, group: group_2, parent: group_2_epic_2)
expect(described_class.deepest_relationship_level).to eq(3)
end
end
describe '#issues_readable_by' do describe '#issues_readable_by' do
let(:user) { create(:user) } let(:user) { create(:user) }
let(:group) { create(:group, :private) } let(:group) { create(:group, :private) }
......
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