Skip to content

Commit

Permalink
Filter out empty platforms when calculating statistics (#1923)
Browse files Browse the repository at this point in the history
We don't need to save statistics when the value is 0, since this is
resulting in unnecessary platforms showing up in the data dashboard.
  • Loading branch information
jayjay-w authored Jun 20, 2024
1 parent 31424c0 commit 1855ccb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
17 changes: 11 additions & 6 deletions lib/tasks/data/statistics.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions test/lib/tasks/statistics_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 1855ccb

Please sign in to comment.