From c6e32501f62849c9f414c4a1425e1b49a54b9cfa Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Thu, 13 Jun 2013 12:00:03 +0200 Subject: [PATCH] Added some fixes to support TimeCalculation without config and timezone. --- app/models/ticket.rb | 39 +++++++++++++++++++++++++--------- lib/time_calculation.rb | 15 +++++++++++-- test/unit/ticket_test.rb | 4 ++++ test/unit/working_time_test.rb | 14 ++++++++++++ 4 files changed, 60 insertions(+), 12 deletions(-) diff --git a/app/models/ticket.rb b/app/models/ticket.rb index 7f6beee4e..b8fb5bd17 100644 --- a/app/models/ticket.rb +++ b/app/models/ticket.rb @@ -610,9 +610,9 @@ class Ticket < ApplicationModel # first response if sla_selected.first_response_time self.first_response_escal_date = TimeCalculation.dest_time( created_at, sla_selected.first_response_time, sla_selected.data, sla_selected.timezone ) - extended_escalation = escalation_suspend( self.first_response_escal_date, 'relative', sla_selected.timezone) - self.first_response_escal_date = TimeCalculation.dest_time( self.first_response_escal_date, extended_escalation.to_i, sla_selected.timezone) - + extended_escalation = escalation_suspend( self.first_response_escal_date, 'relative', sla_selected ) + self.first_response_escal_date = TimeCalculation.dest_time( self.first_response_escal_date, extended_escalation.to_i, sla_selected.data, sla_selected.timezone ) + # set ticket escalation self.escalation_time = self._escalation_calculation_higher_time( self.escalation_time, self.first_response_escal_date, self.first_response ) end @@ -704,7 +704,7 @@ class Ticket < ApplicationModel end #type could be: # real - time without supsend state - # relative - only suspend time + # relative - only suspend time def escalation_suspend (end_time, type, sla_selected) sum_temp = 0 @@ -737,7 +737,11 @@ def escalation_suspend (end_time, type, sla_selected) # use time if ticket got from e. g. open to pending if history_item['value_from'] != 'pending' && history_item['value_to'] == 'pending' - diff = TimeCalculation.business_time_diff( last_state_change, history_item['created_at'],sla_selected.data, sla_selected.timezone) + if sla_selected + diff = TimeCalculation.business_time_diff( last_state_change, history_item['created_at'],sla_selected.data, sla_selected.timezone) + else + diff = TimeCalculation.business_time_diff( last_state_change, history_item['created_at'] ) + end puts 'Diff count !=pending -> ==pending ' + diff.to_s sum_temp = sum_temp + diff total_time = total_time + diff @@ -745,13 +749,21 @@ def escalation_suspend (end_time, type, sla_selected) # use time if ticket got from e. g. open to open elsif history_item['value_from'] != 'pending' && history_item['value_to'] != 'pending' - diff = TimeCalculation.business_time_diff( last_state_change, history_item['created_at'], sla_selected.data, sla_selected.timezone) + if sla_selected + diff = TimeCalculation.business_time_diff( last_state_change, history_item['created_at'], sla_selected.data, sla_selected.timezone) + else + diff = TimeCalculation.business_time_diff( last_state_change, history_item['created_at'] ) + end puts 'Diff count !=pending -> !=pending ' + diff.to_s sum_temp = sum_temp + diff total_time = total_time + diff last_state_is_pending = false elsif history_item['value_from'] == 'pending' && history_item['value_to'] != 'pending' - diff = TimeCalculation.business_time_diff( last_state_change, history_item['created_at'], sla_selected.data, sla_selected.timezone) + if sla_selected + diff = TimeCalculation.business_time_diff( last_state_change, history_item['created_at'], sla_selected.data, sla_selected.timezone) + else + diff = TimeCalculation.business_time_diff( last_state_change, history_item['created_at'] ) + end puts 'Diff count !=pending -> !=pending ' + diff.to_s total_time = total_time + diff last_state_is_pending = false @@ -768,14 +780,22 @@ def escalation_suspend (end_time, type, sla_selected) # if last state isnt pending, count rest if !last_state_is_pending && last_state_change && last_state_change < end_time - diff = TimeCalculation.business_time_diff( last_state_change, end_time, sla_selected.data, sla_selected.timezone) + if sla_selected + diff = TimeCalculation.business_time_diff( last_state_change, end_time, sla_selected.data, sla_selected.timezone) + else + diff = TimeCalculation.business_time_diff( last_state_change, end_time ) + end sum_temp = sum_temp + diff total_time = total_time + diff end # if we have not had any state change if !last_state_change - diff = TimeCalculation.business_time_diff( self.created_at, end_time, sla_selected.data, sla_selected.timezone) + if sla_selected + diff = TimeCalculation.business_time_diff( self.created_at, end_time, sla_selected.data, sla_selected.timezone) + else + diff = TimeCalculation.business_time_diff( self.created_at, end_time ) + end sum_temp = sum_temp + diff total_time = total_time + diff end @@ -788,7 +808,6 @@ def escalation_suspend (end_time, type, sla_selected) else return nil end - end class Number diff --git a/lib/time_calculation.rb b/lib/time_calculation.rb index c09e0c9f4..6d995735f 100644 --- a/lib/time_calculation.rb +++ b/lib/time_calculation.rb @@ -109,7 +109,7 @@ put working hours matrix and timezone in function, returns UTC working hours mat =end - def self.business_time_diff(start_time, end_time, config, timezone = '') + def self.business_time_diff(start_time, end_time, config = nil, timezone = '') if start_time.class == String start_time = Time.parse( start_time.to_s + 'UTC' ) end @@ -117,6 +117,12 @@ put working hours matrix and timezone in function, returns UTC working hours mat end_time = Time.parse( end_time.to_s + 'UTC' ) end + # if no config is given, just return calculation directly + if !config + return ((end_time - start_time) / 60 ).round + end + + working_hours = self.working_hours(start_time, config, timezone) week_day_map = { @@ -225,11 +231,16 @@ put working hours matrix and timezone in function, returns UTC working hours mat =end - def self.dest_time(start_time, diff_in_min, config, timezone = '') + def self.dest_time(start_time, diff_in_min, config = nil, timezone = '') if start_time.class == String start_time = Time.parse( start_time.to_s + ' UTC' ) end + # if no config is given, just return calculation directly + if !config + return start_time + (diff_in_min * 60) + end + # loop working_hours = self.working_hours(start_time, config, timezone) diff --git a/test/unit/ticket_test.rb b/test/unit/ticket_test.rb index 5c3f6ca53..496e36509 100644 --- a/test/unit/ticket_test.rb +++ b/test/unit/ticket_test.rb @@ -569,6 +569,10 @@ class TicketTest < ActiveSupport::TestCase delete = ticket.destroy assert( delete, "ticket destroy" ) + end + + test 'ticket escalation suspend' do + ticket = Ticket.create( :title => 'some title äöüß3', diff --git a/test/unit/working_time_test.rb b/test/unit/working_time_test.rb index a7c01b512..1fda76c88 100644 --- a/test/unit/working_time_test.rb +++ b/test/unit/working_time_test.rb @@ -238,6 +238,13 @@ class WorkingTimeTest < ActiveSupport::TestCase 'end_of_workday' => '6:00 pm', }, }, + + # test 15 + { + :start => '2013-08-29 16:01:00', + :end => '2013-08-29 16:10:59', + :diff => 10, + }, ] tests.each { |test| diff = TimeCalculation.business_time_diff( test[:start], test[:end], test[:config], test[:timezone] ) @@ -495,6 +502,13 @@ class WorkingTimeTest < ActiveSupport::TestCase }, }, + # test 17 + { + :start => '2013-10-21 04:01:00', + :dest_time => '2013-10-21 06:00:00', + :diff => 119, + }, + ] tests.each { |test| dest_time = TimeCalculation.dest_time( test[:start] + ' UTC', test[:diff], test[:config], test[:timezone] )