escalation.rb 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
  2. module Ticket::Escalation
  3. =begin
  4. rebuild escalations for all open tickets
  5. result = Ticket::Escalation.rebuild_all
  6. returns
  7. result = true
  8. =end
  9. def self.rebuild_all
  10. state_list_open = Ticket::State.by_category( 'open' )
  11. tickets = Ticket.where( :state_id => state_list_open )
  12. tickets.each {|ticket|
  13. ticket.escalation_calculation
  14. }
  15. end
  16. =begin
  17. rebuild escalation for ticket
  18. ticket = Ticket.find(123)
  19. result = ticket.escalation_calculation
  20. returns
  21. result = true
  22. =end
  23. def escalation_calculation
  24. # set escalation off if ticket is already closed
  25. state = Ticket::State.lookup( :id => self.state_id )
  26. if state.ignore_escalation?
  27. # nothing to change
  28. return true if !self.escalation_time
  29. self.escalation_time = nil
  30. # self.first_response_escal_date = nil
  31. # self.close_time_escal_date = nil
  32. self.callback_loop = true
  33. self.save
  34. return true
  35. end
  36. # get sla for ticket
  37. sla_selected = escalation_calculation_get_sla
  38. # reset escalation if no sla is set
  39. if !sla_selected
  40. # nothing to change
  41. return true if !self.escalation_time
  42. self.escalation_time = nil
  43. # self.first_response_escal_date = nil
  44. # self.close_time_escal_date = nil
  45. self.callback_loop = true
  46. self.save
  47. return true
  48. end
  49. # puts sla_selected.inspect
  50. # puts days.inspect
  51. self.escalation_time = nil
  52. self.first_response_escal_date = nil
  53. self.update_time_escal_date = nil
  54. self.close_time_escal_date = nil
  55. # first response
  56. if sla_selected.first_response_time
  57. # get escalation date without pending time
  58. self.first_response_escal_date = TimeCalculation.dest_time( self.created_at, sla_selected.first_response_time, sla_selected.data, sla_selected.timezone )
  59. # get pending time between created and first response escal. time
  60. time_in_pending = escalation_suspend( self.created_at, self.first_response_escal_date, 'relative', sla_selected, sla_selected.first_response_time )
  61. # get new escalation time (original escal_date + time_in_pending)
  62. self.first_response_escal_date = TimeCalculation.dest_time( self.first_response_escal_date, time_in_pending.to_i, sla_selected.data, sla_selected.timezone )
  63. # set ticket escalation
  64. self.escalation_time = calculation_higher_time( self.escalation_time, self.first_response_escal_date, self.first_response )
  65. end
  66. if self.first_response# && !self.first_response_in_min
  67. # get response time in min between created and first response
  68. self.first_response_in_min = escalation_suspend( self.created_at, self.first_response, 'real', sla_selected )
  69. end
  70. # set time to show if sla is raised ot in
  71. if sla_selected.first_response_time && self.first_response_in_min
  72. self.first_response_diff_in_min = sla_selected.first_response_time - self.first_response_in_min
  73. end
  74. # update time
  75. last_update = self.last_contact_agent
  76. if !last_update
  77. last_update = self.created_at
  78. end
  79. if sla_selected.update_time
  80. self.update_time_escal_date = TimeCalculation.dest_time( last_update, sla_selected.update_time, sla_selected.data, sla_selected.timezone )
  81. # get pending time between created and update escal. time
  82. time_in_pending = escalation_suspend( last_update, self.update_time_escal_date, 'relative', sla_selected, sla_selected.update_time )
  83. # get new escalation time (original escal_date + time_in_pending)
  84. self.update_time_escal_date = TimeCalculation.dest_time( self.update_time_escal_date, time_in_pending.to_i, sla_selected.data, sla_selected.timezone )
  85. # set ticket escalation
  86. self.escalation_time = calculation_higher_time( self.escalation_time, self.update_time_escal_date, false )
  87. end
  88. if self.last_contact_agent
  89. self.update_time_in_min = TimeCalculation.business_time_diff( self.created_at, self.last_contact_agent, sla_selected.data, sla_selected.timezone )
  90. end
  91. # set sla time
  92. if sla_selected.update_time && self.update_time_in_min
  93. self.update_time_diff_in_min = sla_selected.update_time - self.update_time_in_min
  94. end
  95. # close time
  96. if sla_selected.close_time
  97. # get escalation date without pending time
  98. self.close_time_escal_date = TimeCalculation.dest_time( self.created_at, sla_selected.close_time, sla_selected.data, sla_selected.timezone )
  99. # get pending time between created and close escal. time
  100. extended_escalation = escalation_suspend( self.created_at, self.close_time_escal_date, 'relative', sla_selected, sla_selected.close_time )
  101. # get new escalation time (original escal_date + time_in_pending)
  102. self.close_time_escal_date = TimeCalculation.dest_time( self.close_time_escal_date, extended_escalation.to_i, sla_selected.data, sla_selected.timezone )
  103. # set ticket escalation
  104. self.escalation_time = calculation_higher_time( self.escalation_time, self.close_time_escal_date, self.close_time )
  105. end
  106. if self.close_time # && !self.close_time_in_min
  107. self.close_time_in_min = escalation_suspend( self.created_at, self.close_time, 'real', sla_selected )
  108. end
  109. # set sla time
  110. if sla_selected.close_time && self.close_time_in_min
  111. self.close_time_diff_in_min = sla_selected.close_time - self.close_time_in_min
  112. end
  113. if self.changed?
  114. self.callback_loop = true
  115. self.save
  116. end
  117. end
  118. =begin
  119. return sla for ticket
  120. ticket = Ticket.find(123)
  121. result = ticket.escalation_calculation_get_sla
  122. returns
  123. result = selected_sla
  124. =end
  125. def escalation_calculation_get_sla
  126. sla_selected = nil
  127. sla_list = Cache.get( 'SLA::List::Active' )
  128. if sla_list == nil
  129. sla_list = Sla.where( :active => true )
  130. Cache.write( 'SLA::List::Active', sla_list, { :expires_in => 1.hour } )
  131. end
  132. sla_list.each {|sla|
  133. if !sla.condition || sla.condition.empty?
  134. sla_selected = sla
  135. elsif sla.condition
  136. hit = false
  137. map = [
  138. [ 'tickets.priority_id', 'priority_id' ],
  139. [ 'tickets.group_id', 'group_id' ]
  140. ]
  141. map.each {|item|
  142. if sla.condition[ item[0] ]
  143. if sla.condition[ item[0] ].class == String
  144. sla.condition[ item[0] ] = [ sla.condition[ item[0] ] ]
  145. end
  146. if sla.condition[ item[0] ].include?( self[ item[1] ].to_s )
  147. hit = true
  148. else
  149. hit = false
  150. end
  151. end
  152. }
  153. if hit
  154. sla_selected = sla
  155. end
  156. end
  157. }
  158. sla_selected
  159. end
  160. private
  161. #type could be:
  162. # real - time without supsend state
  163. # relative - only suspend time
  164. def escalation_suspend (start_time, end_time, type, sla_selected, sla_time = 0)
  165. if type == 'relative'
  166. end_time += sla_time * 60
  167. end
  168. total_time_without_pending = 0
  169. total_time = 0
  170. #get history for ticket
  171. history_list = self.history_get
  172. #loop through hist. changes and get time
  173. last_state = nil
  174. last_state_change = nil
  175. last_state_is_pending = false
  176. history_list.each { |history_item|
  177. # ignore if it isn't a state change
  178. next if !history_item['attribute']
  179. next if history_item['attribute'] != 'state'
  180. # ignore all newer state before start_time
  181. next if history_item['created_at'] < start_time
  182. # ignore all older state changes after end_time
  183. next if last_state_change && last_state_change > end_time
  184. # if created_at is later then end_time, use end_time as last time
  185. if history_item['created_at'] > end_time
  186. history_item['created_at'] = end_time
  187. end
  188. # get initial state and time
  189. if !last_state
  190. last_state = history_item['value_from']
  191. last_state_change = start_time
  192. end
  193. # check if time need to be counted
  194. counted = true
  195. if history_item['value_from'] == 'pending'
  196. counted = false
  197. elsif history_item['value_from'] == 'close'
  198. counted = false
  199. end
  200. diff = escalation_time_diff( last_state_change, history_item['created_at'], sla_selected )
  201. if counted
  202. # puts "Diff count #{history_item['value_from']} -> #{history_item['value_to']} / #{last_state_change} -> #{history_item['created_at']}"
  203. total_time_without_pending = total_time_without_pending + diff
  204. else
  205. # puts "Diff not count #{history_item['value_from']} -> #{history_item['value_to']} / #{last_state_change} -> #{history_item['created_at']}"
  206. end
  207. total_time = total_time + diff
  208. if history_item['value_to'] == 'pending'
  209. last_state_is_pending = true
  210. else
  211. last_state_is_pending = false
  212. end
  213. # remember for next loop last state
  214. last_state = history_item['value_to']
  215. last_state_change = history_item['created_at']
  216. }
  217. # if last state isnt pending, count rest
  218. if !last_state_is_pending && last_state_change && last_state_change < end_time
  219. diff = escalation_time_diff( last_state_change, end_time, sla_selected )
  220. # puts "Diff count last state was not pending #{diff.to_s} - #{last_state_change} - #{end_time}"
  221. total_time_without_pending = total_time_without_pending + diff
  222. total_time = total_time + diff
  223. end
  224. # if we have not had any state change
  225. if !last_state_change
  226. diff = escalation_time_diff( start_time, end_time, sla_selected )
  227. # puts 'Diff state has not changed ' + diff.to_s
  228. total_time_without_pending = total_time_without_pending + diff
  229. total_time = total_time + diff
  230. end
  231. #return sum
  232. if type == 'real'
  233. return total_time_without_pending
  234. elsif type == 'relative'
  235. relative = total_time - total_time_without_pending
  236. return relative
  237. else
  238. raise "ERROR: Unknown type #{type}"
  239. end
  240. end
  241. def escalation_time_diff( start_time, end_time, sla_selected )
  242. if sla_selected
  243. diff = TimeCalculation.business_time_diff( start_time, end_time, sla_selected.data, sla_selected.timezone)
  244. else
  245. diff = TimeCalculation.business_time_diff( start_time, end_time )
  246. end
  247. diff
  248. end
  249. def calculation_higher_time(escalation_time, check_time, done_time)
  250. return escalation_time if done_time
  251. return check_time if !escalation_time
  252. return escalation_time if !check_time
  253. return check_time if escalation_time > check_time
  254. return escalation_time
  255. end
  256. end