From 1ae902fa60672da81570e66809643022b89d130c Mon Sep 17 00:00:00 2001 From: Gabriel Grubba <70247653+Grubba27@users.noreply.github.com> Date: Mon, 1 Jul 2024 15:40:52 -0300 Subject: [PATCH] FIX: Division by zero error on WebHookEventsDailyAggregate (#27667) * FIX: Division by zero error on WebHookEventsDailyAggregate * DEV: Update implementation of WebHookEventsDailyAggregate to handle division by zero error --- app/models/web_hook_events_daily_aggregate.rb | 8 +++++++- spec/models/web_hook_events_daily_aggregate_spec.rb | 6 ++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/app/models/web_hook_events_daily_aggregate.rb b/app/models/web_hook_events_daily_aggregate.rb index 67b2f4a7026..d845ebd6449 100644 --- a/app/models/web_hook_events_daily_aggregate.rb +++ b/app/models/web_hook_events_daily_aggregate.rb @@ -25,8 +25,14 @@ class WebHookEventsDailyAggregate < ActiveRecord::Base self.web_hook_id, ) - self.mean_duration = events.sum(:duration) / events.count + if events.empty? + self.mean_duration = 0 + self.successful_event_count = 0 + self.failed_event_count = 0 + return + end + self.mean_duration = events.sum(:duration) / events.count self.successful_event_count = events.where("status >= 200 AND status <= 299").count self.failed_event_count = events.where("status < 200 OR status > 299").count end diff --git a/spec/models/web_hook_events_daily_aggregate_spec.rb b/spec/models/web_hook_events_daily_aggregate_spec.rb index 1f01f641c5b..92dc9af653f 100644 --- a/spec/models/web_hook_events_daily_aggregate_spec.rb +++ b/spec/models/web_hook_events_daily_aggregate_spec.rb @@ -110,5 +110,11 @@ RSpec.describe WebHookEventsDailyAggregate do WebHookEventsDailyAggregate.count } end + + it "should not fail if there are no events" do + expect { Jobs::AggregateWebHooksEvents.new.execute(date: 99.days.ago) }.not_to raise_error + + expect(WebHookEventsDailyAggregate.count).to eq(1) + end end end