diff options
author | Gareth Rees <gareth@mysociety.org> | 2014-05-01 15:10:38 +0100 |
---|---|---|
committer | Gareth Rees <gareth@mysociety.org> | 2014-05-01 15:10:38 +0100 |
commit | 4d04209d29b76c997e5238d8546d7eb17ee2bd53 (patch) | |
tree | cb7862fadee1ab6849f20f90a14420c5e8af1159 /lib | |
parent | 06c40e4d8fb546b315a4af3c82d5f891d4101c5f (diff) | |
parent | 6f5e975c65c4429b8a8a0040b7eb3405559acfd0 (diff) |
Merge branch 'issues/1390-james-cheshire-infographic' into rails-3-develop
Diffstat (limited to 'lib')
-rw-r--r-- | lib/date_quarter.rb | 22 | ||||
-rw-r--r-- | lib/tasks/stats.rake | 55 |
2 files changed, 77 insertions, 0 deletions
diff --git a/lib/date_quarter.rb b/lib/date_quarter.rb new file mode 100644 index 000000000..ac159b420 --- /dev/null +++ b/lib/date_quarter.rb @@ -0,0 +1,22 @@ +module DateQuarter + extend self + + def quarters_between(start_at, finish_at) + results = [] + + quarter_start = start_at.beginning_of_quarter + quarter_end = start_at.end_of_quarter + + while quarter_end <= finish_at.end_of_quarter do + # Collect these + results << [quarter_start, quarter_end] + + # Update dates + quarter_start = quarter_end + 1.second + quarter_end = quarter_start.end_of_quarter + end + + results + end + +end diff --git a/lib/tasks/stats.rake b/lib/tasks/stats.rake index 38eb15996..f09594529 100644 --- a/lib/tasks/stats.rake +++ b/lib/tasks/stats.rake @@ -97,6 +97,61 @@ namespace :stats do end end + desc <<-DESC +Prints the per-quarter number of created FOI Requests made to each Public Body found by the query. +Specify the search query as QUERY='london school' +DESC + task :number_of_requests_created => :environment do + query = ENV['QUERY'] + start_at = PublicBody.minimum(:created_at) + finish_at = PublicBody.maximum(:created_at) + public_bodies = PublicBody.search(query) + quarters = DateQuarter.quarters_between(start_at, finish_at) + + # Headers + headers = ['Body'] + quarters.map { |date_tuple| date_tuple.join('~') } + puts headers.join(",") + + public_bodies.each do |body| + stats = quarters.map do |quarter| + conditions = ['created_at >= ? AND created_at < ?', quarter[0], quarter[1]] + count = body.info_requests.count(:conditions => conditions) + count ? count : 0 + end + + row = [body.name] + stats + puts row.join(",") + end + end + + desc <<-DESC +Prints the per-quarter number of successful FOI Requests made to each Public Body found by the query. +Specify the search query as QUERY='london school' +DESC + task :number_of_requests_successful => :environment do + query = ENV['QUERY'] + start_at = PublicBody.minimum(:created_at) + finish_at = PublicBody.maximum(:created_at) + public_bodies = PublicBody.search(query) + quarters = DateQuarter.quarters_between(start_at, finish_at) + + # Headers + headers = ['Body'] + quarters.map { |date_tuple| date_tuple.join('~') } + puts headers.join(",") + + public_bodies.each do |body| + stats = quarters.map do |quarter| + conditions = ['created_at >= ? AND created_at < ? AND described_state = ?', + quarter[0], quarter[1], 'successful'] + count = body.info_requests.count(:conditions => conditions) + count ? count : 0 + end + + row = [body.name] + stats + puts row.join(",") + end + end + desc 'Update statistics in the public_bodies table' task :update_public_bodies_stats => :environment do verbose = ENV['VERBOSE'] == '1' |