From f3a87e7f438178cdada23f9ac1471477bd83aab3 Mon Sep 17 00:00:00 2001 From: Louise Crow Date: Tue, 11 Sep 2012 09:27:14 +0100 Subject: Add some more efficient methods for getting old unclassified request counts, and random sets of small numbers of old unclassified requests. --- app/models/info_request.rb | 45 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 9 deletions(-) (limited to 'app/models/info_request.rb') diff --git a/app/models/info_request.rb b/app/models/info_request.rb index 6f472c290..f2d8929bc 100644 --- a/app/models/info_request.rb +++ b/app/models/info_request.rb @@ -223,7 +223,7 @@ class InfoRequest < ActiveRecord::Base incoming_message.clear_in_database_caches! end end - + # For debugging def InfoRequest.profile_search(query) t = Time.now.usec @@ -939,17 +939,44 @@ public # Used to find when event last changed def InfoRequest.last_event_time_clause(event_type=nil) event_type_clause = '' - event_type_clause = " and info_request_events.event_type = '#{event_type}'" if event_type - "(select created_at from info_request_events where info_request_events.info_request_id = info_requests.id#{event_type_clause} order by created_at desc limit 1)" + event_type_clause = " AND info_request_events.event_type = '#{event_type}'" if event_type + "(SELECT created_at + FROM info_request_events + WHERE info_request_events.info_request_id = info_requests.id + #{event_type_clause} + ORDER BY created_at desc + LIMIT 1)" end - def InfoRequest.find_old_unclassified(extra_params={}) + def InfoRequest.old_unclassified_params(extra_params, include_last_response_time=false) last_response_created_at = last_event_time_clause('response') age = extra_params[:age_in_days] ? extra_params[:age_in_days].days : OLD_AGE_IN_DAYS - params = {:select => "*, #{last_response_created_at} as last_response_time", - :conditions => ["awaiting_description = ? and #{last_response_created_at} < ? and url_title != 'holding_pen' and user_id is not null", - true, Time.now() - age], - :order => "last_response_time"} + params = { :conditions => ["awaiting_description = ? + AND #{last_response_created_at} < ? + AND url_title != 'holding_pen' + AND user_id IS NOT NULL", + true, Time.now() - age] } + if include_last_response_time + params[:select] = "*, #{last_response_created_at} AS last_response_time" + params[:order] = 'last_response_time' + end + return params + end + + def InfoRequest.count_old_unclassified(extra_params={}) + params = old_unclassified_params(extra_params) + count(:all, params) + end + + def InfoRequest.get_random_old_unclassified(limit) + params = old_unclassified_params({}) + params[:limit] = limit + params[:order] = "random()" + find(:all, params) + end + + def InfoRequest.find_old_unclassified(extra_params={}) + params = old_unclassified_params(extra_params, include_last_response_time=true) params[:limit] = extra_params[:limit] if extra_params[:limit] params[:include] = extra_params[:include] if extra_params[:include] if extra_params[:order] @@ -958,7 +985,7 @@ public end if extra_params[:conditions] condition_string = extra_params[:conditions].shift - params[:conditions][0] += " and #{condition_string}" + params[:conditions][0] += " AND #{condition_string}" params[:conditions] += extra_params[:conditions] end find(:all, params) -- cgit v1.2.3 From 63f76f802285701f4d3c5d0b0e5e915067bc6063 Mon Sep 17 00:00:00 2001 From: Louise Crow Date: Wed, 12 Sep 2012 18:17:07 +0100 Subject: Add a counter cache for the number of info_requests associated with public bodies. --- app/models/info_request.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app/models/info_request.rb') diff --git a/app/models/info_request.rb b/app/models/info_request.rb index f2d8929bc..ed54da840 100644 --- a/app/models/info_request.rb +++ b/app/models/info_request.rb @@ -35,7 +35,7 @@ class InfoRequest < ActiveRecord::Base belongs_to :user validate :must_be_internal_or_external - belongs_to :public_body + belongs_to :public_body, :counter_cache => true validates_presence_of :public_body_id has_many :outgoing_messages, :order => 'created_at' -- cgit v1.2.3 From 7d43d1a379175a7c200e32d6046d8f7c5d203ed9 Mon Sep 17 00:00:00 2001 From: Louise Crow Date: Thu, 13 Sep 2012 09:59:13 +0100 Subject: Allow InfoRequest.find_old_unclassified to accept an offset param so it can be used in conjunction with pagination. --- app/models/info_request.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'app/models/info_request.rb') diff --git a/app/models/info_request.rb b/app/models/info_request.rb index ed54da840..2e16d0f58 100644 --- a/app/models/info_request.rb +++ b/app/models/info_request.rb @@ -977,8 +977,9 @@ public def InfoRequest.find_old_unclassified(extra_params={}) params = old_unclassified_params(extra_params, include_last_response_time=true) - params[:limit] = extra_params[:limit] if extra_params[:limit] - params[:include] = extra_params[:include] if extra_params[:include] + [:limit, :include, :offset].each do |extra| + params[extra] = extra_params[extra] if extra_params[extra] + end if extra_params[:order] params[:order] = extra_params[:order] params.delete(:select) -- cgit v1.2.3