aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorGareth Rees <gareth@mysociety.org>2014-05-01 11:01:13 +0100
committerGareth Rees <gareth@mysociety.org>2014-05-01 11:01:13 +0100
commitf748f59473028c79453a64a4ccd26afa5bbbb8a8 (patch)
tree700e4e368922de10cf1c0d1251ac3bc5839a1aaf /lib
parent965843cdf3322e3cb5986e692bd8bc3f25c84bae (diff)
parent0b9dcaa0a0473be1fa9e5b3e6d9a8a56c0489688 (diff)
Merge branch 'issues/1390-james-cheshire-infographic-wdtk' into wdtk
Diffstat (limited to 'lib')
-rw-r--r--lib/date_quarter.rb22
-rw-r--r--lib/tasks/stats.rake55
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..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'