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
find_by(date: date)
end
def max_historical_user_count
exp_date = License.current&.expires_at || Date.today
def max_historical_user_count(license: nil, from: nil, to: nil)
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
......@@ -406,10 +406,7 @@ class License < ApplicationRecord
end
def historical_max(from = nil, to = nil)
from ||= starts_at - 1.year
to ||= starts_at
HistoricalData.during(from..to).maximum(:active_user_count) || 0
HistoricalData.max_historical_user_count(license: self, from: from, to: to)
end
def historical_max_with_default_period
......@@ -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.")
end
def empty_historical_max?
historical_max == 0
def prior_historical_max
@prior_historical_max ||= begin
from = starts_at - 1.year
to = starts_at
historical_max(from, to)
end
end
def check_users_limit
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
else
return if restricted_user_count >= historical_max
return if restricted_user_count >= prior_historical_max
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
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
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
(1..3).each do |i|
described_class.create!(date: Time.now - i.days, active_user_count: i * 100)
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)
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
before do
......@@ -77,7 +99,7 @@ describe HistoricalData do
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) }
context 'with stats before the license period' do
......@@ -111,4 +133,5 @@ describe HistoricalData do
end
end
end
end
end
......@@ -83,7 +83,7 @@ describe License do
active_user_count: User.active.count - 1
}
allow(license).to receive(:historical_max).and_return(0)
HistoricalData.delete_all
end
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