2016-10-19 03:11:36 +00:00
|
|
|
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
|
2015-04-27 23:19:26 +00:00
|
|
|
module Ticket::Escalation
|
2013-08-16 14:30:51 +00:00
|
|
|
|
2013-08-17 16:09:19 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
rebuild escalations for all open tickets
|
|
|
|
|
|
|
|
result = Ticket::Escalation.rebuild_all
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = true
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
def self.rebuild_all
|
2017-03-27 14:06:18 +00:00
|
|
|
state_list_open = Ticket::State.by_category(:open)
|
2013-08-16 14:30:51 +00:00
|
|
|
|
2016-09-02 10:54:13 +00:00
|
|
|
ticket_ids = Ticket.where(state_id: state_list_open).pluck(:id)
|
|
|
|
ticket_ids.each { |ticket_id|
|
|
|
|
Ticket.find(ticket_id).escalation_calculation(true)
|
|
|
|
}
|
2015-04-27 23:19:26 +00:00
|
|
|
end
|
2013-08-16 14:30:51 +00:00
|
|
|
|
2013-08-17 16:09:19 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
rebuild escalation for ticket
|
|
|
|
|
|
|
|
ticket = Ticket.find(123)
|
|
|
|
result = ticket.escalation_calculation
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
2016-09-02 10:54:13 +00:00
|
|
|
result = true # true = ticket has been updated | false = no changes on ticket
|
2013-08-17 16:09:19 +00:00
|
|
|
|
|
|
|
=end
|
|
|
|
|
2016-09-02 10:54:13 +00:00
|
|
|
def escalation_calculation(force = false)
|
|
|
|
return if !escalation_calculation_int(force)
|
|
|
|
self.callback_loop = true
|
|
|
|
save!
|
|
|
|
self.callback_loop = false
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def escalation_calculation_int(force = false)
|
|
|
|
return if callback_loop == true
|
2014-05-30 13:36:07 +00:00
|
|
|
|
2016-09-02 10:54:13 +00:00
|
|
|
# return if we run import mode
|
|
|
|
return if Setting.get('import_mode') && !Setting.get('import_ignore_sla')
|
|
|
|
|
|
|
|
# set escalation off if current state is not escalation relativ (e. g. ticket is closed)
|
|
|
|
return if !state_id
|
2016-02-23 09:19:06 +00:00
|
|
|
state = Ticket::State.lookup(id: state_id)
|
2015-09-10 19:09:50 +00:00
|
|
|
escalation_disabled = false
|
2015-04-27 23:19:26 +00:00
|
|
|
if state.ignore_escalation?
|
2015-09-10 19:09:50 +00:00
|
|
|
escalation_disabled = true
|
2016-09-02 10:54:13 +00:00
|
|
|
|
|
|
|
# early exit if nothing current state is not escalation relativ
|
|
|
|
if !force
|
2016-09-14 07:15:30 +00:00
|
|
|
return false if escalation_at.nil?
|
|
|
|
self.escalation_at = nil
|
2016-11-08 17:41:57 +00:00
|
|
|
if preferences['escalation_calculation']
|
|
|
|
preferences['escalation_calculation']['escalation_disabled'] = escalation_disabled
|
|
|
|
end
|
2016-09-02 10:54:13 +00:00
|
|
|
return true
|
|
|
|
end
|
2015-04-27 23:19:26 +00:00
|
|
|
end
|
2013-08-16 14:30:51 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# get sla for ticket
|
2015-09-10 19:09:50 +00:00
|
|
|
calendar = nil
|
|
|
|
sla = escalation_calculation_get_sla
|
|
|
|
if sla
|
|
|
|
calendar = sla.calendar
|
|
|
|
end
|
2014-05-30 13:36:07 +00:00
|
|
|
|
2015-09-10 19:09:50 +00:00
|
|
|
# if no escalation is enabled
|
2015-09-23 00:29:13 +00:00
|
|
|
if !sla || !calendar
|
2014-05-30 13:36:07 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# nothing to change
|
2016-10-28 12:04:08 +00:00
|
|
|
return false if !escalation_at && !first_response_escalation_at && !update_escalation_at && !close_escalation_at
|
2017-09-23 06:25:55 +00:00
|
|
|
preferences['escalation_calculation'] = {}
|
2016-09-14 07:15:30 +00:00
|
|
|
self.escalation_at = nil
|
2016-10-28 12:04:08 +00:00
|
|
|
self.first_response_escalation_at = nil
|
|
|
|
self.escalation_at = nil
|
|
|
|
self.update_escalation_at = nil
|
|
|
|
self.close_escalation_at = nil
|
2016-11-08 17:41:57 +00:00
|
|
|
if preferences['escalation_calculation']
|
|
|
|
preferences['escalation_calculation']['escalation_disabled'] = escalation_disabled
|
|
|
|
end
|
2015-04-27 23:19:26 +00:00
|
|
|
return true
|
|
|
|
end
|
2013-08-16 14:30:51 +00:00
|
|
|
|
2016-09-14 07:15:30 +00:00
|
|
|
# get last_update_at
|
|
|
|
if !last_contact_customer_at && !last_contact_agent_at
|
|
|
|
last_update_at = created_at
|
|
|
|
elsif !last_contact_customer_at && last_contact_agent_at
|
|
|
|
last_update_at = last_contact_agent_at
|
|
|
|
elsif last_contact_customer_at && !last_contact_agent_at
|
|
|
|
last_update_at = last_contact_customer_at
|
|
|
|
elsif last_contact_agent_at > last_contact_customer_at
|
|
|
|
last_update_at = last_contact_agent_at
|
|
|
|
elsif last_contact_agent_at < last_contact_customer_at
|
|
|
|
last_update_at = last_contact_customer_at
|
2016-09-02 10:54:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# check if calculation need be done
|
|
|
|
escalation_calculation = preferences[:escalation_calculation] || {}
|
|
|
|
sla_changed = true
|
|
|
|
if escalation_calculation['sla_id'] == sla.id && escalation_calculation['sla_updated_at'] == sla.updated_at
|
|
|
|
sla_changed = false
|
|
|
|
end
|
|
|
|
calendar_changed = true
|
|
|
|
if escalation_calculation['calendar_id'] == calendar.id && escalation_calculation['calendar_updated_at'] == calendar.updated_at
|
|
|
|
calendar_changed = false
|
|
|
|
end
|
|
|
|
if sla_changed == true || calendar_changed == true
|
|
|
|
force = true
|
|
|
|
end
|
2016-09-14 07:15:30 +00:00
|
|
|
first_response_at_changed = true
|
|
|
|
if escalation_calculation['first_response_at'] == first_response_at
|
|
|
|
first_response_at_changed = false
|
2016-09-02 10:54:13 +00:00
|
|
|
end
|
2016-09-14 07:15:30 +00:00
|
|
|
last_update_at_changed = true
|
2017-09-23 06:25:55 +00:00
|
|
|
if escalation_calculation['last_update_at'] == last_update_at && !saved_change_to_attribute('state_id')
|
2016-09-14 07:15:30 +00:00
|
|
|
last_update_at_changed = false
|
2016-09-02 10:54:13 +00:00
|
|
|
end
|
2016-09-14 07:15:30 +00:00
|
|
|
close_at_changed = true
|
|
|
|
if escalation_calculation['close_at'] == close_at
|
|
|
|
close_at_changed = false
|
2016-09-02 10:54:13 +00:00
|
|
|
end
|
2013-08-16 14:30:51 +00:00
|
|
|
|
2016-09-02 10:54:13 +00:00
|
|
|
if !force && preferences[:escalation_calculation]
|
2016-09-14 07:15:30 +00:00
|
|
|
if first_response_at_changed == false &&
|
|
|
|
last_update_at_changed == false &&
|
|
|
|
close_at_changed == false &&
|
2016-09-02 10:54:13 +00:00
|
|
|
sla_changed == false &&
|
|
|
|
calendar_changed == false &&
|
|
|
|
escalation_calculation['escalation_disabled'] == escalation_disabled
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# reset escalation attributes
|
2016-09-14 07:15:30 +00:00
|
|
|
self.escalation_at = nil
|
2016-09-02 10:54:13 +00:00
|
|
|
if force == true
|
2016-09-14 07:15:30 +00:00
|
|
|
self.first_response_escalation_at = nil
|
|
|
|
self.update_escalation_at = nil
|
|
|
|
self.close_escalation_at = nil
|
2016-09-02 10:54:13 +00:00
|
|
|
end
|
2015-09-10 19:09:50 +00:00
|
|
|
biz = Biz::Schedule.new do |config|
|
2015-09-22 23:22:45 +00:00
|
|
|
|
|
|
|
# get business hours
|
2015-09-20 23:13:06 +00:00
|
|
|
hours = {}
|
2016-06-30 20:04:48 +00:00
|
|
|
calendar.business_hours.each { |day, meta|
|
2015-09-20 23:13:06 +00:00
|
|
|
next if !meta[:active]
|
|
|
|
next if !meta[:timeframes]
|
|
|
|
hours[day.to_sym] = {}
|
2016-06-30 20:04:48 +00:00
|
|
|
meta[:timeframes].each { |frame|
|
2015-09-20 23:13:06 +00:00
|
|
|
next if !frame[0]
|
|
|
|
next if !frame[1]
|
|
|
|
hours[day.to_sym][frame[0]] = frame[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
config.hours = hours
|
2016-12-02 11:24:00 +00:00
|
|
|
if hours.blank?
|
2016-08-22 09:49:35 +00:00
|
|
|
raise "No configured hours found in calendar #{calendar.inspect}"
|
2015-10-30 09:14:34 +00:00
|
|
|
end
|
2015-09-22 23:22:45 +00:00
|
|
|
|
|
|
|
# get holidays
|
|
|
|
holidays = []
|
|
|
|
if calendar.public_holidays
|
2016-06-30 20:04:48 +00:00
|
|
|
calendar.public_holidays.each { |day, meta|
|
2015-09-22 23:22:45 +00:00
|
|
|
next if !meta
|
|
|
|
next if !meta['active']
|
|
|
|
next if meta['removed']
|
|
|
|
holidays.push Date.parse(day)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
config.holidays = holidays
|
2015-09-10 19:09:50 +00:00
|
|
|
config.time_zone = calendar.timezone
|
2015-04-27 23:19:26 +00:00
|
|
|
end
|
2013-08-16 14:30:51 +00:00
|
|
|
|
2016-08-22 09:49:35 +00:00
|
|
|
# get history data
|
2016-09-02 10:54:13 +00:00
|
|
|
history_data = nil
|
2016-08-22 09:49:35 +00:00
|
|
|
|
2015-09-10 19:09:50 +00:00
|
|
|
# calculate first response escalation
|
2016-09-14 07:15:30 +00:00
|
|
|
if force == true || first_response_at_changed == true
|
2016-09-02 10:54:13 +00:00
|
|
|
if !history_data
|
|
|
|
history_data = history_get
|
|
|
|
end
|
|
|
|
if sla.first_response_time
|
2016-09-14 07:15:30 +00:00
|
|
|
self.first_response_escalation_at = destination_time(created_at, sla.first_response_time, biz, history_data)
|
2016-09-02 10:54:13 +00:00
|
|
|
end
|
2013-08-16 14:30:51 +00:00
|
|
|
|
2016-09-02 10:54:13 +00:00
|
|
|
# get response time in min
|
2016-09-14 07:15:30 +00:00
|
|
|
if first_response_at
|
2016-11-08 17:41:57 +00:00
|
|
|
self.first_response_in_min = period_working_minutes(created_at, first_response_at, biz, history_data)
|
2016-09-02 10:54:13 +00:00
|
|
|
end
|
2013-08-16 14:30:51 +00:00
|
|
|
|
2016-09-02 10:54:13 +00:00
|
|
|
# set time to show if sla is raised or not
|
|
|
|
if sla.first_response_time && first_response_in_min
|
|
|
|
self.first_response_diff_in_min = sla.first_response_time - first_response_in_min
|
|
|
|
end
|
2015-04-27 23:19:26 +00:00
|
|
|
end
|
2013-08-16 14:30:51 +00:00
|
|
|
|
2016-09-02 10:54:13 +00:00
|
|
|
# calculate update time escalation
|
2016-09-14 07:15:30 +00:00
|
|
|
if force == true || last_update_at_changed == true
|
2016-09-02 10:54:13 +00:00
|
|
|
if !history_data
|
|
|
|
history_data = history_get
|
|
|
|
end
|
2016-09-14 07:15:30 +00:00
|
|
|
if sla.update_time && last_update_at
|
|
|
|
self.update_escalation_at = destination_time(last_update_at, sla.update_time, biz, history_data)
|
2016-09-02 10:54:13 +00:00
|
|
|
end
|
2015-09-10 19:09:50 +00:00
|
|
|
|
2016-09-02 10:54:13 +00:00
|
|
|
# get update time in min
|
2016-09-14 07:15:30 +00:00
|
|
|
if last_update_at && last_update_at != created_at
|
2016-11-08 17:41:57 +00:00
|
|
|
self.update_in_min = period_working_minutes(created_at, last_update_at, biz, history_data)
|
2016-09-02 10:54:13 +00:00
|
|
|
end
|
2013-08-16 14:30:51 +00:00
|
|
|
|
2016-09-02 10:54:13 +00:00
|
|
|
# set sla time
|
2016-09-14 07:15:30 +00:00
|
|
|
if sla.update_time && update_in_min
|
|
|
|
self.update_diff_in_min = sla.update_time - update_in_min
|
2016-09-02 10:54:13 +00:00
|
|
|
end
|
2015-04-27 23:19:26 +00:00
|
|
|
end
|
2013-08-16 14:30:51 +00:00
|
|
|
|
2015-09-10 19:09:50 +00:00
|
|
|
# calculate close time escalation
|
2016-09-14 07:15:30 +00:00
|
|
|
if force == true || close_at_changed == true
|
2016-09-02 10:54:13 +00:00
|
|
|
if !history_data
|
|
|
|
history_data = history_get
|
|
|
|
end
|
|
|
|
if sla.solution_time
|
2016-09-14 07:15:30 +00:00
|
|
|
self.close_escalation_at = destination_time(created_at, sla.solution_time, biz, history_data)
|
2016-09-02 10:54:13 +00:00
|
|
|
end
|
2015-04-27 23:19:26 +00:00
|
|
|
|
2016-09-02 10:54:13 +00:00
|
|
|
# get close time in min
|
2016-09-14 07:15:30 +00:00
|
|
|
if close_at
|
2016-11-08 17:41:57 +00:00
|
|
|
self.close_in_min = period_working_minutes(created_at, close_at, biz, history_data)
|
2016-09-02 10:54:13 +00:00
|
|
|
end
|
2015-09-10 19:09:50 +00:00
|
|
|
|
2016-09-02 10:54:13 +00:00
|
|
|
# set time to show if sla is raised or not
|
2016-09-14 07:15:30 +00:00
|
|
|
if sla.solution_time && close_in_min
|
|
|
|
self.close_diff_in_min = sla.solution_time - close_in_min
|
2016-09-02 10:54:13 +00:00
|
|
|
end
|
2015-04-27 23:19:26 +00:00
|
|
|
end
|
2015-09-10 19:09:50 +00:00
|
|
|
|
2016-09-02 10:54:13 +00:00
|
|
|
# set closest escalation time
|
2015-09-10 19:09:50 +00:00
|
|
|
if escalation_disabled
|
2016-09-14 07:15:30 +00:00
|
|
|
self.escalation_at = nil
|
2016-09-02 10:54:13 +00:00
|
|
|
else
|
2016-09-14 07:15:30 +00:00
|
|
|
if !first_response_at && first_response_escalation_at
|
|
|
|
self.escalation_at = first_response_escalation_at
|
2016-09-02 10:54:13 +00:00
|
|
|
end
|
2016-09-14 07:15:30 +00:00
|
|
|
if update_escalation_at && ((!escalation_at && update_escalation_at) || update_escalation_at < escalation_at)
|
|
|
|
self.escalation_at = update_escalation_at
|
2016-09-02 10:54:13 +00:00
|
|
|
end
|
2016-09-14 07:15:30 +00:00
|
|
|
if !close_at && close_escalation_at && ((!escalation_at && close_escalation_at) || close_escalation_at < escalation_at)
|
|
|
|
self.escalation_at = close_escalation_at
|
2016-09-02 10:54:13 +00:00
|
|
|
end
|
2015-04-27 23:19:26 +00:00
|
|
|
end
|
2015-04-30 15:25:04 +00:00
|
|
|
|
2016-09-02 10:54:13 +00:00
|
|
|
# remember already counted time to do on next update only the diff
|
|
|
|
preferences[:escalation_calculation] = {
|
2016-09-14 07:15:30 +00:00
|
|
|
first_response_at: first_response_at,
|
|
|
|
last_update_at: last_update_at,
|
|
|
|
close_at: close_at,
|
2016-09-02 10:54:13 +00:00
|
|
|
sla_id: sla.id,
|
|
|
|
sla_updated_at: sla.updated_at,
|
|
|
|
calendar_id: calendar.id,
|
|
|
|
calendar_updated_at: calendar.updated_at,
|
|
|
|
escalation_disabled: escalation_disabled,
|
|
|
|
}
|
|
|
|
true
|
2015-04-27 23:19:26 +00:00
|
|
|
end
|
2013-08-16 14:30:51 +00:00
|
|
|
|
2013-08-25 23:04:41 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
return sla for ticket
|
|
|
|
|
|
|
|
ticket = Ticket.find(123)
|
|
|
|
result = ticket.escalation_calculation_get_sla
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = selected_sla
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
def escalation_calculation_get_sla
|
|
|
|
sla_selected = nil
|
2015-12-11 16:00:04 +00:00
|
|
|
sla_list = Cache.get('SLA::List::Active')
|
2015-05-07 10:11:45 +00:00
|
|
|
if sla_list.nil?
|
2016-01-19 06:58:58 +00:00
|
|
|
sla_list = Sla.all.order(:name, :created_at)
|
2015-12-11 16:00:04 +00:00
|
|
|
Cache.write('SLA::List::Active', sla_list, { expires_in: 1.hour })
|
2015-04-27 23:19:26 +00:00
|
|
|
end
|
2016-06-30 20:04:48 +00:00
|
|
|
sla_list.each { |sla|
|
2015-04-27 23:19:26 +00:00
|
|
|
if !sla.condition || sla.condition.empty?
|
|
|
|
sla_selected = sla
|
|
|
|
elsif sla.condition
|
2016-10-28 12:04:08 +00:00
|
|
|
query_condition, bind_condition, tables = Ticket.selector2sql(sla.condition)
|
|
|
|
ticket = Ticket.where(query_condition, *bind_condition).joins(tables).find_by(id: id)
|
2015-09-22 23:22:45 +00:00
|
|
|
next if !ticket
|
|
|
|
sla_selected = sla
|
|
|
|
break
|
2013-08-16 14:30:51 +00:00
|
|
|
end
|
2015-04-27 23:19:26 +00:00
|
|
|
}
|
|
|
|
sla_selected
|
|
|
|
end
|
2015-04-27 21:27:51 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
private
|
2013-08-16 14:30:51 +00:00
|
|
|
|
2016-08-02 12:12:24 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
return destination_time for time range
|
|
|
|
|
2016-08-22 09:49:35 +00:00
|
|
|
destination_time = destination_time(start_time, move_minutes, biz, history_data)
|
2016-08-02 12:12:24 +00:00
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
destination_time = Time.zone.parse('2016-08-02T11:11:11Z')
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2016-08-22 09:49:35 +00:00
|
|
|
def destination_time(start_time, move_minutes, biz, history_data)
|
2017-08-13 15:18:54 +00:00
|
|
|
local_destination_time = biz.time(move_minutes, :minutes).after(start_time)
|
2016-08-02 12:12:24 +00:00
|
|
|
|
2016-11-08 17:41:57 +00:00
|
|
|
# go step by step to end of move_minutes until move_minutes is 0
|
|
|
|
200.times.each { |_count|
|
2016-08-02 12:12:24 +00:00
|
|
|
|
|
|
|
# check if we have pending time in the range to the destination time
|
2017-08-13 15:18:54 +00:00
|
|
|
working_minutes = period_working_minutes(start_time, local_destination_time, biz, history_data, true)
|
2016-11-08 17:41:57 +00:00
|
|
|
move_minutes -= working_minutes
|
2016-08-02 12:12:24 +00:00
|
|
|
|
|
|
|
# skip if no pending time is given
|
2016-11-08 17:41:57 +00:00
|
|
|
break if move_minutes <= 0
|
2016-08-02 12:12:24 +00:00
|
|
|
|
|
|
|
# set pending destination to start time and add pending time to destination time
|
2017-08-13 15:18:54 +00:00
|
|
|
start_time = local_destination_time
|
|
|
|
local_destination_time = biz.time(move_minutes, :minutes).after(start_time)
|
2016-08-02 12:12:24 +00:00
|
|
|
}
|
2017-08-13 15:18:54 +00:00
|
|
|
local_destination_time
|
2016-08-02 12:12:24 +00:00
|
|
|
end
|
|
|
|
|
2016-11-08 17:41:57 +00:00
|
|
|
# get period working minutes time in minutes
|
|
|
|
def period_working_minutes(start_time, end_time, biz, history_list, add_current = false)
|
2015-04-27 21:27:51 +00:00
|
|
|
|
2016-08-02 12:12:24 +00:00
|
|
|
working_time_in_min = 0
|
|
|
|
last_state = nil
|
|
|
|
last_state_change = nil
|
|
|
|
ignore_escalation_states = Ticket::State.where(
|
|
|
|
ignore_escalation: true,
|
|
|
|
).map(&:name)
|
|
|
|
|
2016-11-08 17:41:57 +00:00
|
|
|
# add state changes till now
|
2017-09-23 06:25:55 +00:00
|
|
|
if add_current && saved_change_to_attribute('state_id') && saved_change_to_attribute('state_id')[0] && saved_change_to_attribute('state_id')[1]
|
2016-11-08 17:41:57 +00:00
|
|
|
last_history_state = nil
|
|
|
|
history_list.each { |history_item|
|
|
|
|
next if !history_item['attribute']
|
|
|
|
next if history_item['attribute'] != 'state'
|
|
|
|
next if history_item['id']
|
|
|
|
last_history_state = history_item
|
|
|
|
}
|
|
|
|
local_updated_at = updated_at
|
2017-09-23 06:25:55 +00:00
|
|
|
if saved_change_to_attribute('updated_at') && saved_change_to_attribute('updated_at')[1]
|
|
|
|
local_updated_at = saved_change_to_attribute('updated_at')[1]
|
2016-11-08 17:41:57 +00:00
|
|
|
end
|
|
|
|
history_item = {
|
|
|
|
'attribute' => 'state',
|
|
|
|
'created_at' => local_updated_at,
|
2017-09-23 06:25:55 +00:00
|
|
|
'value_from' => Ticket::State.find(saved_change_to_attribute('state_id')[0]).name,
|
|
|
|
'value_to' => Ticket::State.find(saved_change_to_attribute('state_id')[1]).name,
|
2016-11-08 17:41:57 +00:00
|
|
|
}
|
|
|
|
if last_history_state
|
|
|
|
last_history_state = history_item
|
|
|
|
else
|
|
|
|
history_list.push history_item
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
history_list.each { |history|
|
2015-04-27 23:19:26 +00:00
|
|
|
|
|
|
|
# ignore if it isn't a state change
|
2016-11-08 17:41:57 +00:00
|
|
|
next if !history['attribute']
|
|
|
|
next if history['attribute'] != 'state'
|
2015-04-27 23:19:26 +00:00
|
|
|
|
2016-11-08 17:41:57 +00:00
|
|
|
created_at = history['created_at']
|
2016-08-22 09:49:35 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# ignore all newer state before start_time
|
2016-08-22 09:49:35 +00:00
|
|
|
next if created_at < start_time
|
2015-04-27 23:19:26 +00:00
|
|
|
|
|
|
|
# ignore all older state changes after end_time
|
|
|
|
next if last_state_change && last_state_change > end_time
|
|
|
|
|
|
|
|
# if created_at is later then end_time, use end_time as last time
|
2016-08-22 09:49:35 +00:00
|
|
|
if created_at > end_time
|
|
|
|
created_at = end_time
|
2015-04-27 23:19:26 +00:00
|
|
|
end
|
2015-04-27 21:27:51 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# get initial state and time
|
|
|
|
if !last_state
|
2016-11-08 17:41:57 +00:00
|
|
|
last_state = history['value_from']
|
2015-04-27 23:19:26 +00:00
|
|
|
last_state_change = start_time
|
|
|
|
end
|
2015-04-27 21:27:51 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# check if time need to be counted
|
|
|
|
counted = true
|
2016-11-08 17:41:57 +00:00
|
|
|
if ignore_escalation_states.include?(history['value_from'])
|
2015-04-27 23:19:26 +00:00
|
|
|
counted = false
|
2013-08-16 14:30:51 +00:00
|
|
|
end
|
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
if counted
|
2016-11-08 17:41:57 +00:00
|
|
|
diff = biz.within(last_state_change, created_at).in_minutes
|
|
|
|
working_time_in_min += diff
|
2016-08-02 12:12:24 +00:00
|
|
|
end
|
2015-04-27 23:19:26 +00:00
|
|
|
|
|
|
|
# remember for next loop last state
|
2016-11-08 17:41:57 +00:00
|
|
|
last_state = history['value_to']
|
2016-08-22 09:49:35 +00:00
|
|
|
last_state_change = created_at
|
2015-04-27 23:19:26 +00:00
|
|
|
}
|
|
|
|
|
2016-11-08 17:41:57 +00:00
|
|
|
# if we have time to count after history entries has finished
|
|
|
|
if last_state_change && last_state_change < end_time
|
2015-09-10 19:09:50 +00:00
|
|
|
diff = biz.within(last_state_change, end_time).in_minutes
|
2016-11-08 17:41:57 +00:00
|
|
|
working_time_in_min += diff
|
2013-08-16 14:30:51 +00:00
|
|
|
end
|
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# if we have not had any state change
|
|
|
|
if !last_state_change
|
2015-09-10 19:09:50 +00:00
|
|
|
diff = biz.within(start_time, end_time).in_minutes
|
2016-11-08 17:41:57 +00:00
|
|
|
working_time_in_min += diff
|
2013-08-16 14:30:51 +00:00
|
|
|
end
|
2015-04-27 23:19:26 +00:00
|
|
|
|
2015-09-10 19:09:50 +00:00
|
|
|
working_time_in_min
|
2015-04-27 23:19:26 +00:00
|
|
|
end
|
|
|
|
|
2014-02-03 19:23:00 +00:00
|
|
|
end
|