Skip to content

Commit

Permalink
Add rake job to regenerate statistics
Browse files Browse the repository at this point in the history
Add rake job to regenerate all statistics.
  • Loading branch information
jayjay-w committed Jul 9, 2024
1 parent 9971f81 commit 7e14e4e
Showing 1 changed file with 50 additions and 5 deletions.
55 changes: 50 additions & 5 deletions lib/tasks/data/statistics.rake
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,27 @@ namespace :check do
begin
# Give user help if they want it
supported_stats = %w(
conversations
average_messages_per_day
unique_users
returning_users
valid_new_requests
published_native_reports
published_imported_reports
requests_answered_with_report
reports_sent_to_users
unique_users_who_received_report
median_response_time
unique_newsletters_sent
new_newsletter_subscriptions
newsletter_cancellations
current_subscribers
)

# Make sure we have at least one valid argument
requested_stats = (args.stats_to_generate || '').split(',').map(&:strip)
valid_requested_stats = requested_stats.intersection(supported_stats)
unless valid_requested_stats.length > 0
valid_requested_stats = requested_stats & supported_stats
unless valid_requested_stats.any?
raise Check::Statistics::ArgumentError.new("Argument '#{args.stats_to_generate}' is invalid. We currently support the following values passed a comma-separated list: #{supported_stats.join(',')}.")
end

Expand All @@ -132,9 +146,40 @@ namespace :check do
language = monthly_stats.language

begin
if valid_requested_stats.include?('unique_newsletters_sent')
monthly_stats.update!(unique_newsletters_sent: CheckStatistics.number_of_newsletters_sent(team_id, start_date, end_date, language))
total_successful[:unique_newsletters_sent] += 1
valid_requested_stats.each do |stat|
case stat
when 'conversations'
monthly_stats.update!(conversations: CheckStatistics.conversations(team_id, start_date, end_date, language))
when 'average_messages_per_day'
monthly_stats.update!(average_messages_per_day: CheckStatistics.average_messages_per_day(team_id, start_date, end_date, language))
when 'unique_users'
monthly_stats.update!(unique_users: CheckStatistics.unique_users(team_id, start_date, end_date, language))
when 'returning_users'
monthly_stats.update!(returning_users: CheckStatistics.returning_users(team_id, start_date, end_date, language))
when 'valid_new_requests'
monthly_stats.update!(valid_new_requests: CheckStatistics.valid_new_requests(team_id, start_date, end_date, language))
when 'published_native_reports'
monthly_stats.update!(published_native_reports: CheckStatistics.published_native_reports(team_id, start_date, end_date, language))
when 'published_imported_reports'
monthly_stats.update!(published_imported_reports: CheckStatistics.published_imported_reports(team_id, start_date, end_date, language))
when 'requests_answered_with_report'
monthly_stats.update!(requests_answered_with_report: CheckStatistics.requests_answered_with_report(team_id, start_date, end_date, language))
when 'reports_sent_to_users'
monthly_stats.update!(reports_sent_to_users: CheckStatistics.reports_sent_to_users(team_id, start_date, end_date, language))
when 'unique_users_who_received_report'
monthly_stats.update!(unique_users_who_received_report: CheckStatistics.unique_users_who_received_report(team_id, start_date, end_date, language))
when 'median_response_time'
monthly_stats.update!(median_response_time: CheckStatistics.median_response_time(team_id, start_date, end_date, language))
when 'unique_newsletters_sent'
monthly_stats.update!(unique_newsletters_sent: CheckStatistics.number_of_newsletters_sent(team_id, start_date, end_date, language))
when 'new_newsletter_subscriptions'
monthly_stats.update!(new_newsletter_subscriptions: CheckStatistics.new_newsletter_subscriptions(team_id, start_date, end_date, language))
when 'newsletter_cancellations'
monthly_stats.update!(newsletter_cancellations: CheckStatistics.newsletter_cancellations(team_id, start_date, end_date, language))
when 'current_subscribers'
monthly_stats.update!(current_subscribers: CheckStatistics.current_subscribers(team_id, start_date, end_date, language))
end
total_successful[stat.to_sym] += 1
end
rescue StandardError => e
$stderr.puts "[#{Time.now}] Failed to update MonthlyTeamStatistic with ID #{monthly_stats.id}. Error: #{e}"
Expand Down

0 comments on commit 7e14e4e

Please sign in to comment.