diff --git a/ee/app/models/historical_data.rb b/ee/app/models/historical_data.rb index 145e4ac483bca1d6ad898e0e991f05b1aaa746b6..642b951cdbd1e58e3a0df1db441fc3778dce749d 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 0000000000000000000000000000000000000000..9e67362a5aa9517d5018e6e7ca3987984a5099bf --- /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 bdb3a1427e17a4ecb4c08ddcbb70ee8b7db35471..321ad7e2c849d1405d4755d529209dcd77f4fd85 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