From f1929bf724b58e89ecc6a3f0c667af993c707765 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20D=C3=A1vila?= Date: Tue, 2 Apr 2019 17:10:58 -0500 Subject: [PATCH] Fix query used to calculate number of users over license The new query use the License period as the range date for the query. With this change, customers will see a more accurate number on their Dashboard for the Users Over License section. --- ee/app/models/historical_data.rb | 4 ++- ...d-on-a-license-s-start-date-not-1-year.yml | 5 +++ ee/spec/models/historical_data_spec.rb | 35 +++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 ee/changelogs/unreleased/rd-8292-maximum-historical-users-should-be-based-on-a-license-s-start-date-not-1-year.yml diff --git a/ee/app/models/historical_data.rb b/ee/app/models/historical_data.rb index 145e4ac483bca1..642b951cdbd1e5 100644 --- a/ee/app/models/historical_data.rb +++ b/ee/app/models/historical_data.rb @@ -22,7 +22,9 @@ def at(date) end def max_historical_user_count - HistoricalData.during(1.year.ago..Date.today).maximum(:active_user_count) || 0 + exp_date = License.current&.expires_at || Date.today + + HistoricalData.during(exp_date.ago(1.year)..exp_date).maximum(:active_user_count) || 0 end end end diff --git a/ee/changelogs/unreleased/rd-8292-maximum-historical-users-should-be-based-on-a-license-s-start-date-not-1-year.yml b/ee/changelogs/unreleased/rd-8292-maximum-historical-users-should-be-based-on-a-license-s-start-date-not-1-year.yml new file mode 100644 index 00000000000000..9e67362a5aa951 --- /dev/null +++ b/ee/changelogs/unreleased/rd-8292-maximum-historical-users-should-be-based-on-a-license-s-start-date-not-1-year.yml @@ -0,0 +1,5 @@ +--- +title: Fix query used to calculate number of users over license +merge_request: 10556 +author: +type: fixed diff --git a/ee/spec/models/historical_data_spec.rb b/ee/spec/models/historical_data_spec.rb index bdb3a1427e17a4..321ad7e2c849d1 100644 --- a/ee/spec/models/historical_data_spec.rb +++ b/ee/spec/models/historical_data_spec.rb @@ -76,4 +76,39 @@ end end end + + describe '.max_historical_user_count with data outside of the license period' do + let!(:license) { create(:license) } + + context 'with stats before the license period' do + before do + described_class.create!(date: license.starts_at.ago(2.days), active_user_count: 10) + end + + it 'ignore those records' do + expect(described_class.max_historical_user_count).to eq(0) + end + end + + context 'with stats after the license period' do + before do + described_class.create!(date: license.expires_at.in(2.days), active_user_count: 10) + end + + it 'ignore those records' do + expect(described_class.max_historical_user_count).to eq(0) + end + end + + context 'with stats inside license period' do + before do + described_class.create!(date: license.starts_at.in(2.days), active_user_count: 10) + described_class.create!(date: license.starts_at.in(5.days), active_user_count: 15) + end + + it 'returns max value for active_user_count' do + expect(described_class.max_historical_user_count).to eq(15) + end + end + end end -- GitLab