Commit 14065e89 authored by Stan Hu's avatar Stan Hu

Merge branch '12241-current-active-users-historic-max' into 'master'

Resolve "Current active users > historic max"

Closes #12241

See merge request gitlab-org/gitlab-ee!15107
parents d2e8d26c b65c6773
...@@ -21,10 +21,13 @@ class HistoricalData < ApplicationRecord ...@@ -21,10 +21,13 @@ class HistoricalData < ApplicationRecord
find_by(date: date) find_by(date: date)
end end
def max_historical_user_count def max_historical_user_count(license: nil, from: nil, to: nil)
exp_date = License.current&.expires_at || Date.today license ||= License.current
expires_at = license&.expires_at || Date.today
from ||= expires_at - 1.year
to ||= expires_at
HistoricalData.during(exp_date.ago(1.year)..exp_date).maximum(:active_user_count) || 0 HistoricalData.during(from..to).maximum(:active_user_count) || 0
end end
end end
end end
...@@ -406,10 +406,7 @@ class License < ApplicationRecord ...@@ -406,10 +406,7 @@ class License < ApplicationRecord
end end
def historical_max(from = nil, to = nil) def historical_max(from = nil, to = nil)
from ||= starts_at - 1.year HistoricalData.max_historical_user_count(license: self, from: from, to: to)
to ||= starts_at
HistoricalData.during(from..to).maximum(:active_user_count) || 0
end end
def historical_max_with_default_period def historical_max_with_default_period
...@@ -439,22 +436,27 @@ class License < ApplicationRecord ...@@ -439,22 +436,27 @@ class License < ApplicationRecord
self.errors.add(:base, "The license key is invalid. Make sure it is exactly as you received it from GitLab Inc.") self.errors.add(:base, "The license key is invalid. Make sure it is exactly as you received it from GitLab Inc.")
end end
def empty_historical_max? def prior_historical_max
historical_max == 0 @prior_historical_max ||= begin
from = starts_at - 1.year
to = starts_at
historical_max(from, to)
end
end end
def check_users_limit def check_users_limit
return unless restricted_user_count return unless restricted_user_count
if previous_user_count && (historical_max <= previous_user_count) if previous_user_count && (prior_historical_max <= previous_user_count)
return if restricted_user_count >= current_active_users_count return if restricted_user_count >= current_active_users_count
else else
return if restricted_user_count >= historical_max return if restricted_user_count >= prior_historical_max
end end
user_count = empty_historical_max? ? current_active_users_count : historical_max user_count = prior_historical_max.zero? ? current_active_users_count : prior_historical_max
add_limit_error(current_period: empty_historical_max?, user_count: user_count) add_limit_error(current_period: prior_historical_max.zero?, user_count: user_count)
end end
def check_trueup def check_trueup
......
---
title: Show correct historic max user count for a license
merge_request: 15107
author:
type: fixed
...@@ -39,19 +39,41 @@ describe HistoricalData do ...@@ -39,19 +39,41 @@ describe HistoricalData do
end end
end end
describe ".max_historical_user_count" do describe '.max_historical_user_count' do
context 'with multiple historical data points for the current license' do
before do before do
(1..3).each do |i| (1..3).each do |i|
described_class.create!(date: Time.now - i.days, active_user_count: i * 100) described_class.create!(date: Time.now - i.days, active_user_count: i * 100)
end end
end end
it "returns max user count for the past year" do it 'returns max user count for the past year' do
expect(described_class.max_historical_user_count).to eq(300) expect(described_class.max_historical_user_count).to eq(300)
end end
end end
describe '.max_historical_user_count with different plans' do context 'using parameters' do
let!(:license) do
create(
:license,
starts_at: Date.new(2014, 1, 1),
expires_at: Date.new(2014, 12, 1)
)
end
it 'returns max user count for the given license' do
expect(described_class.max_historical_user_count(license: license)).to eq(1200)
end
it 'returns max user count for the time range' do
from = Date.new(2014, 6, 1)
to = Date.new(2014, 9, 1)
expect(described_class.max_historical_user_count(from: from, to: to)).to eq(900)
end
end
context 'with different plans' do
using RSpec::Parameterized::TableSyntax using RSpec::Parameterized::TableSyntax
before do before do
...@@ -77,7 +99,7 @@ describe HistoricalData do ...@@ -77,7 +99,7 @@ describe HistoricalData do
end end
end end
describe '.max_historical_user_count with data outside of the license period' do context 'with data outside of the license period' do
let!(:license) { create(:license) } let!(:license) { create(:license) }
context 'with stats before the license period' do context 'with stats before the license period' do
...@@ -111,4 +133,5 @@ describe HistoricalData do ...@@ -111,4 +133,5 @@ describe HistoricalData do
end end
end end
end end
end
end end
...@@ -83,7 +83,7 @@ describe License do ...@@ -83,7 +83,7 @@ describe License do
active_user_count: User.active.count - 1 active_user_count: User.active.count - 1
} }
allow(license).to receive(:historical_max).and_return(0) HistoricalData.delete_all
end end
context 'with previous_user_count and active users above of license limit' do context 'with previous_user_count and active users above of license limit' do
......
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