Commit 8825f453 authored by Fabio Pitino's avatar Fabio Pitino Committed by Luke Duncalfe

Move last reset date of CI minutes behind abstraction method

The method `shared_runners_seconds_last_reset` reads from legacy
tables where CI minutes are tracked. In preparation to switch to
a new monthly tracking (different tables) we need to move this
attribute reader method behind an abstraction so we can eventually
switch the source via a feature flag.

https://gitlab.com/gitlab-org/gitlab/-/issues/277448
parent 46cae225
......@@ -47,6 +47,14 @@ module Ci
end
end
def reset_date
strong_memoize(:reset_date) do
# TODO: use namespace.new_monthly_ci_minutes_enabled? to switch to
# ::Ci::Minutes::NamespaceMonthlyUsage.find_or_create_current(namespace.id).date
namespace.namespace_statistics&.shared_runners_seconds_last_reset
end
end
def purchased_minutes
strong_memoize(:purchased_minutes) do
namespace.extra_shared_runners_minutes_limit.to_i
......
......@@ -79,8 +79,6 @@ module EE
.or(where.not(last_ci_minutes_usage_notification_level: nil))
end
delegate :shared_runners_seconds_last_reset, to: :namespace_statistics, allow_nil: true
delegate :additional_purchased_storage_size, :additional_purchased_storage_size=,
:additional_purchased_storage_ends_on, :additional_purchased_storage_ends_on=,
:temporary_storage_increase_ends_on, :temporary_storage_increase_ends_on=,
......
......@@ -190,8 +190,7 @@ module EE
.order(excess_arel.desc)
end
delegate :shared_runners_seconds, :shared_runners_seconds_last_reset,
to: :statistics, allow_nil: true
delegate :shared_runners_seconds, to: :statistics, allow_nil: true
delegate :ci_minutes_quota, to: :shared_runners_limit_namespace
......
......@@ -16,7 +16,7 @@
.row
.col-sm-6
%strong
- last_reset = namespace.shared_runners_seconds_last_reset
- last_reset = minutes_quota.reset_date
- if last_reset
= s_('UsageQuota|Usage since')
= last_reset.strftime('%b %d, %Y')
......
......@@ -15,7 +15,11 @@ FactoryBot.modify do
after(:create) do |namespace, evaluator|
if evaluator.ci_minutes_used
create(:ci_namespace_monthly_usage, namespace: namespace, amount_used: evaluator.ci_minutes_used)
create(:namespace_statistics, namespace: namespace, shared_runners_seconds: evaluator.ci_minutes_used.minutes)
create(:namespace_statistics,
namespace: namespace,
shared_runners_seconds: evaluator.ci_minutes_used.minutes,
shared_runners_seconds_last_reset: Time.current)
end
end
end
......
......@@ -13,7 +13,7 @@ RSpec.describe 'Reset namespace pipeline minutes', :js do
end
shared_examples 'resetting pipeline minutes' do
context 'when namespace has namespace statistics' do
context 'when namespace has minutes used' do
before do
set_ci_minutes_used(namespace, 100)
end
......@@ -29,7 +29,7 @@ RSpec.describe 'Reset namespace pipeline minutes', :js do
expect(current_path).to include(namespace.path)
expect(namespace.reload.ci_minutes_quota.total_minutes_used).to eq(0)
expect(namespace.namespace_statistics.reload.shared_runners_seconds_last_reset).to be_like_time(time)
expect(namespace.ci_minutes_quota.reset_date).to be_like_time(time)
end
end
end
......
......@@ -189,7 +189,7 @@ RSpec.describe Ci::Minutes::Quota do
end
end
describe 'purchased_minutes_used_up?' do
describe '#purchased_minutes_used_up?' do
subject { quota.purchased_minutes_used_up? }
context 'when quota is enabled' do
......@@ -234,4 +234,20 @@ RSpec.describe Ci::Minutes::Quota do
it { is_expected.to be_falsey }
end
end
describe '#reset_date' do
subject(:reset_date) { quota.reset_date }
let(:reset_time) { Date.new(2021, 10, 14) }
let(:namespace) do
travel_to(reset_time) do
create(:namespace, :with_ci_minutes)
end
end
it 'corresponds to the reset time' do
expect(reset_date).to eq(reset_time)
end
end
end
......@@ -20,7 +20,6 @@ RSpec.describe Namespace do
it { is_expected.to have_one :upcoming_reconciliation }
it { is_expected.to have_many(:ci_minutes_additional_packs) }
it { is_expected.to delegate_method(:shared_runners_seconds_last_reset).to(:namespace_statistics) }
it { is_expected.to delegate_method(:trial?).to(:gitlab_subscription) }
it { is_expected.to delegate_method(:trial_ends_on).to(:gitlab_subscription) }
it { is_expected.to delegate_method(:trial_starts_on).to(:gitlab_subscription) }
......
......@@ -11,7 +11,6 @@ RSpec.describe Project do
describe 'associations' do
it { is_expected.to delegate_method(:shared_runners_seconds).to(:statistics) }
it { is_expected.to delegate_method(:shared_runners_seconds_last_reset).to(:statistics) }
it { is_expected.to delegate_method(:ci_minutes_quota).to(:shared_runners_limit_namespace) }
it { is_expected.to delegate_method(:shared_runners_minutes_limit_enabled?).to(:shared_runners_limit_namespace) }
......
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