Commit 08dc2c7c authored by Kamil Trzcinski's avatar Kamil Trzcinski

Rename shared_runners_minutes to be shared_runners_seconds when calculating data

parent 71165067
module EE module EE
module GroupsHelper module GroupsHelper
def group_shared_runner_limits_quota(group) def group_shared_runner_limits_quota(group)
used = group.shared_runners_minutes used = group.shared_runners_minutes.to_i
if group.shared_runners_minutes_limit_enabled? if group.shared_runners_minutes_limit_enabled?
limit = group.actual_shared_runners_minutes_limit limit = group.actual_shared_runners_minutes_limit
...@@ -19,7 +19,7 @@ module EE ...@@ -19,7 +19,7 @@ module EE
def group_shared_runner_limits_percent_used(group) def group_shared_runner_limits_percent_used(group)
return 0 unless group.shared_runners_minutes_limit_enabled? return 0 unless group.shared_runners_minutes_limit_enabled?
100 * group.shared_runners_minutes / group.actual_shared_runners_minutes_limit 100 * group.shared_runners_minutes.to_i / group.actual_shared_runners_minutes_limit
end end
def group_shared_runner_limits_progress_bar(group) def group_shared_runner_limits_progress_bar(group)
......
...@@ -9,7 +9,7 @@ module EE ...@@ -9,7 +9,7 @@ module EE
prepended do prepended do
has_one :namespace_statistics, dependent: :destroy has_one :namespace_statistics, dependent: :destroy
delegate :shared_runners_minutes, :shared_runners_minutes_last_reset, delegate :shared_runners_minutes, :shared_runners_seconds, :shared_runners_seconds_last_reset,
to: :namespace_statistics, allow_nil: true to: :namespace_statistics, allow_nil: true
end end
......
...@@ -9,7 +9,7 @@ module EE ...@@ -9,7 +9,7 @@ module EE
prepended do prepended do
scope :with_shared_runners_limit_enabled, -> { with_shared_runners.non_public_only } scope :with_shared_runners_limit_enabled, -> { with_shared_runners.non_public_only }
delegate :shared_runners_minutes, :shared_runners_minutes_last_reset, delegate :shared_runners_minutes, :shared_runners_seconds, :shared_runners_seconds_last_reset,
to: :statistics, allow_nil: true to: :statistics, allow_nil: true
delegate :actual_shared_runners_minutes_limit, delegate :actual_shared_runners_minutes_limit,
......
...@@ -2,4 +2,8 @@ class NamespaceStatistics < ActiveRecord::Base ...@@ -2,4 +2,8 @@ class NamespaceStatistics < ActiveRecord::Base
belongs_to :namespace belongs_to :namespace
validates :namespace, presence: true validates :namespace, presence: true
def shared_runners_minutes
shared_runners_seconds.to_i / 60.0
end
end end
...@@ -7,6 +7,10 @@ class ProjectStatistics < ActiveRecord::Base ...@@ -7,6 +7,10 @@ class ProjectStatistics < ActiveRecord::Base
STORAGE_COLUMNS = [:repository_size, :lfs_objects_size, :build_artifacts_size] STORAGE_COLUMNS = [:repository_size, :lfs_objects_size, :build_artifacts_size]
STATISTICS_COLUMNS = [:commit_count] + STORAGE_COLUMNS STATISTICS_COLUMNS = [:commit_count] + STORAGE_COLUMNS
def shared_runners_minutes
shared_runners_seconds.to_i / 60.0
end
def total_repository_size def total_repository_size
repository_size + lfs_objects_size repository_size + lfs_objects_size
end end
......
...@@ -21,7 +21,7 @@ module EE ...@@ -21,7 +21,7 @@ module EE
where('namespaces.id = projects.namespace_id'). where('namespaces.id = projects.namespace_id').
joins('LEFT JOIN namespace_statistics ON namespace_statistics.namespace_id = namespaces.id'). joins('LEFT JOIN namespace_statistics ON namespace_statistics.namespace_id = namespaces.id').
where('COALESCE(namespaces.shared_runners_minutes_limit, ?, 0) = 0 OR ' \ where('COALESCE(namespaces.shared_runners_minutes_limit, ?, 0) = 0 OR ' \
'COALESCE(namespace_statistics.shared_runners_minutes, 0) < COALESCE(namespaces.shared_runners_minutes_limit, ?, 0)', 'COALESCE(namespace_statistics.shared_runners_seconds, 0) < COALESCE(namespaces.shared_runners_minutes_limit, ?, 0) * 60',
application_shared_runners_minutes, application_shared_runners_minutes). application_shared_runners_minutes, application_shared_runners_minutes).
select('1') select('1')
end end
......
...@@ -4,11 +4,11 @@ class UpdateBuildMinutesService < BaseService ...@@ -4,11 +4,11 @@ class UpdateBuildMinutesService < BaseService
return unless build.complete? return unless build.complete?
return unless build.duration return unless build.duration
ProjectStatistics.update_counters(project.statistics, ProjectStatistics.update_counters(project_statistics,
shared_runners_minutes: build.duration) shared_runners_seconds: build.duration)
NamespaceStatistics.update_counters(namespace_statistics, NamespaceStatistics.update_counters(namespace_statistics,
shared_runners_minutes: build.duration) shared_runners_seconds: build.duration)
end end
private private
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
.row .row
.col-sm-6 .col-sm-6
%strong %strong
- last_reset = @group.shared_runners_minutes_last_reset - last_reset = @group.shared_runners_seconds_last_reset
- if last_reset - if last_reset
Usage since Usage since
= last_reset.strftime('%b %d, %Y') = last_reset.strftime('%b %d, %Y')
......
...@@ -8,12 +8,12 @@ class ClearSharedRunnersMinutesWorker ...@@ -8,12 +8,12 @@ class ClearSharedRunnersMinutesWorker
return unless try_obtain_lease return unless try_obtain_lease
ProjectStatistics.update_all( ProjectStatistics.update_all(
shared_runners_minutes: 0, shared_runners_seconds: 0,
shared_runners_minutes_last_reset: Time.now) shared_runners_seconds_last_reset: Time.now)
NamespaceStatistics.update_all( NamespaceStatistics.update_all(
shared_runners_minutes: 0, shared_runners_seconds: 0,
shared_runners_minutes_last_reset: Time.now) shared_runners_seconds_last_reset: Time.now)
end end
private private
......
...@@ -6,8 +6,8 @@ class CreateTableNamespaceStatistics < ActiveRecord::Migration ...@@ -6,8 +6,8 @@ class CreateTableNamespaceStatistics < ActiveRecord::Migration
def change def change
create_table :namespace_statistics do |t| create_table :namespace_statistics do |t|
t.references :namespace, null: false, index: { unique: true }, foreign_key: { on_delete: :cascade } t.references :namespace, null: false, index: { unique: true }, foreign_key: { on_delete: :cascade }
t.integer :shared_runners_minutes, default: 0, null: false t.integer :shared_runners_seconds, default: 0, null: false
t.timestamp :shared_runners_minutes_last_reset t.timestamp :shared_runners_seconds_last_reset
end end
end end
end end
class AddSharedRunnersMinutesToProjectStatistics < ActiveRecord::Migration class AddSharedRunnersSecondsToProjectStatistics < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers include Gitlab::Database::MigrationHelpers
DOWNTIME = false DOWNTIME = false
def change def change
counter_column = { limit: 8, null: false, default: 0 } counter_column = { limit: 8, null: false, default: 0 }
add_column :project_statistics, :shared_runners_minutes, :integer, counter_column add_column :project_statistics, :shared_runners_seconds, :integer, counter_column
add_column :project_statistics, :shared_runners_minutes_last_reset, :timestamp add_column :project_statistics, :shared_runners_seconds_last_reset, :timestamp
end end
end end
...@@ -827,8 +827,8 @@ ActiveRecord::Schema.define(version: 20170106172237) do ...@@ -827,8 +827,8 @@ ActiveRecord::Schema.define(version: 20170106172237) do
create_table "namespace_statistics", force: :cascade do |t| create_table "namespace_statistics", force: :cascade do |t|
t.integer "namespace_id", null: false t.integer "namespace_id", null: false
t.integer "shared_runners_minutes", default: 0, null: false t.integer "shared_runners_seconds", default: 0, null: false
t.datetime "shared_runners_minutes_last_reset" t.datetime "shared_runners_seconds_last_reset"
end end
add_index "namespace_statistics", ["namespace_id"], name: "index_namespace_statistics_on_namespace_id", unique: true, using: :btree add_index "namespace_statistics", ["namespace_id"], name: "index_namespace_statistics_on_namespace_id", unique: true, using: :btree
...@@ -1050,8 +1050,8 @@ ActiveRecord::Schema.define(version: 20170106172237) do ...@@ -1050,8 +1050,8 @@ ActiveRecord::Schema.define(version: 20170106172237) do
t.integer "repository_size", limit: 8, default: 0, null: false t.integer "repository_size", limit: 8, default: 0, null: false
t.integer "lfs_objects_size", limit: 8, default: 0, null: false t.integer "lfs_objects_size", limit: 8, default: 0, null: false
t.integer "build_artifacts_size", limit: 8, default: 0, null: false t.integer "build_artifacts_size", limit: 8, default: 0, null: false
t.integer "shared_runners_minutes", limit: 8, default: 0, null: false t.integer "shared_runners_seconds", limit: 8, default: 0, null: false
t.datetime "shared_runners_minutes_last_reset" t.datetime "shared_runners_seconds_last_reset"
end end
add_index "project_statistics", ["namespace_id"], name: "index_project_statistics_on_namespace_id", using: :btree add_index "project_statistics", ["namespace_id"], name: "index_project_statistics_on_namespace_id", using: :btree
......
...@@ -5,7 +5,7 @@ FactoryGirl.define do ...@@ -5,7 +5,7 @@ FactoryGirl.define do
owner owner
trait :with_build_minutes do trait :with_build_minutes do
namespace_statistics factory: :namespace_statistics, shared_runners_minutes: 400 namespace_statistics factory: :namespace_statistics, shared_runners_seconds: 400.minutes.to_i
end end
trait :with_build_minutes_limit do trait :with_build_minutes_limit do
...@@ -13,12 +13,12 @@ FactoryGirl.define do ...@@ -13,12 +13,12 @@ FactoryGirl.define do
end end
trait :with_not_used_build_minutes_limit do trait :with_not_used_build_minutes_limit do
namespace_statistics factory: :namespace_statistics, shared_runners_minutes: 300 namespace_statistics factory: :namespace_statistics, shared_runners_seconds: 300.minutes.to_i
shared_runners_minutes_limit 500 shared_runners_minutes_limit 500
end end
trait :with_used_build_minutes_limit do trait :with_used_build_minutes_limit do
namespace_statistics factory: :namespace_statistics, shared_runners_minutes: 1000 namespace_statistics factory: :namespace_statistics, shared_runners_seconds: 1000.minutes.to_i
shared_runners_minutes_limit 500 shared_runners_minutes_limit 500
end end
end end
......
...@@ -9,7 +9,7 @@ describe Prependable do ...@@ -9,7 +9,7 @@ describe Prependable do
end end
it 'can execute a method' do it 'can execute a method' do
expect(subject.class_value).to eq(200) expect(subject.class_value).to eq(20)
end end
end end
......
...@@ -6,7 +6,8 @@ describe Namespace, models: true do ...@@ -6,7 +6,8 @@ describe Namespace, models: true do
it { is_expected.to have_one(:namespace_statistics).dependent(:destroy) } it { is_expected.to have_one(:namespace_statistics).dependent(:destroy) }
it { is_expected.to delegate_method(:shared_runners_minutes).to(:namespace_statistics) } it { is_expected.to delegate_method(:shared_runners_minutes).to(:namespace_statistics) }
it { is_expected.to delegate_method(:shared_runners_minutes_last_reset).to(:namespace_statistics) } it { is_expected.to delegate_method(:shared_runners_seconds).to(:namespace_statistics) }
it { is_expected.to delegate_method(:shared_runners_seconds_last_reset).to(:namespace_statistics) }
describe '#shared_runners_enabled?' do describe '#shared_runners_enabled?' do
subject { namespace.shared_runners_enabled? } subject { namespace.shared_runners_enabled? }
......
...@@ -3,7 +3,8 @@ require 'spec_helper' ...@@ -3,7 +3,8 @@ require 'spec_helper'
describe Project, models: true do describe Project, models: true do
describe 'associations' do describe 'associations' do
it { is_expected.to delegate_method(:shared_runners_minutes).to(:statistics) } it { is_expected.to delegate_method(:shared_runners_minutes).to(:statistics) }
it { is_expected.to delegate_method(:shared_runners_minutes_last_reset).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) }
it { is_expected.to delegate_method(:actual_shared_runners_minutes_limit).to(:namespace) } it { is_expected.to delegate_method(:actual_shared_runners_minutes_limit).to(:namespace) }
it { is_expected.to delegate_method(:shared_runners_minutes_limit_enabled?).to(:namespace) } it { is_expected.to delegate_method(:shared_runners_minutes_limit_enabled?).to(:namespace) }
......
...@@ -4,4 +4,10 @@ describe NamespaceStatistics, models: true do ...@@ -4,4 +4,10 @@ describe NamespaceStatistics, models: true do
it { is_expected.to belong_to(:namespace) } it { is_expected.to belong_to(:namespace) }
it { is_expected.to validate_presence_of(:namespace) } it { is_expected.to validate_presence_of(:namespace) }
describe '#shared_runners_minutes' do
let(:namespace_statistics) { build(:namespace_statistics, shared_runners_seconds: 120) }
it { expect(namespace_statistics.shared_runners_minutes).to eq(2) }
end
end end
...@@ -990,8 +990,8 @@ describe Project, models: true do ...@@ -990,8 +990,8 @@ describe Project, models: true do
context 'when shared runners are disabled for project' do context 'when shared runners are disabled for project' do
let!(:project) { create(:empty_project, shared_runners_enabled: false) } let!(:project) { create(:empty_project, shared_runners_enabled: false) }
it "returns a project" do it "returns an empty array" do
is_expected.to eq([project]) is_expected.to be_empty
end end
end end
end end
...@@ -1155,7 +1155,7 @@ describe Project, models: true do ...@@ -1155,7 +1155,7 @@ describe Project, models: true do
let!(:project) { create(:empty_project, shared_runners_enabled: false) } let!(:project) { create(:empty_project, shared_runners_enabled: false) }
it "returns a empty list" do it "returns a empty list" do
is_expected.to be_nil is_expected.to be_empty
end end
end end
end end
......
...@@ -11,7 +11,7 @@ module Ci ...@@ -11,7 +11,7 @@ module Ci
context 'for project with shared runners when global minutes limit is set' do context 'for project with shared runners when global minutes limit is set' do
before do before do
project.update(shared_runners_enabled: true) project.update(shared_runners_enabled: true)
stub_application_setting(shared_runners_minutes: 500) stub_application_setting(shared_runners_minutes: 100)
end end
context 'allow to pick builds' do context 'allow to pick builds' do
...@@ -23,7 +23,7 @@ module Ci ...@@ -23,7 +23,7 @@ module Ci
context 'when over the global quota' do context 'when over the global quota' do
before do before do
project.namespace.create_namespace_statistics( project.namespace.create_namespace_statistics(
shared_runners_minutes: 600) shared_runners_seconds: 6001)
end end
let(:build) { execute(shared_runner) } let(:build) { execute(shared_runner) }
...@@ -54,7 +54,7 @@ module Ci ...@@ -54,7 +54,7 @@ module Ci
context 'when namespace quota is bigger than a global one' do context 'when namespace quota is bigger than a global one' do
before do before do
project.namespace.update(shared_runners_minutes_limit: 1000) project.namespace.update(shared_runners_minutes_limit: 101)
end end
it "does return the build" do it "does return the build" do
......
...@@ -2,9 +2,9 @@ require 'spec_helper' ...@@ -2,9 +2,9 @@ require 'spec_helper'
describe UpdateBuildMinutesService, services: true do describe UpdateBuildMinutesService, services: true do
context '#perform' do context '#perform' do
let(:namespace) { create(:namespace) } let(:namespace) { create(:namespace, shared_runners_minutes_limit: 100) }
let(:project) { create(:empty_project, namespace: namespace) } let(:project) { create(:empty_project, namespace: namespace) }
let(:pipeline) { create(:ci_pipeline) } let(:pipeline) { create(:ci_pipeline, project: project) }
let(:build) do let(:build) do
create(:ci_build, :success, create(:ci_build, :success,
runner: runner, pipeline: pipeline, runner: runner, pipeline: pipeline,
...@@ -16,29 +16,29 @@ describe UpdateBuildMinutesService, services: true do ...@@ -16,29 +16,29 @@ describe UpdateBuildMinutesService, services: true do
context 'with shared runner' do context 'with shared runner' do
let(:runner) { create(:ci_runner, :shared) } let(:runner) { create(:ci_runner, :shared) }
it "creates a metrics and sets duration" do it "creates a statistics and sets duration" do
subject subject
expect(project.statistics.reload.shared_runners_minutes). expect(project.statistics.reload.shared_runners_seconds).
to eq(build.duration.to_i) to eq(build.duration.to_i)
expect(namespace.namespace_statistics.reload.shared_runners_minutes). expect(namespace.namespace_statistics.reload.shared_runners_seconds).
to eq(build.duration.to_i) to eq(build.duration.to_i)
end end
context 'when metrics are created' do context 'when statistics are created' do
before do before do
project.create_statistics(shared_runners_minutes: 100) project.statistics.update(shared_runners_seconds: 100)
namespace.create_namespace_statistics(shared_runners_minutes: 100) namespace.create_namespace_statistics(shared_runners_seconds: 100)
end end
it "updates metrics and adds duration" do it "updates statistics and adds duration" do
subject subject
expect(project.statistics.reload.shared_runners_minutes). expect(project.statistics.reload.shared_runners_seconds).
to eq(100 + build.duration.to_i) to eq(100 + build.duration.to_i)
expect(namespace.namespace_statistics.reload.shared_runners_minutes). expect(namespace.namespace_statistics.reload.shared_runners_seconds).
to eq(100 + build.duration.to_i) to eq(100 + build.duration.to_i)
end end
end end
...@@ -47,10 +47,9 @@ describe UpdateBuildMinutesService, services: true do ...@@ -47,10 +47,9 @@ describe UpdateBuildMinutesService, services: true do
context 'for specific runner' do context 'for specific runner' do
let(:runner) { create(:ci_runner) } let(:runner) { create(:ci_runner) }
it "does not create metrics" do it "does not create statistics" do
subject subject
expect(project.statistics).to be_nil
expect(namespace.namespace_statistics).to be_nil expect(namespace.namespace_statistics).to be_nil
end end
end end
......
...@@ -16,35 +16,35 @@ describe ClearSharedRunnersMinutesWorker do ...@@ -16,35 +16,35 @@ describe ClearSharedRunnersMinutesWorker do
let(:statistics) { project.statistics } let(:statistics) { project.statistics }
before do before do
statistics.update(shared_runners_minutes: 100) statistics.update(shared_runners_seconds: 100)
end end
it 'clears counters' do it 'clears counters' do
subject subject
expect(statistics.reload.shared_runners_minutes).to be_zero expect(statistics.reload.shared_runners_seconds).to be_zero
end end
it 'resets timer' do it 'resets timer' do
subject subject
expect(statistics.reload.shared_runners_minutes_last_reset).to be_like_time(Time.now) expect(statistics.reload.shared_runners_seconds_last_reset).to be_like_time(Time.now)
end end
end end
context 'when namespace statistics are defined' do context 'when namespace statistics are defined' do
let!(:statistics) { create(:namespace_statistics, shared_runners_minutes: 100) } let!(:statistics) { create(:namespace_statistics, shared_runners_seconds: 100) }
it 'clears counters' do it 'clears counters' do
subject subject
expect(statistics.reload.shared_runners_minutes).to be_zero expect(statistics.reload.shared_runners_seconds).to be_zero
end end
it 'resets timer' do it 'resets timer' do
subject subject
expect(statistics.reload.shared_runners_minutes_last_reset).to be_like_time(Time.now) expect(statistics.reload.shared_runners_seconds_last_reset).to be_like_time(Time.now)
end end
end end
end end
......
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