Commit 734d6fcdf00846364fa39fc374753b7ea2cef603
1 parent
41bbbb6d
Exists in
master
and in
4 other branches
Perfomance updating Project activity sort
Showing
5 changed files
with
32 additions
and
7 deletions
Show diff stats
app/models/project.rb
@@ -29,7 +29,7 @@ class Project < ActiveRecord::Base | @@ -29,7 +29,7 @@ class Project < ActiveRecord::Base | ||
29 | 29 | ||
30 | attr_accessible :name, :path, :description, :default_branch, :issues_tracker, | 30 | attr_accessible :name, :path, :description, :default_branch, :issues_tracker, |
31 | :issues_enabled, :wall_enabled, :merge_requests_enabled, :snippets_enabled, :issues_tracker_id, | 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 | attr_accessible :namespace_id, :creator_id, as: :admin | 34 | attr_accessible :namespace_id, :creator_id, as: :admin |
35 | 35 | ||
@@ -92,12 +92,12 @@ class Project < ActiveRecord::Base | @@ -92,12 +92,12 @@ class Project < ActiveRecord::Base | ||
92 | scope :in_team, ->(team) { where("id IN (:ids)", ids: team.projects.map(&:id)) } | 92 | scope :in_team, ->(team) { where("id IN (:ids)", ids: team.projects.map(&:id)) } |
93 | scope :in_namespace, ->(namespace) { where(namespace_id: namespace.id) } | 93 | scope :in_namespace, ->(namespace) { where(namespace_id: namespace.id) } |
94 | scope :in_group_namespace, -> { joins(:group) } | 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 | scope :personal, ->(user) { where(namespace_id: user.namespace_id) } | 96 | scope :personal, ->(user) { where(namespace_id: user.namespace_id) } |
97 | scope :joined, ->(user) { where("namespace_id != ?", user.namespace_id) } | 97 | scope :joined, ->(user) { where("namespace_id != ?", user.namespace_id) } |
98 | scope :public_only, -> { where(public: true) } | 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 | class << self | 102 | class << self |
103 | def abandoned | 103 | def abandoned |
@@ -157,8 +157,7 @@ class Project < ActiveRecord::Base | @@ -157,8 +157,7 @@ class Project < ActiveRecord::Base | ||
157 | unless creator.can_create_project? | 157 | unless creator.can_create_project? |
158 | errors[:limit_reached] << ("Your own projects limit is #{creator.projects_limit}! Please contact administrator to increase it") | 158 | errors[:limit_reached] << ("Your own projects limit is #{creator.projects_limit}! Please contact administrator to increase it") |
159 | end | 159 | end |
160 | - rescue => ex | ||
161 | - errors[:base] << ex.message | 160 | + rescue |
162 | errors[:base] << ("Can't check your ability to create project") | 161 | errors[:base] << ("Can't check your ability to create project") |
163 | end | 162 | end |
164 | 163 | ||
@@ -191,7 +190,7 @@ class Project < ActiveRecord::Base | @@ -191,7 +190,7 @@ class Project < ActiveRecord::Base | ||
191 | end | 190 | end |
192 | 191 | ||
193 | def last_activity_date | 192 | def last_activity_date |
194 | - last_event.try(:created_at) || updated_at | 193 | + last_activity_at |
195 | end | 194 | end |
196 | 195 | ||
197 | def project_id | 196 | def project_id |
config/application.rb
@@ -24,6 +24,7 @@ module Gitlab | @@ -24,6 +24,7 @@ module Gitlab | ||
24 | 24 | ||
25 | # Activate observers that should always be running. | 25 | # Activate observers that should always be running. |
26 | config.active_record.observers = :activity_observer, | 26 | config.active_record.observers = :activity_observer, |
27 | + :project_activity_cache_observer, | ||
27 | :issue_observer, | 28 | :issue_observer, |
28 | :key_observer, | 29 | :key_observer, |
29 | :merge_request_observer, | 30 | :merge_request_observer, |
db/migrate/20130403003950_add_last_activity_column_into_project.rb
0 → 100644
@@ -0,0 +1,15 @@ | @@ -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,7 +11,7 @@ | ||
11 | # | 11 | # |
12 | # It's strongly recommended to check this file into your version control system. | 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 | create_table "events", :force => true do |t| | 16 | create_table "events", :force => true do |t| |
17 | t.string "target_type" | 17 | t.string "target_type" |
@@ -156,9 +156,11 @@ ActiveRecord::Schema.define(:version => 20130325173941) do | @@ -156,9 +156,11 @@ ActiveRecord::Schema.define(:version => 20130325173941) do | ||
156 | t.string "issues_tracker", :default => "gitlab", :null => false | 156 | t.string "issues_tracker", :default => "gitlab", :null => false |
157 | t.string "issues_tracker_id" | 157 | t.string "issues_tracker_id" |
158 | t.boolean "snippets_enabled", :default => true, :null => false | 158 | t.boolean "snippets_enabled", :default => true, :null => false |
159 | + t.datetime "last_activity_at" | ||
159 | end | 160 | end |
160 | 161 | ||
161 | add_index "projects", ["creator_id"], :name => "index_projects_on_owner_id" | 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 | add_index "projects", ["namespace_id"], :name => "index_projects_on_namespace_id" | 164 | add_index "projects", ["namespace_id"], :name => "index_projects_on_namespace_id" |
163 | 165 | ||
164 | create_table "protected_branches", :force => true do |t| | 166 | create_table "protected_branches", :force => true do |t| |