Commit 7cf73994 authored by Pavel Shutsin's avatar Pavel Shutsin

Fix productivity analytics listing with multiple labels

parent c53477fa
---
title: Fix productivity analytics listing with multiple labels
merge_request: 33182
author:
type: fixed
...@@ -43,28 +43,36 @@ class ProductivityAnalytics ...@@ -43,28 +43,36 @@ class ProductivityAnalytics
end end
columns.unshift(MergeRequest.arel_table[Arel.star]) columns.unshift(MergeRequest.arel_table[Arel.star])
mrs = merge_requests.select(columns) MergeRequest.joins(:metrics).select(columns).where(id: merge_requests).order(sorting)
mrs = mrs.reorder(custom_sorting) if custom_sorting
mrs
end end
private private
def histogram_query(column) def histogram_query(column)
merge_requests.except(:select).select("#{column} as metric, count(*) as mr_count").group(column).reorder(nil) MergeRequest::Metrics.joins(:merge_request)
.where(merge_request_id: merge_requests)
.select("#{column} as metric, count(*) as mr_count")
.group(column)
end end
def scatterplot_query(column) def scatterplot_query(column)
merge_requests.except(:select).select("#{column} as metric, merge_requests.id, merge_request_metrics.merged_at").reorder("merge_request_metrics.merged_at ASC") merge_requests
.except(:select)
.select("#{column} as metric, merge_requests.id, merge_request_metrics.merged_at")
.reorder("merge_request_metrics.merged_at ASC")
end end
def custom_sorting def sorting
return unless sort return default_sorting unless sort
column, direction = sort.split(/_(asc|desc)$/i) column, direction = sort.split(/_(asc|desc)$/i)
return unless column.in?(METRIC_TYPES) return default_sorting unless column.in?(METRIC_TYPES)
Arel.sql("#{column} #{direction}") Arel.sql("#{column} #{direction}")
end end
def default_sorting
{id: :desc}
end
end end
...@@ -3,10 +3,17 @@ ...@@ -3,10 +3,17 @@
require 'spec_helper' require 'spec_helper'
describe ProductivityAnalytics do describe ProductivityAnalytics do
subject(:analytics) { described_class.new(merge_requests: MergeRequest.all, sort: custom_sort) } describe 'metrics data' do
subject(:analytics) { described_class.new(merge_requests: finder_mrs, sort: custom_sort) }
let(:finder_mrs) { ProductivityAnalyticsFinder.new(create(:admin), finder_options).execute }
let(:finder_options) { { state: 'merged' } }
let(:custom_sort) { nil } let(:custom_sort) { nil }
let(:label_a) { create(:label) }
let(:label_b) { create(:label) }
let(:long_mr) do let(:long_mr) do
metrics_data = { metrics_data = {
merged_at: 1.day.ago, merged_at: 1.day.ago,
...@@ -16,7 +23,10 @@ describe ProductivityAnalytics do ...@@ -16,7 +23,10 @@ describe ProductivityAnalytics do
diff_size: 310, diff_size: 310,
modified_paths_size: 15 modified_paths_size: 15
} }
create(:merge_request, :merged, :with_productivity_metrics, created_at: 31.days.ago, metrics_data: metrics_data) create(:labeled_merge_request, :merged, :with_productivity_metrics,
labels: [label_a, label_b],
created_at: 31.days.ago,
metrics_data: metrics_data)
end end
let(:medium_mr) do let(:medium_mr) do
...@@ -29,7 +39,9 @@ describe ProductivityAnalytics do ...@@ -29,7 +39,9 @@ describe ProductivityAnalytics do
modified_paths_size: 3 modified_paths_size: 3
} }
create(:merge_request, :merged, :with_productivity_metrics, created_at: 15.days.ago, metrics_data: metrics_data) create(:labeled_merge_request, :merged, :with_productivity_metrics,
created_at: 15.days.ago,
metrics_data: metrics_data)
end end
let(:short_mr) do let(:short_mr) do
...@@ -42,7 +54,10 @@ describe ProductivityAnalytics do ...@@ -42,7 +54,10 @@ describe ProductivityAnalytics do
modified_paths_size: 3 modified_paths_size: 3
} }
create(:merge_request, :merged, :with_productivity_metrics, created_at: 31.days.ago, metrics_data: metrics_data) create(:labeled_merge_request, :merged, :with_productivity_metrics,
labels: [label_a, label_b],
created_at: 31.days.ago,
metrics_data: metrics_data)
end end
let(:short_mr_2) do let(:short_mr_2) do
...@@ -55,7 +70,51 @@ describe ProductivityAnalytics do ...@@ -55,7 +70,51 @@ describe ProductivityAnalytics do
modified_paths_size: 1 modified_paths_size: 1
} }
create(:merge_request, :merged, :with_productivity_metrics, created_at: 31.days.ago, metrics_data: metrics_data) create(:labeled_merge_request, :merged, :with_productivity_metrics,
labels: [label_a, label_b],
created_at: 31.days.ago,
metrics_data: metrics_data)
end
let(:expected_mr_data) do
{
long_mr: {
'days_to_merge' => 30,
'time_to_first_comment' => 0,
'time_to_last_commit' => 29 * 24,
'time_to_merge' => 24,
'commits_count' => 20,
'loc_per_commit' => 15,
'files_touched' => 15
},
medium_mr: {
'days_to_merge' => 14,
'time_to_first_comment' => 0,
'time_to_last_commit' => 13 * 24,
'time_to_merge' => 24,
'commits_count' => 5,
'loc_per_commit' => 16,
'files_touched' => 3
},
short_mr: {
'days_to_merge' => 3,
'time_to_first_comment' => 24,
'time_to_last_commit' => 2 * 24,
'time_to_merge' => 0,
'commits_count' => 1,
'loc_per_commit' => 14,
'files_touched' => 3
},
short_mr_2: {
'days_to_merge' => 3,
'time_to_first_comment' => 0,
'time_to_last_commit' => 2 * 24,
'time_to_merge' => 24,
'commits_count' => 1,
'loc_per_commit' => 5,
'files_touched' => 1
}
}
end end
before do before do
...@@ -131,6 +190,15 @@ describe ProductivityAnalytics do ...@@ -131,6 +190,15 @@ describe ProductivityAnalytics do
it { is_expected.to eq nil } it { is_expected.to eq nil }
end end
context 'for multiple labeled mrs' do
let(:finder_options) { super().merge(label_name: [label_a.title, label_b.title]) }
let(:metric) { 'days_to_merge' }
it 'returns aggregated data' do
expect(subject).to eq(3 => 2, 30 => 1)
end
end
end end
# Test coverage depends on #histogram_data tests. We want to avoid duplication here, so test only for 1 metric. # Test coverage depends on #histogram_data tests. We want to avoid duplication here, so test only for 1 metric.
...@@ -152,46 +220,14 @@ describe ProductivityAnalytics do ...@@ -152,46 +220,14 @@ describe ProductivityAnalytics do
it 'returns MRs data with all the metrics calculated' do it 'returns MRs data with all the metrics calculated' do
expected_data = { expected_data = {
long_mr.id => { long_mr.id => expected_mr_data[:long_mr],
'days_to_merge' => 30, medium_mr.id => expected_mr_data[:medium_mr],
'time_to_first_comment' => 0, short_mr.id => expected_mr_data[:short_mr],
'time_to_last_commit' => 29 * 24, short_mr_2.id => expected_mr_data[:short_mr_2]
'time_to_merge' => 24,
'commits_count' => 20,
'loc_per_commit' => 15,
'files_touched' => 15
},
medium_mr.id => {
'days_to_merge' => 14,
'time_to_first_comment' => 0,
'time_to_last_commit' => 13 * 24,
'time_to_merge' => 24,
'commits_count' => 5,
'loc_per_commit' => 16,
'files_touched' => 3
},
short_mr.id => {
'days_to_merge' => 3,
'time_to_first_comment' => 24,
'time_to_last_commit' => 2 * 24,
'time_to_merge' => 0,
'commits_count' => 1,
'loc_per_commit' => 14,
'files_touched' => 3
},
short_mr_2.id => {
'days_to_merge' => 3,
'time_to_first_comment' => 0,
'time_to_last_commit' => 2 * 24,
'time_to_merge' => 24,
'commits_count' => 1,
'loc_per_commit' => 5,
'files_touched' => 1
}
} }
expected_data.each do |mr_id, expected_attributes| expected_data.each do |mr_id, expected_attributes|
expect(subject.detect { |mr| mr.id == mr_id}.attributes).to include(expected_attributes) expect(subject.detect { |mr| mr.id == mr_id }.attributes).to include(expected_attributes)
end end
end end
...@@ -204,8 +240,26 @@ describe ProductivityAnalytics do ...@@ -204,8 +240,26 @@ describe ProductivityAnalytics do
context 'with unknown sorting' do context 'with unknown sorting' do
let(:custom_sort) { 'weird_stuff' } let(:custom_sort) { 'weird_stuff' }
it 'does not apply custom sorting' do
expect(subject).to eq [long_mr, medium_mr, short_mr, short_mr_2] it 'sorts by id desc' do
expect(subject).to eq [short_mr_2, short_mr, medium_mr, long_mr]
end
end
end
context 'for multiple labeled mrs' do
let(:finder_options) { super().merge(label_name: [label_a.title, label_b.title]) }
it 'properly returns MRs with metrics calculated' do
expected_data = {
long_mr.id => expected_mr_data[:long_mr],
short_mr.id => expected_mr_data[:short_mr],
short_mr_2.id => expected_mr_data[:short_mr_2]
}
expected_data.each do |mr_id, expected_attributes|
expect(subject.detect { |mr| mr.id == mr_id }.attributes).to include(expected_attributes)
end
end end
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