From b30ef07940ee3358fcb7703d35f8ae68843ba474 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Sat, 23 Mar 2013 17:56:18 +0100 Subject: [PATCH] Improved escalation calculation. --- app/models/ticket.rb | 32 ++++------ test/unit/ticket_test.rb | 133 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 138 insertions(+), 27 deletions(-) diff --git a/app/models/ticket.rb b/app/models/ticket.rb index f00050bee..521fd3604 100644 --- a/app/models/ticket.rb +++ b/app/models/ticket.rb @@ -580,10 +580,6 @@ class Ticket < ApplicationModel self.escalation_time = self._escalation_calculation_higher_time( self.escalation_time, self.first_response_escal_date, self.first_response ) end if self.first_response# && !self.first_response_in_min -# created_at = Time.parse(self.created_at.to_s) -# first_response_at = Time.parse(self.first_response.to_s) -# diff = created_at.business_time_until(first_response_at) / 60 -# self.first_response_in_min = diff.round self.first_response_in_min = self._escalation_calculation_business_time_diff( self.created_at, self.first_response ) end @@ -592,24 +588,27 @@ class Ticket < ApplicationModel self.first_response_diff_in_min = sla_selected.first_response_time - self.first_response_in_min end + # update time + last_update = self.last_contact_agent + if !last_update + last_update = self.created_at + end if sla_selected.update_time - last_update = self.last_contact_agent - if !last_update - last_update = self.created_at - end self.update_time_escal_date = self._escalation_calculation_dest_time( last_update, sla_selected.update_time ) # set ticket escalation self.escalation_time = self._escalation_calculation_higher_time( self.escalation_time, self.update_time_escal_date, false ) end + if self.last_contact_agent + self.update_time_in_min = self._escalation_calculation_business_time_diff( self.created_at, self.last_contact_agent ) + end + + # set sla time + if sla_selected.update_time && self.update_time_in_min + self.update_time_diff_in_min = sla_selected.update_time - self.update_time_in_min + end -# if self.last_contact_agent && !self.update_time_in_min -# created_at = Time.parse(self.created_at.to_s) -# last_contact_agent = Time.parse(self.last_contact_agent.to_s) -# diff = created_at.business_time_until(closed_at) / 60 -# self.close_time_in_min = diff.round -# end # close time if sla_selected.close_time @@ -619,12 +618,7 @@ class Ticket < ApplicationModel self.escalation_time = self._escalation_calculation_higher_time( self.escalation_time, self.close_time_escal_date, self.close_time ) end if self.close_time# && !self.close_time_in_min -# created_at = Time.parse(self.created_at.to_s) -# closed_at = Time.parse(self.close_time.to_s) -# diff = created_at.business_time_until(closed_at) / 60 -# self.close_time_in_min = diff.round self.close_time_in_min = self._escalation_calculation_business_time_diff( self.created_at, self.close_time ) - end # set sla time if sla_selected.close_time && self.close_time_in_min diff --git a/test/unit/ticket_test.rb b/test/unit/ticket_test.rb index d49a3f4a0..ff67e859e 100644 --- a/test/unit/ticket_test.rb +++ b/test/unit/ticket_test.rb @@ -81,12 +81,18 @@ class TicketTest < ActiveSupport::TestCase assert_equal( ticket.first_response, nil, 'ticket.first_response verify 2' ) assert_equal( ticket.first_response_in_min, nil, 'ticket.first_response_in_min verify 2' ) assert_equal( ticket.first_response_diff_in_min, nil, 'ticket.first_response_diff_in_min verify 2' ) - assert_equal( ticket.update_time_escal_date.gmtime.to_s, '2013-03-21 11:30:00 UTC', 'ticket.update_time_escal_date verify 2' ) - assert_equal( ticket.close_time_escal_date.gmtime.to_s, '2013-03-21 12:30:00 UTC', 'ticket.close_time_escal_date verify 2' ) + assert_equal( ticket.update_time_escal_date.gmtime.to_s, '2013-03-21 11:30:00 UTC', 'ticket.update_time_escal_date verify 2' ) + assert_equal( ticket.update_time_in_min, nil, 'ticket.update_time_in_min verify 2' ) + assert_equal( ticket.update_time_diff_in_min, nil, 'ticket.update_time_diff_in_min verify 2' ) + + assert_equal( ticket.close_time_escal_date.gmtime.to_s, '2013-03-21 12:30:00 UTC', 'ticket.close_time_escal_date verify 2' ) + assert_equal( ticket.close_time_in_min, nil, 'ticket.close_time_in_min verify 2' ) + assert_equal( ticket.close_time_diff_in_min, nil, 'ticket.close_time_diff_in_min verify 2' ) + + # set first response in time ticket.update_attributes( -# :first_response_escal_date => '2013-03-26 09:30:00 UTC', - :first_response => '2013-03-21 10:00:00 UTC', + :first_response => '2013-03-21 10:00:00 UTC', ) ticket.escalation_calculation puts ticket.inspect @@ -96,12 +102,18 @@ class TicketTest < ActiveSupport::TestCase assert_equal( ticket.first_response.gmtime.to_s, '2013-03-21 10:00:00 UTC', 'ticket.first_response verify 3' ) assert_equal( ticket.first_response_in_min, 30, 'ticket.first_response_in_min verify 3' ) assert_equal( ticket.first_response_diff_in_min, 30, 'ticket.first_response_diff_in_min verify 3' ) + assert_equal( ticket.update_time_escal_date.gmtime.to_s, '2013-03-21 11:30:00 UTC', 'ticket.update_time_escal_date verify 3' ) + assert_equal( ticket.update_time_in_min, nil, 'ticket.update_time_in_min verify 3' ) + assert_equal( ticket.update_time_diff_in_min, nil, 'ticket.update_time_diff_in_min verify 3' ) + assert_equal( ticket.close_time_escal_date.gmtime.to_s, '2013-03-21 12:30:00 UTC', 'ticket.close_time_escal_date verify 3' ) + assert_equal( ticket.close_time_in_min, nil, 'ticket.close_time_in_min verify 3' ) + assert_equal( ticket.close_time_diff_in_min, nil, 'ticket.close_time_diff_in_min verify 3' ) - ticket.update_attributes( -# :first_response_escal_date => '2013-03-26 09:30:00 UTC', - :first_response => '2013-03-21 14:00:00 UTC', + # set first reponse over time + ticket.update_attributes( + :first_response => '2013-03-21 14:00:00 UTC', ) ticket.escalation_calculation puts ticket.inspect @@ -111,9 +123,114 @@ class TicketTest < ActiveSupport::TestCase assert_equal( ticket.first_response.gmtime.to_s, '2013-03-21 14:00:00 UTC', 'ticket.first_response verify 4' ) assert_equal( ticket.first_response_in_min, 270, 'ticket.first_response_in_min verify 4' ) assert_equal( ticket.first_response_diff_in_min, -210, 'ticket.first_response_diff_in_min verify 4' ) + assert_equal( ticket.update_time_escal_date.gmtime.to_s, '2013-03-21 11:30:00 UTC', 'ticket.update_time_escal_date verify 4' ) + assert_equal( ticket.update_time_in_min, nil, 'ticket.update_time_in_min verify 4' ) + assert_equal( ticket.update_time_diff_in_min, nil, 'ticket.update_time_diff_in_min verify 4' ) + assert_equal( ticket.close_time_escal_date.gmtime.to_s, '2013-03-21 12:30:00 UTC', 'ticket.close_time_escal_date verify 4' ) - + assert_equal( ticket.close_time_in_min, nil, 'ticket.close_time_in_min verify 4' ) + assert_equal( ticket.close_time_diff_in_min, nil, 'ticket.close_time_diff_in_min verify 4' ) + + # set update time in time + ticket.update_attributes( + :last_contact_agent => '2013-03-21 11:00:00 UTC', + ) + ticket.escalation_calculation + assert_equal( ticket.escalation_time.gmtime.to_s, '2013-03-21 12:30:00 UTC', 'ticket.escalation_time verify 5' ) + + assert_equal( ticket.first_response_escal_date.gmtime.to_s, '2013-03-21 10:30:00 UTC', 'ticket.first_response_escal_date verify 5' ) + assert_equal( ticket.first_response.gmtime.to_s, '2013-03-21 14:00:00 UTC', 'ticket.first_response verify 5' ) + assert_equal( ticket.first_response_in_min, 270, 'ticket.first_response_in_min verify 5' ) + assert_equal( ticket.first_response_diff_in_min, -210, 'ticket.first_response_diff_in_min verify 5' ) + + assert_equal( ticket.update_time_escal_date.gmtime.to_s, '2013-03-21 13:00:00 UTC', 'ticket.update_time_escal_date verify 5' ) + assert_equal( ticket.update_time_in_min, 90, 'ticket.update_time_in_min verify 5' ) + assert_equal( ticket.update_time_diff_in_min, 30, 'ticket.update_time_diff_in_min verify 5' ) + + assert_equal( ticket.close_time_escal_date.gmtime.to_s, '2013-03-21 12:30:00 UTC', 'ticket.close_time_escal_date verify 5' ) + assert_equal( ticket.close_time_in_min, nil, 'ticket.close_time_in_min verify 5' ) + assert_equal( ticket.close_time_diff_in_min, nil, 'ticket.close_time_diff_in_min verify 5' ) + + # set update time over time + ticket.update_attributes( + :last_contact_agent => '2013-03-21 12:00:00 UTC', + ) + ticket.escalation_calculation + assert_equal( ticket.escalation_time.gmtime.to_s, '2013-03-21 12:30:00 UTC', 'ticket.escalation_time verify 6' ) + + assert_equal( ticket.first_response_escal_date.gmtime.to_s, '2013-03-21 10:30:00 UTC', 'ticket.first_response_escal_date verify 6' ) + assert_equal( ticket.first_response.gmtime.to_s, '2013-03-21 14:00:00 UTC', 'ticket.first_response verify 6' ) + assert_equal( ticket.first_response_in_min, 270, 'ticket.first_response_in_min verify 6' ) + assert_equal( ticket.first_response_diff_in_min, -210, 'ticket.first_response_diff_in_min verify 6' ) + + assert_equal( ticket.update_time_escal_date.gmtime.to_s, '2013-03-21 14:00:00 UTC', 'ticket.update_time_escal_date verify 6' ) + assert_equal( ticket.update_time_in_min, 150, 'ticket.update_time_in_min verify 6' ) + assert_equal( ticket.update_time_diff_in_min, -30, 'ticket.update_time_diff_in_min verify 6' ) + + assert_equal( ticket.close_time_escal_date.gmtime.to_s, '2013-03-21 12:30:00 UTC', 'ticket.close_time_escal_date verify 6' ) + assert_equal( ticket.close_time_in_min, nil, 'ticket.close_time_in_min verify 6' ) + assert_equal( ticket.close_time_diff_in_min, nil, 'ticket.close_time_diff_in_min verify 6' ) + + # set close time in time + ticket.update_attributes( + :close_time => '2013-03-21 11:30:00 UTC', + ) + ticket.escalation_calculation + assert_equal( ticket.escalation_time.gmtime.to_s, '2013-03-21 14:00:00 UTC', 'ticket.escalation_time verify 7' ) + + assert_equal( ticket.first_response_escal_date.gmtime.to_s, '2013-03-21 10:30:00 UTC', 'ticket.first_response_escal_date verify 7' ) + assert_equal( ticket.first_response.gmtime.to_s, '2013-03-21 14:00:00 UTC', 'ticket.first_response verify 7' ) + assert_equal( ticket.first_response_in_min, 270, 'ticket.first_response_in_min verify 7' ) + assert_equal( ticket.first_response_diff_in_min, -210, 'ticket.first_response_diff_in_min verify 7' ) + + assert_equal( ticket.update_time_escal_date.gmtime.to_s, '2013-03-21 14:00:00 UTC', 'ticket.update_time_escal_date verify 7' ) + assert_equal( ticket.update_time_in_min, 150, 'ticket.update_time_in_min verify 7' ) + assert_equal( ticket.update_time_diff_in_min, -30, 'ticket.update_time_diff_in_min verify 7' ) + + assert_equal( ticket.close_time_escal_date.gmtime.to_s, '2013-03-21 12:30:00 UTC', 'ticket.close_time_escal_date verify 7' ) + assert_equal( ticket.close_time_in_min, 120, 'ticket.close_time_in_min verify 7' ) + assert_equal( ticket.close_time_diff_in_min, 60, 'ticket.close_time_diff_in_min verify 7' ) + + # set close time over time + ticket.update_attributes( + :close_time => '2013-03-21 13:00:00 UTC', + ) + ticket.escalation_calculation + assert_equal( ticket.escalation_time.gmtime.to_s, '2013-03-21 14:00:00 UTC', 'ticket.escalation_time verify 8' ) + + assert_equal( ticket.first_response_escal_date.gmtime.to_s, '2013-03-21 10:30:00 UTC', 'ticket.first_response_escal_date verify 8' ) + assert_equal( ticket.first_response.gmtime.to_s, '2013-03-21 14:00:00 UTC', 'ticket.first_response verify 8' ) + assert_equal( ticket.first_response_in_min, 270, 'ticket.first_response_in_min verify 8' ) + assert_equal( ticket.first_response_diff_in_min, -210, 'ticket.first_response_diff_in_min verify 8' ) + + assert_equal( ticket.update_time_escal_date.gmtime.to_s, '2013-03-21 14:00:00 UTC', 'ticket.update_time_escal_date verify 8' ) + assert_equal( ticket.update_time_in_min, 150, 'ticket.update_time_in_min verify 8' ) + assert_equal( ticket.update_time_diff_in_min, -30, 'ticket.update_time_diff_in_min verify 8' ) + + assert_equal( ticket.close_time_escal_date.gmtime.to_s, '2013-03-21 12:30:00 UTC', 'ticket.close_time_escal_date verify 8' ) + assert_equal( ticket.close_time_in_min, 210, 'ticket.close_time_in_min verify 8' ) + assert_equal( ticket.close_time_diff_in_min, -30, 'ticket.close_time_diff_in_min verify 8' ) + + # set close time over time + ticket.update_attributes( + :ticket_state => Ticket::State.lookup( :name => 'closed' ) + ) + ticket.escalation_calculation + assert_equal( ticket.escalation_time, nil, 'ticket.escalation_time verify 9' ) + + assert_equal( ticket.first_response_escal_date.gmtime.to_s, '2013-03-21 10:30:00 UTC', 'ticket.first_response_escal_date verify 9' ) + assert_equal( ticket.first_response.gmtime.to_s, '2013-03-21 14:00:00 UTC', 'ticket.first_response verify 9' ) + assert_equal( ticket.first_response_in_min, 270, 'ticket.first_response_in_min verify 9' ) + assert_equal( ticket.first_response_diff_in_min, -210, 'ticket.first_response_diff_in_min verify 9' ) + + assert_equal( ticket.update_time_escal_date.gmtime.to_s, '2013-03-21 14:00:00 UTC', 'ticket.update_time_escal_date verify 9' ) + assert_equal( ticket.update_time_in_min, 150, 'ticket.update_time_in_min verify 9' ) + assert_equal( ticket.update_time_diff_in_min, -30, 'ticket.update_time_diff_in_min verify 9' ) + + assert_equal( ticket.close_time_escal_date.gmtime.to_s, '2013-03-21 12:30:00 UTC', 'ticket.close_time_escal_date verify 9' ) + assert_equal( ticket.close_time_in_min, 210, 'ticket.close_time_in_min verify 9' ) + assert_equal( ticket.close_time_diff_in_min, -30, 'ticket.close_time_diff_in_min verify 9' ) delete = sla.destroy