diff --git a/lib/stats/ticket_waiting_time.rb b/lib/stats/ticket_waiting_time.rb index 7a7bda2a4..106815c2c 100644 --- a/lib/stats/ticket_waiting_time.rb +++ b/lib/stats/ticket_waiting_time.rb @@ -54,20 +54,26 @@ class Stats::TicketWaitingTime end def self.calculate_average(tickets, start_time) - average_time = 0 - count_time = 0 + average_time = 0 + count_articles = 0 tickets.each { |ticket| + count_time = 0 ticket.articles.joins(:type).where('ticket_articles.created_at > ? AND ticket_articles.internal = ? AND ticket_article_types.communication = ?', start_time, false, true).each { |article| if article.sender.name == 'Customer' count_time = article.created_at.to_i - else - average_time += article.created_at.to_i - count_time - count_time = 0 + elsif count_time.positive? + average_time += article.created_at.to_i - count_time + count_articles += 1 + count_time = 0 end } } + if count_articles.positive? + average_time = average_time / count_articles + end + average_time end end diff --git a/test/unit/stats_ticket_waiting_time_test.rb b/test/unit/stats_ticket_waiting_time_test.rb index d2d72d784..bd4229d0c 100644 --- a/test/unit/stats_ticket_waiting_time_test.rb +++ b/test/unit/stats_ticket_waiting_time_test.rb @@ -144,7 +144,12 @@ class StatsTicketWaitingTimeTest < ActiveSupport::TestCase ) average_time = Stats::TicketWaitingTime.calculate_average([ticket1, ticket1], '2017-04-13 00:00:00') - assert_equal(60 * 60 * 6 * 2, average_time) + + expected_average_time = 60 * 60 * 2 # for communication 2 + expected_average_time += 60 * 60 * 4 # for communication 3 + expected_average_time = expected_average_time / 2 # for average + + assert_equal(expected_average_time, average_time) end end