Commit 734d6fcdf00846364fa39fc374753b7ea2cef603

Authored by Andrey Kumanyaev
1 parent 41bbbb6d

Perfomance updating Project activity sort

app/models/project.rb
... ... @@ -29,7 +29,7 @@ class Project < ActiveRecord::Base
29 29  
30 30 attr_accessible :name, :path, :description, :default_branch, :issues_tracker,
31 31 :issues_enabled, :wall_enabled, :merge_requests_enabled, :snippets_enabled, :issues_tracker_id,
32   - :wiki_enabled, :public, :import_url, as: [:default, :admin]
  32 + :wiki_enabled, :public, :import_url, :last_activity_at, as: [:default, :admin]
33 33  
34 34 attr_accessible :namespace_id, :creator_id, as: :admin
35 35  
... ... @@ -92,12 +92,12 @@ class Project < ActiveRecord::Base
92 92 scope :in_team, ->(team) { where("id IN (:ids)", ids: team.projects.map(&:id)) }
93 93 scope :in_namespace, ->(namespace) { where(namespace_id: namespace.id) }
94 94 scope :in_group_namespace, -> { joins(:group) }
95   - scope :sorted_by_activity, -> { order("(SELECT max(events.created_at) FROM events WHERE events.project_id = projects.id) DESC") }
  95 + scope :sorted_by_activity, -> { order("last_activity_at DESC") }
96 96 scope :personal, ->(user) { where(namespace_id: user.namespace_id) }
97 97 scope :joined, ->(user) { where("namespace_id != ?", user.namespace_id) }
98 98 scope :public_only, -> { where(public: true) }
99 99  
100   - enumerize :issues_tracker, :in => (Gitlab.config.issues_tracker.keys).append(:gitlab), :default => :gitlab
  100 + enumerize :issues_tracker, in: (Gitlab.config.issues_tracker.keys).append(:gitlab), default: :gitlab
101 101  
102 102 class << self
103 103 def abandoned
... ... @@ -157,8 +157,7 @@ class Project &lt; ActiveRecord::Base
157 157 unless creator.can_create_project?
158 158 errors[:limit_reached] << ("Your own projects limit is #{creator.projects_limit}! Please contact administrator to increase it")
159 159 end
160   - rescue => ex
161   - errors[:base] << ex.message
  160 + rescue
162 161 errors[:base] << ("Can't check your ability to create project")
163 162 end
164 163  
... ... @@ -191,7 +190,7 @@ class Project &lt; ActiveRecord::Base
191 190 end
192 191  
193 192 def last_activity_date
194   - last_event.try(:created_at) || updated_at
  193 + last_activity_at
195 194 end
196 195  
197 196 def project_id
... ...
app/observers/project_activity_cache_observer.rb 0 → 100644
... ... @@ -0,0 +1,8 @@
  1 +class ProjectActivityCacheObserver < BaseObserver
  2 + observe :event
  3 +
  4 + def after_create(event)
  5 + event.project.update_attribute(:last_activity_at, event.created_at) if event.project
  6 + end
  7 +end
  8 +
... ...
config/application.rb
... ... @@ -24,6 +24,7 @@ module Gitlab
24 24  
25 25 # Activate observers that should always be running.
26 26 config.active_record.observers = :activity_observer,
  27 + :project_activity_cache_observer,
27 28 :issue_observer,
28 29 :key_observer,
29 30 :merge_request_observer,
... ...
db/migrate/20130403003950_add_last_activity_column_into_project.rb 0 → 100644
... ... @@ -0,0 +1,15 @@
  1 +class AddLastActivityColumnIntoProject < ActiveRecord::Migration
  2 + def up
  3 + add_column :projects, :last_activity_at, :datetime
  4 + add_index :projects, :last_activity_at
  5 +
  6 + Project.find_each do |project|
  7 + project.update_attribute(:last_activity_at, project.last_activity_date)
  8 + end
  9 + end
  10 +
  11 + def down
  12 + remove_index :projects, :last_activity_at
  13 + remove_column :projects, :last_activity_at
  14 + end
  15 +end
... ...
db/schema.rb
... ... @@ -11,7 +11,7 @@
11 11 #
12 12 # It's strongly recommended to check this file into your version control system.
13 13  
14   -ActiveRecord::Schema.define(:version => 20130325173941) do
  14 +ActiveRecord::Schema.define(:version => 20130403003950) do
15 15  
16 16 create_table "events", :force => true do |t|
17 17 t.string "target_type"
... ... @@ -156,9 +156,11 @@ ActiveRecord::Schema.define(:version =&gt; 20130325173941) do
156 156 t.string "issues_tracker", :default => "gitlab", :null => false
157 157 t.string "issues_tracker_id"
158 158 t.boolean "snippets_enabled", :default => true, :null => false
  159 + t.datetime "last_activity_at"
159 160 end
160 161  
161 162 add_index "projects", ["creator_id"], :name => "index_projects_on_owner_id"
  163 + add_index "projects", ["last_activity_at"], :name => "index_projects_on_last_activity_at"
162 164 add_index "projects", ["namespace_id"], :name => "index_projects_on_namespace_id"
163 165  
164 166 create_table "protected_branches", :force => true do |t|
... ...