Commit 8fac0f36 authored by Fabio Pitino's avatar Fabio Pitino Committed by Jan Provaznik

Refactor project shared_runners_seconds to use new tracking

parent 958b6550
......@@ -31,7 +31,6 @@ class ProjectStatistics < ApplicationRecord
scope :for_project_ids, ->(project_ids) { where(project_id: project_ids) }
scope :for_namespaces, -> (namespaces) { where(namespace: namespaces) }
scope :with_any_ci_minutes_used, -> { where.not(shared_runners_seconds: 0) }
def total_repository_size
repository_size + lfs_objects_size
......
......@@ -252,6 +252,12 @@ module EE
@ci_minutes_quota ||= ::Ci::Minutes::Quota.new(self)
end
def new_monthly_ci_minutes_enabled?
strong_memoize(:new_monthly_ci_minutes_enabled) do
::Feature.enabled?(:ci_use_new_monthly_minutes, self, default_enabled: :yaml)
end
end
# The same method name is used also at project level
def shared_runners_minutes_limit_enabled?
any_project_with_shared_runners_enabled? && ci_minutes_quota.enabled?
......
......@@ -190,7 +190,7 @@ module EE
.order(excess_arel.desc)
end
delegate :shared_runners_minutes, :shared_runners_seconds, :shared_runners_seconds_last_reset,
delegate :shared_runners_seconds, :shared_runners_seconds_last_reset,
to: :statistics, allow_nil: true
delegate :ci_minutes_quota, to: :shared_runners_limit_namespace
......@@ -363,6 +363,14 @@ module EE
!disable_overriding_approvers_per_merge_request
end
def ci_minutes_used(namespace_actor)
if namespace_actor.new_monthly_ci_minutes_enabled?
ci_minutes_usage.amount_used.to_i
else
shared_runners_seconds.to_i / 60
end
end
def shared_runners_available?
super && !ci_minutes_quota.minutes_used_up?
end
......@@ -831,6 +839,12 @@ module EE
private
def ci_minutes_usage
strong_memoize(:ci_minutes_usage) do
::Ci::Minutes::ProjectMonthlyUsage.find_or_create_current(project_id: id)
end
end
def github_integration_enabled?
feature_available?(:github_project_service_integration)
end
......
# frozen_string_literal: true
module EE
module ProjectStatistics
def shared_runners_minutes
shared_runners_seconds.to_i / 60
end
end
end
......@@ -8,7 +8,6 @@ class NamespaceStatistics < ApplicationRecord
validates :namespace, presence: true
scope :for_namespaces, -> (namespaces) { where(namespace: namespaces) }
scope :with_any_ci_minutes_used, -> { where.not(shared_runners_seconds: 0) }
before_save :update_storage_size
after_save :update_root_storage_statistics, if: :saved_change_to_storage_size?
......
......@@ -98,17 +98,19 @@ module Ci
Arel::Nodes::NamedFunction.new(name, attrs)
end
# rubocop: disable CodeReuse/ActiveRecord
def reset_shared_runners_seconds!(namespaces)
NamespaceStatistics
.for_namespaces(namespaces)
.with_any_ci_minutes_used
.where.not(shared_runners_seconds: 0)
.update_all(shared_runners_seconds: 0, shared_runners_seconds_last_reset: Time.current)
::ProjectStatistics
.for_namespaces(namespaces)
.with_any_ci_minutes_used
.where.not(shared_runners_seconds: 0)
.update_all(shared_runners_seconds: 0, shared_runners_seconds_last_reset: Time.current)
end
# rubocop: enable CodeReuse/ActiveRecord
def reset_ci_minutes_notifications!(namespaces)
namespaces.without_last_ci_minutes_notification.update_all(
......
......@@ -65,7 +65,7 @@
= project_icon(project, alt: '', class: 'avatar project-avatar s20')
%strong= link_to project.full_name, project
%td
= project.shared_runners_minutes
= project.ci_minutes_used(namespace)
- if projects.blank?
%tr
%td{ colspan: 2 }
......
---
name: ci_use_new_monthly_minutes
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/71180
rollout_issue_url: https://gitlab.com/gitlab-org/gitlab/-/issues/341730
milestone: '14.4'
type: development
group: group::pipeline execution
default_enabled: false
......@@ -667,6 +667,22 @@ RSpec.describe Namespace do
end
end
describe '#new_monthly_ci_minutes_enabled?' do
subject { namespace.new_monthly_ci_minutes_enabled? }
context 'when feature flag ci_use_new_monthly_minutes is enabled' do
it { is_expected.to be_truthy }
end
context 'when feature flag ci_use_new_monthly_minutes is disabled' do
before do
stub_feature_flags(ci_use_new_monthly_minutes: false)
end
it { is_expected.to be_falsy }
end
end
describe '#shared_runners_minutes_limit_enabled?' do
subject { namespace.shared_runners_minutes_limit_enabled? }
......
......@@ -10,7 +10,6 @@ RSpec.describe Project do
let(:project) { create(:project) }
describe 'associations' do
it { is_expected.to delegate_method(:shared_runners_minutes).to(:statistics) }
it { is_expected.to delegate_method(:shared_runners_seconds).to(:statistics) }
it { is_expected.to delegate_method(:shared_runners_seconds_last_reset).to(:statistics) }
......@@ -1191,6 +1190,44 @@ RSpec.describe Project do
end
end
describe '#ci_minutes_used' do
subject { project.ci_minutes_used(project.namespace) }
context 'when CI minutes have not been used' do
it { is_expected.to be_zero }
context 'when ci_use_new_monthly_minutes feature flag is disabled' do
before do
stub_feature_flags(ci_use_new_monthly_minutes: false)
end
it { is_expected.to be_zero }
end
end
context 'when CI minutes have been used' do
let(:minutes_used) { 70.3 }
# this difference in minutes is purely to test that the value
# comes from the expected table
let(:legacy_minutes_used) { 60.3 }
before do
create(:project_statistics, project: project, shared_runners_seconds: legacy_minutes_used.minutes)
create(:ci_project_monthly_usage, project: project, amount_used: minutes_used )
end
it { is_expected.to eq(70) }
context 'when ci_use_new_monthly_minutes feature flag is disabled' do
before do
stub_feature_flags(ci_use_new_monthly_minutes: false)
end
it { is_expected.to eq(60) }
end
end
end
describe '#root_namespace' do
let(:project) { build(:project, namespace: parent) }
......
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