From 84c2578e1cfae9d517721ada03625245957778b4 Mon Sep 17 00:00:00 2001 From: Gareth Rees Date: Mon, 3 Aug 2015 17:10:00 +0100 Subject: [PATCH 1/6] Add missing associations --- app/models/info_request_event.rb | 2 ++ app/models/request_classification.rb | 1 + 2 files changed, 3 insertions(+) diff --git a/app/models/info_request_event.rb b/app/models/info_request_event.rb index 4189c27dde..43bf86c012 100644 --- a/app/models/info_request_event.rb +++ b/app/models/info_request_event.rb @@ -32,6 +32,8 @@ class InfoRequestEvent < ActiveRecord::Base belongs_to :incoming_message belongs_to :comment + has_one :request_classification + has_many :user_info_request_sent_alerts has_many :track_things_sent_emails diff --git a/app/models/request_classification.rb b/app/models/request_classification.rb index c6398e42b7..a70b89cac4 100644 --- a/app/models/request_classification.rb +++ b/app/models/request_classification.rb @@ -12,6 +12,7 @@ class RequestClassification < ActiveRecord::Base belongs_to :user + belongs_to :info_request_event # return classification instances representing the top n # users, with a 'cnt' attribute representing the number From 80cd379283cfd1a268941606f3c7db9a71b53ef5 Mon Sep 17 00:00:00 2001 From: Gareth Rees Date: Mon, 3 Aug 2015 17:10:10 +0100 Subject: [PATCH 2/6] Add RequestClassification factory --- spec/factories/request_classifications.rb | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 spec/factories/request_classifications.rb diff --git a/spec/factories/request_classifications.rb b/spec/factories/request_classifications.rb new file mode 100644 index 0000000000..3532f1543f --- /dev/null +++ b/spec/factories/request_classifications.rb @@ -0,0 +1,9 @@ +# -*- encoding : utf-8 -*- +FactoryGirl.define do + + factory :request_classification do + user + info_request_event + end + +end From 87fd4511aad02c562768d6a34eeddb4cbaa77f90 Mon Sep 17 00:00:00 2001 From: Gareth Rees Date: Mon, 3 Aug 2015 17:13:18 +0100 Subject: [PATCH 3/6] Add more stats to GeneralController#version - Covers all countable transactions - Adds spec for the action --- app/controllers/general_controller.rb | 22 ++++-- doc/CHANGES.md | 4 ++ spec/controllers/general_controller_spec.rb | 74 +++++++++++++++++++++ 3 files changed, 94 insertions(+), 6 deletions(-) diff --git a/app/controllers/general_controller.rb b/app/controllers/general_controller.rb index a74e5047b9..51680fabe1 100644 --- a/app/controllers/general_controller.rb +++ b/app/controllers/general_controller.rb @@ -190,12 +190,22 @@ def not_found def version respond_to do |format| - format.json { render :json => { - :alaveteli_git_commit => alaveteli_git_commit, - :alaveteli_version => ALAVETELI_VERSION, - :ruby_version => RUBY_VERSION, - :visible_request_count => InfoRequest.visible.count, - :confirmed_user_count => User.where(:email_confirmed => true).count + format.json { + render :json => { + :alaveteli_git_commit => alaveteli_git_commit, + :alaveteli_version => ALAVETELI_VERSION, + :ruby_version => RUBY_VERSION, + :visible_public_body_count => PublicBody.visible.count, + :visible_request_count => InfoRequest.visible.count, + :confirmed_user_count => User.where(:email_confirmed => true).count, + :visible_comment_count => Comment.visible.count, + :track_thing_count => TrackThing.count, + :widget_vote_count => WidgetVote.count, + :public_body_change_request_count => PublicBodyChangeRequest.count, + :request_classification_count => RequestClassification.count, + :visible_followup_message_count => + OutgoingMessage.where(:prominence => 'normal', + :message_type => 'followup').count }} end end diff --git a/doc/CHANGES.md b/doc/CHANGES.md index 9fb17e4e3f..c361557311 100644 --- a/doc/CHANGES.md +++ b/doc/CHANGES.md @@ -1,5 +1,9 @@ # develop +## Highlighted Features + +* Added additional transaction stats to /version.json endpoint (Gareth Rees). + # Version 0.22.2.0 ## Highlighted Features diff --git a/spec/controllers/general_controller_spec.rb b/spec/controllers/general_controller_spec.rb index 61572e89f0..f203aeea8a 100644 --- a/spec/controllers/general_controller_spec.rb +++ b/spec/controllers/general_controller_spec.rb @@ -2,6 +2,80 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') require 'fakeweb' +describe GeneralController do + + describe 'GET version' do + + it 'renders json stats about the install' do + # Clean up fixtures + InfoRequest.find_each(&:fully_destroy) + Comment.find_each(&:fully_destroy) + PublicBody.find_each(&:destroy) + TrackThing.find_each(&:destroy) + User.find_each(&:destroy) + + # Create some constant God models for other factories + user = FactoryGirl.create(:user) + body = FactoryGirl.create(:public_body) + info_request = FactoryGirl.create(:info_request, + :user => user, :public_body => body) + default_args = { :info_request => info_request, + :public_body => body, + :user => user } + + # Create the other data we're checking + FactoryGirl.create(:info_request, :user => user, + :public_body => body, + :prominence => 'hidden') + FactoryGirl.create(:user, :email_confirmed => false) + FactoryGirl.create(:visible_comment, + default_args.dup.slice!(:public_body)) + FactoryGirl.create(:hidden_comment, + default_args.dup.slice!(:public_body)) + FactoryGirl.create(:search_track, :tracking_user => user) + FactoryGirl.create(:widget_vote, + default_args.dup.slice!(:user, :public_body)) + FactoryGirl.create(:internal_review_request, + default_args.dup.slice!(:user, :public_body)) + FactoryGirl.create(:internal_review_request, + :info_request => info_request, :prominence => 'hidden') + FactoryGirl.create(:add_body_request, + default_args.dup.slice!(:info_request)) + event = FactoryGirl.create(:info_request_event, + default_args.dup.slice!(:user, :public_body)) + FactoryGirl.create(:request_classification, :user => user, + :info_request_event => event) + + mock_git_commit = Digest::SHA1.hexdigest(Time.now.to_s) + + ApplicationController. + any_instance. + stub(:alaveteli_git_commit). + and_return(mock_git_commit) + + expected = { :alaveteli_git_commit => mock_git_commit, + :alaveteli_version => ALAVETELI_VERSION, + :ruby_version => RUBY_VERSION, + :visible_public_body_count => 1, + :visible_request_count => 1, + :confirmed_user_count => 1, + :visible_comment_count => 1, + :track_thing_count => 1, + :widget_vote_count => 1, + :public_body_change_request_count => 1, + :request_classification_count => 1, + :visible_followup_message_count => 1 } + + get :version, :format => :json + + parsed_body = JSON.parse(response.body).symbolize_keys + expect(parsed_body).to eq(expected) + end + + end + +end + describe GeneralController, "when trying to show the blog" do before (:each) do FakeWeb.clean_registry From 9d7f68c9936db91d8975d020df1fe728dbe89ba5 Mon Sep 17 00:00:00 2001 From: Gareth Rees Date: Tue, 4 Aug 2015 09:47:34 +0100 Subject: [PATCH 4/6] Tidy up --- lib/tasks/stats.rake | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/lib/tasks/stats.rake b/lib/tasks/stats.rake index 3627d98527..1aae9286ed 100644 --- a/lib/tasks/stats.rake +++ b/lib/tasks/stats.rake @@ -8,7 +8,9 @@ namespace :stats do start_month = (ENV['START_MONTH'] || 1).to_i end_year = (ENV['END_YEAR'] || Time.now.year).to_i end_month = (ENV['END_MONTH'] || Time.now.month).to_i + month_starts = (Date.new(start_year, start_month)..Date.new(end_year, end_month)).select { |d| d.day == 1 } + headers = ['Period', 'Requests sent', 'Annotations added', @@ -16,32 +18,46 @@ namespace :stats do 'Comments on own requests', 'Follow up messages sent'] puts headers.join("\t") + month_starts.each do |month_start| month_end = month_start.end_of_month period = "#{month_start}-#{month_end}" + date_conditions = ['created_at >= ? AND created_at < ?', month_start, month_end+1] - request_count = InfoRequest.count(:conditions => date_conditions) - comment_count = Comment.count(:conditions => date_conditions) + + request_count = InfoRequest.where(date_conditions).count + comment_count = Comment.where(date_conditions).count + track_conditions = ['track_type = ? AND track_medium = ? AND created_at >= ? AND created_at < ?', - 'request_updates', 'email_daily', month_start, month_end+1] - email_request_track_count = TrackThing.count(:conditions => track_conditions) + 'request_updates', + 'email_daily', + month_start, + month_end + 1] + email_request_track_count = TrackThing.where(track_conditions).count + comment_on_own_request_conditions = ['comments.user_id = info_requests.user_id AND comments.created_at >= ? AND comments.created_at < ?', month_start, month_end+1] - comment_on_own_request_count = Comment.count(:conditions => comment_on_own_request_conditions, - :include => :info_request) + + comment_on_own_request_count = + Comment. + includes(:info_request). + where(comment_on_own_request_conditions). + count followup_conditions = ['message_type = ? AND created_at >= ? AND created_at < ?', - 'followup', month_start, month_end+1] - follow_up_count = OutgoingMessage.count(:conditions => followup_conditions) + 'followup', + month_start, + month_end + 1] + follow_up_count = OutgoingMessage.where(followup_conditions).count puts [period, request_count, comment_count, From 7259b92140e9ef773d9550bb203fa3da0088140a Mon Sep 17 00:00:00 2001 From: Gareth Rees Date: Tue, 4 Aug 2015 10:36:06 +0100 Subject: [PATCH 5/6] Add more transactions to stats.rake --- doc/CHANGES.md | 1 + lib/tasks/stats.rake | 40 +++++++++++++++++++++++++++++++++++----- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/doc/CHANGES.md b/doc/CHANGES.md index c361557311..60936b4a71 100644 --- a/doc/CHANGES.md +++ b/doc/CHANGES.md @@ -3,6 +3,7 @@ ## Highlighted Features * Added additional transaction stats to /version.json endpoint (Gareth Rees). +* Added additional transaction stats to stats:show rake task (Gareth Rees). # Version 0.22.2.0 diff --git a/lib/tasks/stats.rake b/lib/tasks/stats.rake index 1aae9286ed..943ed0cb4e 100644 --- a/lib/tasks/stats.rake +++ b/lib/tasks/stats.rake @@ -13,10 +13,15 @@ namespace :stats do headers = ['Period', 'Requests sent', - 'Annotations added', + 'Visible comments', 'Track this request email signups', 'Comments on own requests', - 'Follow up messages sent'] + 'Follow up messages sent', + 'Confirmed users', + 'Request classifications', + 'Public body change requests', + 'Widget votes', + 'Total tracks'] puts headers.join("\t") month_starts.each do |month_start| @@ -28,7 +33,7 @@ namespace :stats do month_start, month_end+1] request_count = InfoRequest.where(date_conditions).count - comment_count = Comment.where(date_conditions).count + visible_comments_count = Comment.visible.where(date_conditions).count track_conditions = ['track_type = ? AND track_medium = ? @@ -52,18 +57,43 @@ namespace :stats do count followup_conditions = ['message_type = ? + AND prominence = ? AND created_at >= ? AND created_at < ?', 'followup', + 'normal', month_start, month_end + 1] follow_up_count = OutgoingMessage.where(followup_conditions).count + + confirmed_users_count = + User. + where(:email_confirmed => true). + where(date_conditions). + count + + request_classifications_count = + RequestClassification.where(date_conditions).count + + public_body_change_requests_count = + PublicBodyChangeRequest.where(date_conditions).count + + widget_votes_count = WidgetVote.where(date_conditions).count + + total_tracks_count = TrackThing.where(date_conditions).count + + puts [period, request_count, - comment_count, + visible_comments_count, email_request_track_count, comment_on_own_request_count, - follow_up_count].join("\t") + follow_up_count, + confirmed_users_count, + request_classifications_count, + public_body_change_requests_count, + widget_votes_count, + total_tracks_count].join("\t") end end From 322a3689a936e92df63ab37fc87b1891cbed2bc1 Mon Sep 17 00:00:00 2001 From: Gareth Rees Date: Thu, 13 Aug 2015 16:39:36 +0100 Subject: [PATCH 6/6] Bump alaveteli version --- config/initializers/alaveteli.rb | 2 +- doc/CHANGES.md | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/config/initializers/alaveteli.rb b/config/initializers/alaveteli.rb index 68d8348b95..352302eaec 100644 --- a/config/initializers/alaveteli.rb +++ b/config/initializers/alaveteli.rb @@ -11,7 +11,7 @@ load "util.rb" # Application version -ALAVETELI_VERSION = '0.22.2.0' +ALAVETELI_VERSION = '0.22.3.0' # Add new inflection rules using the following format # (all these examples are active by default): diff --git a/doc/CHANGES.md b/doc/CHANGES.md index 60936b4a71..29a36dbbb4 100644 --- a/doc/CHANGES.md +++ b/doc/CHANGES.md @@ -2,9 +2,15 @@ ## Highlighted Features +# Version 0.22.3.0 + * Added additional transaction stats to /version.json endpoint (Gareth Rees). * Added additional transaction stats to stats:show rake task (Gareth Rees). +## Upgrade Notes + +* There should be no action necessary. + # Version 0.22.2.0 ## Highlighted Features