Skip to content

A platform for community discussion. Free, open, simple.

FIX: Division by zero error on WebHookEventsDailyAggregate (#27667)

Gabriel Grubba1ae902fa6067
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