aboutsummaryrefslogtreecommitdiffstats
path: root/lib/tasks
diff options
context:
space:
mode:
authorMark Longair <mhl@pobox.com>2013-08-19 10:34:05 +0100
committerMark Longair <mhl@pobox.com>2013-08-20 12:11:45 +0100
commite5855f7fd2657574c5af99890c63d530ca3bb5d0 (patch)
tree338b64fd532af727027ada82b7a4d2042855e305 /lib/tasks
parent32625f08b9c7e7bb65bd24099e64d7d17be5e45e (diff)
Improve calculation of PublicBody statistics columns
On PublicBody, we don't need to update info_requests_count because that's already done with :counter_cache. On the other hand, info_requests_successful_count and info_requests_not_held_count can't be updated easily with counter_cache (since they need conditions to be attached). Instead we update them in post_save and post_destroy, as suggested here: http://blog.douglasfshearer.com/post/17495285851/custom-counter-cache-with-conditions This also adds tests to ensure that the after_(save|destroy) callbacks are called and that they modify the counts correctly.
Diffstat (limited to 'lib/tasks')
-rw-r--r--lib/tasks/stats.rake21
1 files changed, 3 insertions, 18 deletions
diff --git a/lib/tasks/stats.rake b/lib/tasks/stats.rake
index f7a3b07a5..1242575fe 100644
--- a/lib/tasks/stats.rake
+++ b/lib/tasks/stats.rake
@@ -94,25 +94,10 @@ namespace :stats do
desc 'Update statistics in the public_bodies table'
task :update_public_bodies_stats => :environment do
PublicBody.all.each do |public_body|
- puts "Finding statistics for #{public_body.name}"
- [["info_requests_count=", nil],
- ["info_requests_successful_count=", ['successful', 'partially_successful']],
- ["info_requests_not_held_count=", ['not_held']]].each do |column, states|
- puts " Aggregating data for column #{column}"
- where_clause = 'public_body_id = :pb'
- parameters = {:pb => public_body.id}
- if states
- where_clause += " AND described_state in (:states)"
- parameters[:states] = states
- end
- public_body.send(column,
- InfoRequest.where(where_clause,
- parameters).count.to_s)
- end
- # Now looking for values of 'waiting_response_overdue' and
+ puts "Counting overdue requests for #{public_body.name}"
+ # Look for values of 'waiting_response_overdue' and
# 'waiting_response_very_overdue' which aren't directly in the
- # described_state column, and instead need to
- puts " Counting overdue requests"
+ # described_state column, and instead need to be calculated:
overdue_count = 0
very_overdue_count = 0
InfoRequest.find_each(:conditions => {:public_body_id => public_body.id}) do |ir|