From 48c60549daf4ba5b94bb164ae27c7d1cbf9110bf Mon Sep 17 00:00:00 2001 From: Gareth Rees Date: Thu, 24 Apr 2014 18:00:42 +0100 Subject: Add rake tasks for James Cheshire stats --- lib/tasks/stats.rake | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) (limited to 'lib/tasks') diff --git a/lib/tasks/stats.rake b/lib/tasks/stats.rake index 38eb15996..d32dfa94d 100644 --- a/lib/tasks/stats.rake +++ b/lib/tasks/stats.rake @@ -97,6 +97,61 @@ namespace :stats do end end + desc <<-DESC +Prints the total and 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', 'Total Requests'] + 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, body.info_requests_count] + stats + puts row.join(",") + end + end + + desc <<-DESC +Prints the total and 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', 'Total Requests'] + 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, body.info_requests_count] + 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' -- cgit v1.2.3 From 6f5e975c65c4429b8a8a0040b7eb3405559acfd0 Mon Sep 17 00:00:00 2001 From: Gareth Rees Date: Thu, 1 May 2014 14:17:59 +0100 Subject: Remove Total Requests from quarterly stats Was confusing what the number meant; easier to just sum numbers in the quartery columns to get totals. --- lib/tasks/stats.rake | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'lib/tasks') diff --git a/lib/tasks/stats.rake b/lib/tasks/stats.rake index d32dfa94d..f09594529 100644 --- a/lib/tasks/stats.rake +++ b/lib/tasks/stats.rake @@ -98,7 +98,7 @@ namespace :stats do end desc <<-DESC -Prints the total and per-quarter number of created FOI Requests made to each Public Body found by the query. +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 @@ -109,7 +109,7 @@ DESC quarters = DateQuarter.quarters_between(start_at, finish_at) # Headers - headers = ['Body', 'Total Requests'] + quarters.map { |date_tuple| date_tuple.join('~') } + headers = ['Body'] + quarters.map { |date_tuple| date_tuple.join('~') } puts headers.join(",") public_bodies.each do |body| @@ -119,13 +119,13 @@ DESC count ? count : 0 end - row = [body.name, body.info_requests_count] + stats + row = [body.name] + stats puts row.join(",") end end desc <<-DESC -Prints the total and per-quarter number of successful FOI Requests made to each Public Body found by the query. +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 @@ -136,7 +136,7 @@ DESC quarters = DateQuarter.quarters_between(start_at, finish_at) # Headers - headers = ['Body', 'Total Requests'] + quarters.map { |date_tuple| date_tuple.join('~') } + headers = ['Body'] + quarters.map { |date_tuple| date_tuple.join('~') } puts headers.join(",") public_bodies.each do |body| @@ -147,7 +147,7 @@ DESC count ? count : 0 end - row = [body.name, body.info_requests_count] + stats + row = [body.name] + stats puts row.join(",") end end -- cgit v1.2.3