aboutsummaryrefslogtreecommitdiffstats
path: root/app/controllers/admin_general_controller.rb
diff options
context:
space:
mode:
authorLouise Crow <louise.crow@gmail.com>2012-09-13 16:29:59 +0100
committerLouise Crow <louise.crow@gmail.com>2012-09-13 16:29:59 +0100
commit4f436ae56238faa4ab49b6d299152f04b266fd7a (patch)
treea6a73b67dd3d2129e539350c1a763633eb961aaa /app/controllers/admin_general_controller.rb
parenta622b31a33658696525ad675cdeb1e433f2a7c8b (diff)
parent0ec315c52a731ff149977b9231a15770fa3bd742 (diff)
Merge branch 'feature/faster-admin-timeline' into develop
Diffstat (limited to 'app/controllers/admin_general_controller.rb')
-rw-r--r--app/controllers/admin_general_controller.rb59
1 files changed, 51 insertions, 8 deletions
diff --git a/app/controllers/admin_general_controller.rb b/app/controllers/admin_general_controller.rb
index 2c961dfc5..7e8498d8a 100644
--- a/app/controllers/admin_general_controller.rb
+++ b/app/controllers/admin_general_controller.rb
@@ -61,16 +61,59 @@ class AdminGeneralController < AdminController
@events_title = "Events, all time"
date_back_to = Time.now - 1000.years
end
- @events = InfoRequestEvent.find(:all, :order => "created_at desc, id desc",
- :conditions => ["created_at > ? ", date_back_to.getutc])
- @public_body_history = PublicBody.versioned_class.find(:all, :order => "updated_at desc, id desc",
- :conditions => ["updated_at > ? ", date_back_to.getutc])
- for pbh in @public_body_history
- pbh.created_at = pbh.updated_at
+
+ # Get an array of event attributes within the timespan in the format
+ # [id, type_of_model, event_timestamp]
+ # Note that the relevent date for InfoRequestEvents is creation, but
+ # for PublicBodyVersions is update thoughout
+ connection = InfoRequestEvent.connection
+ timestamps = connection.select_rows("SELECT id,'InfoRequestEvent',
+ created_at AS timestamp
+ FROM info_request_events
+ WHERE created_at > '#{date_back_to.getutc}'
+ UNION
+ SELECT id, 'PublicBodyVersion',
+ updated_at AS timestamp
+ FROM #{PublicBody.versioned_class.table_name}
+ WHERE updated_at > '#{date_back_to.getutc}'
+ ORDER by timestamp desc")
+ @events = WillPaginate::Collection.create((params[:page] or 1), 100) do |pager|
+ # create a hash for each model type being returned
+ info_request_event_ids = {}
+ public_body_version_ids = {}
+ # get the relevant slice from the paginator
+ timestamps.slice(pager.offset, pager.per_page).each_with_index do |event, index|
+ # for each event in the slice, add an item to the hash for the model type
+ # whose key is the model id, and value is the position in the slice
+ if event[1] == 'InfoRequestEvent'
+ info_request_event_ids[event[0].to_i] = index
+ else
+ public_body_version_ids[event[0].to_i] = index
+ end
+ end
+ # get all the models in the slice, eagerly loading the associations we use in the view
+ public_body_versions = PublicBody.versioned_class.find(:all,
+ :conditions => ['id in (?)', public_body_version_ids.keys],
+ :include => [ { :public_body => :translations }])
+ info_request_events = InfoRequestEvent.find(:all,
+ :conditions => ['id in (?)', info_request_event_ids.keys],
+ :include => [:info_request])
+ @events = []
+ # drop the models into a combined array, ordered by their position in the timestamp slice
+ public_body_versions.each do |version|
+ @events[public_body_version_ids[version.id]] = [version, version.updated_at]
+ end
+ info_request_events.each do |event|
+ @events[info_request_event_ids[event.id]] = [event, event.created_at]
+ end
+
+ # inject the result array into the paginated collection:
+ pager.replace(@events)
+
+ # set the total entries for the page to the overall number of results
+ pager.total_entries = timestamps.size
end
- @events += @public_body_history
- @events.sort! { |a,b| b.created_at <=> a.created_at }
end
def stats