Commit b5b9b321 authored by Kamil Trzcinski's avatar Kamil Trzcinski

Add project_metrics

parent 3e40f2fc
......@@ -42,7 +42,8 @@ class Namespace < ActiveRecord::Base
scope :root, -> { where('type IS NULL') }
delegate :shared_runners_minutes, to: :namespace_metrics, allow_nil: true
delegate :shared_runners_minutes, :shared_runners_minutes_last_reset,
to: :namespace_metrics, allow_nil: true
class << self
def by_path(path)
......
......@@ -28,9 +28,11 @@ class Project < ActiveRecord::Base
:merge_requests_enabled?, :issues_enabled?, to: :project_feature,
allow_nil: true
delegate :shared_runners_minutes, :shared_runners_minutes_limit,
:shared_runners_minutes_used?,
to: :namespace, allow_nil: true
delegate :shared_runners_minutes, :shared_runners_minutes_last_reset,
to: :project_metrics, allow_nil: true
delegate :shared_runners_minutes_limit_enabled?, :shared_runners_minutes_limit,
:shared_runners_minutes_used?, to: :namespace
default_value_for :archived, false
default_value_for :visibility_level, gitlab_config_features.visibility_level
......@@ -74,6 +76,8 @@ class Project < ActiveRecord::Base
belongs_to :namespace
belongs_to :mirror_user, foreign_key: 'mirror_user_id', class_name: 'User'
has_one :project_metrics, dependent: :destroy
has_one :push_rule, dependent: :destroy
has_one :last_event, -> {order 'events.created_at DESC'}, class_name: 'Event'
has_many :boards, dependent: :destroy
......@@ -1237,7 +1241,7 @@ class Project < ActiveRecord::Base
end
shared_runners_enabled? &&
!shared_runners_minutes_used? &&
!namespace.shared_runners_minutes_used? &&
Ci::Runner.shared.active.any?(&block)
end
......
class ProjectMetrics < ActiveRecord::Base
belongs_to :project
validates :project, presence: true
end
......@@ -6,6 +6,9 @@ class UpdateBuildMinutesService < BaseService
return unless build.project
return unless build.project.shared_runners_minutes_limit_enabled?
project.find_or_create_project_metrics.
update_all('shared_runners_minutes = shared_runners_minutes + ?', build.duration)
project.namespace.find_or_create_namespace_metrics.
update_all('shared_runners_minutes = shared_runners_minutes + ?', build.duration)
end
......
......@@ -3,6 +3,12 @@ class ClearSharedRunnerMinutesWorker
include DedicatedSidekiqQueue
def perform
NamespaceMetrics.update_all(shared_runners_minutes: 0)
ProjectMetrics.update_all(
shared_runners_minutes: 0,
shared_runners_minutes_last_reset: Time.now)
NamespaceMetrics.update_all(
shared_runners_minutes: 0,
shared_runners_minutes_last_reset: Time.now)
end
end
......@@ -5,8 +5,9 @@ class CreateTableNamespaceMetrics < ActiveRecord::Migration
def change
create_table :namespace_metrics do |t|
t.integer :namespace_id, null: false
t.integer :namespace_id, null: false, unique: true
t.integer :shared_runners_minutes, default: 0, null: false
t.timestamp :shared_runners_minutes_last_reset
end
add_foreign_key :namespace_metrics, :namespaces, on_delete: :cascade
......
class CreateTableProjectMetrics < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
def change
create_table :project_metrics do |t|
t.integer :project_id, null: false, unique: true
t.integer :shared_runners_minutes, default: 0, null: false
t.timestamp :shared_runners_minutes_last_reset
end
add_foreign_key :project_metrics, :projects, on_delete: :cascade
end
end
class AddIndexToNamespaceMetrics < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
disable_ddl_transaction!
def change
add_concurrent_index :project_metrics, [:project_id], { unique: true }
end
end
FactoryGirl.define do
factory :namespace_metrics do
trait :with_used_limit do
shared_runners_minutes 1000
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