Skip to content

Commit

Permalink
Extract Statistics::MonthlyTransactions
Browse files Browse the repository at this point in the history
Simplifies the rake task and makes the statistics generation reusable.
  • Loading branch information
garethrees committed Feb 9, 2024
1 parent 356effb commit 721bd79
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 102 deletions.
135 changes: 135 additions & 0 deletions app/models/statistics/monthly_transactions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
class Statistics::MonthlyTransactions
include Enumerable

def initialize(start_year: nil, start_month: nil, end_year: nil, end_month: nil)
@start_year = (start_year || InfoRequest.first.created_at.year).to_i
@start_month = (start_month || 1).to_i
@end_year = (end_year || Time.zone.now.year).to_i
@end_month = (end_month || Time.zone.now.month).to_i
end

def to_a
each_month.unshift(headers)
end

def each
yield(headers)

month_starts.each do |month_start|
yield(monthly_transactions(month_start))
end
end

def headers
['Period',
'Requests sent',
'Pro requests sent',
'Visible comments',
'Track this request email signups',
'Comments on own requests',
'Follow up messages sent',
'Confirmed users',
'Confirmed pro users',
'Request classifications',
'Public body change requests',
'Widget votes',
'Total tracks']
end

protected

attr_reader :start_year
attr_reader :start_month
attr_reader :end_year
attr_reader :end_month

private

def month_starts
(Date.new(start_year, start_month)..Date.new(end_year, end_month)).
select { |d| d.day == 1 }
end

def monthly_transactions(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.where(date_conditions).count
pro_request_count = InfoRequest.pro.where('info_requests.created_at >= ?
AND info_requests.created_at < ?',
month_start, month_end+1).count
visible_comments_count = Comment.visible.where('comments.created_at >= ?
AND comments.created_at < ?',
month_start, month_end+1).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.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.
includes(:info_request).
references(:info_request).
where(comment_on_own_request_conditions).
count

followup_date_range =
['created_at >= ? AND created_at < ?', month_start, month_end + 1]

follow_up_count =
OutgoingMessage.followup.is_searchable.where(followup_date_range).count

confirmed_users_count =
User.active.
where(email_confirmed: true).
where(date_conditions).
count

pro_confirmed_users_count =
User.pro.active.
where(email_confirmed: true).
where('users.created_at >= ?
AND users.created_at < ?',
month_start, month_end+1).
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

[period,
request_count,
pro_request_count,
visible_comments_count,
email_request_track_count,
comment_on_own_request_count,
follow_up_count,
confirmed_users_count,
pro_confirmed_users_count,
request_classifications_count,
public_body_change_requests_count,
widget_votes_count,
total_tracks_count]
end
end
110 changes: 8 additions & 102 deletions lib/tasks/stats.rake
Original file line number Diff line number Diff line change
Expand Up @@ -4,110 +4,16 @@ namespace :stats do
task show: :environment do
example = 'rake stats:show START_YEAR=2009 [START_MONTH=3 END_YEAR=2012 END_MONTH=10]'
check_for_env_vars(['START_YEAR'], example)
start_year = (ENV['START_YEAR']).to_i
start_month = (ENV['START_MONTH'] || 1).to_i
end_year = (ENV['END_YEAR'] || Time.zone.now.year).to_i
end_month = (ENV['END_MONTH'] || Time.zone.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',
'Pro requests sent',
'Visible comments',
'Track this request email signups',
'Comments on own requests',
'Follow up messages sent',
'Confirmed users',
'Confirmed pro users',
'Request classifications',
'Public body change requests',
'Widget votes',
'Total tracks']
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.where(date_conditions).count
pro_request_count = InfoRequest.pro.where('info_requests.created_at >= ?
AND info_requests.created_at < ?',
month_start, month_end+1).count
visible_comments_count = Comment.visible.where('comments.created_at >= ?
AND comments.created_at < ?',
month_start, month_end+1).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.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.
includes(:info_request).
references(:info_request).
where(comment_on_own_request_conditions).
count

followup_date_range =
['created_at >= ? AND created_at < ?', month_start, month_end + 1]

follow_up_count =
OutgoingMessage.followup.is_searchable.where(followup_date_range).count

confirmed_users_count =
User.active.
where(email_confirmed: true).
where(date_conditions).
count

pro_confirmed_users_count =
User.pro.active.
where(email_confirmed: true).
where('users.created_at >= ?
AND users.created_at < ?',
month_start, month_end+1).
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

stats = Statistics::MonthlyTransactions.new(
start_year: (ENV['START_YEAR']).to_i,
start_month: (ENV['START_MONTH'] || 1).to_i,
end_year: (ENV['END_YEAR'] || Time.zone.now.year).to_i,
end_month: (ENV['END_MONTH'] || Time.zone.now.month).to_i
)

puts [period,
request_count,
pro_request_count,
visible_comments_count,
email_request_track_count,
comment_on_own_request_count,
follow_up_count,
confirmed_users_count,
pro_confirmed_users_count,
request_classifications_count,
public_body_change_requests_count,
widget_votes_count,
total_tracks_count].join("\t")
stats.each do |row|
puts row.join("\t")
end
end

Expand Down

0 comments on commit 721bd79

Please sign in to comment.