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
This commit is contained in:
Gabriel Grubba 2024-07-01 15:40:52 -03:00 committed by GitHub
parent 88d259b73a
commit 1ae902fa60
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 additions and 1 deletions

View File

@ -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

View File

@ -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