From e38bfa797f64b58849d661593566ede042f523ff Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Wed, 31 Jul 2013 16:20:30 +0400 Subject: [PATCH] #245: added ProjectStatistic model --- app/models/build_list_observer.rb | 7 ++++--- app/models/project.rb | 1 + app/models/project_statistic.rb | 10 ++++++++++ .../20130731120901_create_project_statistics.rb | 13 +++++++++++++ db/schema.rb | 13 ++++++++++++- 5 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 app/models/project_statistic.rb create mode 100644 db/migrate/20130731120901_create_project_statistics.rb diff --git a/app/models/build_list_observer.rb b/app/models/build_list_observer.rb index dc1921ecf..02ffe37e6 100644 --- a/app/models/build_list_observer.rb +++ b/app/models/build_list_observer.rb @@ -14,9 +14,10 @@ class BuildListObserver < ActiveRecord::Observer if record.status == BuildList::SUCCESS # Update project average build time - build_count = record.project.build_count - new_av_time = ( record.project.average_build_time * build_count + record.duration ) / ( build_count + 1 ) - record.project.update_attributes({ :average_build_time => new_av_time, :build_count => build_count + 1 }, :without_protection => true) + statistic = record.project.project_statistics.find_or_create_by_arch_id(record.arch_id) + build_count = statistic.build_count + new_av_time = ( statistic.average_build_time * build_count + record.duration ) / ( build_count + 1 ) + statistic.update_attributes(:average_build_time => new_av_time, :build_count => build_count) end end end diff --git a/app/models/project.rb b/app/models/project.rb index 9c4ff4c63..87a582205 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -15,6 +15,7 @@ class Project < ActiveRecord::Base has_many :project_to_repositories, :dependent => :destroy has_many :repositories, :through => :project_to_repositories has_many :project_tags, :dependent => :destroy + has_many :project_statistics, :dependent => :destroy has_many :build_lists, :dependent => :destroy has_many :hooks, :dependent => :destroy diff --git a/app/models/project_statistic.rb b/app/models/project_statistic.rb new file mode 100644 index 000000000..3655378dd --- /dev/null +++ b/app/models/project_statistic.rb @@ -0,0 +1,10 @@ +class ProjectStatistic < ActiveRecord::Base + + belongs_to :arch + belongs_to :project + + validates :arch_id, :project_id, :average_build_time, :build_count, :presence => true + validates :project_id, :uniqueness => {:scope => :arch_id} + + attr_accessible :arch_id, :average_build_time, :build_count, :project_id +end diff --git a/db/migrate/20130731120901_create_project_statistics.rb b/db/migrate/20130731120901_create_project_statistics.rb new file mode 100644 index 000000000..3d7103210 --- /dev/null +++ b/db/migrate/20130731120901_create_project_statistics.rb @@ -0,0 +1,13 @@ +class CreateProjectStatistics < ActiveRecord::Migration + def change + create_table :project_statistics do |t| + t.integer :average_build_time, :null => false, :default => 0 + t.integer :build_count, :null => false, :default => 0 + t.integer :arch_id, :null => false + t.integer :project_id, :null => false + + t.timestamps + end + add_index :project_statistics, [:project_id, :arch_id], :unique => true + end +end diff --git a/db/schema.rb b/db/schema.rb index b1cd1d2d7..9fda0ca1c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20130724105821) do +ActiveRecord::Schema.define(:version => 20130731120901) do create_table "activity_feeds", :force => true do |t| t.integer "user_id", :null => false @@ -376,6 +376,17 @@ ActiveRecord::Schema.define(:version => 20130724105821) do add_index "project_imports", ["platform_id", "name"], :name => "index_project_imports_on_name_and_platform_id", :unique => true, :case_sensitive => false + create_table "project_statistics", :force => true do |t| + t.integer "average_build_time", :default => 0, :null => false + t.integer "build_count", :default => 0, :null => false + t.integer "arch_id", :null => false + t.integer "project_id", :null => false + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false + end + + add_index "project_statistics", ["project_id", "arch_id"], :name => "index_project_statistics_on_project_id_and_arch_id", :unique => true + create_table "project_tags", :force => true do |t| t.integer "project_id" t.string "commit_id"