diff --git a/lib/tasks/data/statistics.rake b/lib/tasks/data/statistics.rake index 13ff2a0ada..d01967c16b 100644 --- a/lib/tasks/data/statistics.rake +++ b/lib/tasks/data/statistics.rake @@ -66,13 +66,18 @@ namespace :check do end end - partial_month = MonthlyTeamStatistic.find_by(team_id: team_id, platform: platform, language: language, start_date: month_start) - if partial_month.present? - partial_month.update!(row_attributes.merge!(team_id: team_id)) - team_stats[:updated] += 1 + # Check if any statistics are non-zero + if row_attributes.values.any? { |value| value != 0 } + partial_month = MonthlyTeamStatistic.find_by(team_id: team_id, platform: platform, language: language, start_date: month_start) + if partial_month.present? + partial_month.update!(row_attributes.merge!(team_id: team_id)) + team_stats[:updated] += 1 + else + MonthlyTeamStatistic.create!(row_attributes.merge!(team_id: team_id)) + team_stats[:created] += 1 + end else - MonthlyTeamStatistic.create!(row_attributes.merge!(team_id: team_id)) - team_stats[:created] += 1 + team_stats[:skipped_zero] += 1 end rescue StandardError => e diff --git a/test/lib/tasks/statistics_test.rb b/test/lib/tasks/statistics_test.rb index 10e09643d2..ddb8a1137b 100644 --- a/test/lib/tasks/statistics_test.rb +++ b/test/lib/tasks/statistics_test.rb @@ -236,6 +236,30 @@ def teardown assert_equal 0, MonthlyTeamStatistic.where(team: @tipline_team).count end + test "check:data:statistics skips generating statistics with zero values" do + # Setup + team = @tipline_team + bot_user = BotUser.smooch_user + + CheckStatistics.stubs(:get_statistics).returns( + conversations_24hr: 0, + another_stat: 0 + ) + + assert_equal 0, MonthlyTeamStatistic.where(team: team).count + + travel_to @current_date + + out, err = capture_io do + Rake::Task['check:data:statistics'].invoke + end + + Rake::Task['check:data:statistics'].reenable + + assert err.blank?, "Expected no errors, but got: #{err}" + assert_equal 0, MonthlyTeamStatistic.where(team: team).count, "Expected no statistics to be created" + end + test "check:data:statistics skips generating statistics for teams without a team bot installation" do TeamBotInstallation.delete_all