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