Просмотр исходного кода

Fixed issue #645 - Dashboard widget "Waiting time today" is not counting.

Rolf Schmidt 8 лет назад
Родитель
Сommit
de7fef8c18
2 измененных файлов с 17 добавлено и 6 удалено
  1. 11 5
      lib/stats/ticket_waiting_time.rb
  2. 6 1
      test/unit/stats_ticket_waiting_time_test.rb

+ 11 - 5
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

+ 6 - 1
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