escalation.rb 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. # Copyright (C) 2012-2016 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. ticket_ids = Ticket.where(state_id: state_list_open).limit(20_000).pluck(:id)
  12. ticket_ids.each do |ticket_id|
  13. next if !Ticket.exists?(ticket_id)
  14. Ticket.find(ticket_id).escalation_calculation(true)
  15. end
  16. end
  17. =begin
  18. rebuild escalation for ticket
  19. ticket = Ticket.find(123)
  20. result = ticket.escalation_calculation
  21. returns
  22. result = true # true = ticket has been updated | false = no changes on ticket
  23. =end
  24. def escalation_calculation(force = false)
  25. return if !escalation_calculation_int(force)
  26. self.callback_loop = true
  27. save!
  28. self.callback_loop = false
  29. true
  30. end
  31. def escalation_calculation_int(force = false)
  32. return if callback_loop == true
  33. # return if we run import mode
  34. return if Setting.get('import_mode') && !Setting.get('import_ignore_sla')
  35. # set escalation off if current state is not escalation relativ (e. g. ticket is closed)
  36. return if !state_id
  37. state = Ticket::State.lookup(id: state_id)
  38. escalation_disabled = false
  39. if state.ignore_escalation?
  40. escalation_disabled = true
  41. # early exit if nothing current state is not escalation relativ
  42. if !force
  43. return false if escalation_at.nil?
  44. self.escalation_at = nil
  45. if preferences['escalation_calculation']
  46. preferences['escalation_calculation']['escalation_disabled'] = escalation_disabled
  47. end
  48. return true
  49. end
  50. end
  51. # get sla for ticket
  52. calendar = nil
  53. sla = escalation_calculation_get_sla
  54. if sla
  55. calendar = sla.calendar
  56. end
  57. # if no escalation is enabled
  58. if !sla || !calendar
  59. # nothing to change
  60. return false if !escalation_at && !first_response_escalation_at && !update_escalation_at && !close_escalation_at
  61. preferences['escalation_calculation'] = {}
  62. self.escalation_at = nil
  63. self.first_response_escalation_at = nil
  64. self.escalation_at = nil
  65. self.update_escalation_at = nil
  66. self.close_escalation_at = nil
  67. if preferences['escalation_calculation']
  68. preferences['escalation_calculation']['escalation_disabled'] = escalation_disabled
  69. end
  70. return true
  71. end
  72. # get last_update_at
  73. if !last_contact_customer_at && !last_contact_agent_at
  74. last_update_at = created_at
  75. elsif !last_contact_customer_at && last_contact_agent_at
  76. last_update_at = last_contact_agent_at
  77. elsif last_contact_customer_at && !last_contact_agent_at
  78. last_update_at = last_contact_customer_at
  79. elsif last_contact_agent_at > last_contact_customer_at
  80. last_update_at = last_contact_agent_at
  81. elsif last_contact_agent_at < last_contact_customer_at
  82. last_update_at = last_contact_customer_at
  83. end
  84. # check if calculation need be done
  85. escalation_calculation = preferences[:escalation_calculation] || {}
  86. sla_changed = true
  87. if escalation_calculation['sla_id'] == sla.id && escalation_calculation['sla_updated_at'] == sla.updated_at
  88. sla_changed = false
  89. end
  90. calendar_changed = true
  91. if escalation_calculation['calendar_id'] == calendar.id && escalation_calculation['calendar_updated_at'] == calendar.updated_at
  92. calendar_changed = false
  93. end
  94. if sla_changed == true || calendar_changed == true
  95. force = true
  96. end
  97. first_response_at_changed = true
  98. if escalation_calculation['first_response_at'] == first_response_at
  99. first_response_at_changed = false
  100. end
  101. last_update_at_changed = true
  102. if escalation_calculation['last_update_at'] == last_update_at && !saved_change_to_attribute('state_id')
  103. last_update_at_changed = false
  104. end
  105. close_at_changed = true
  106. if escalation_calculation['close_at'] == close_at
  107. close_at_changed = false
  108. end
  109. if !force && preferences[:escalation_calculation]
  110. if first_response_at_changed == false &&
  111. last_update_at_changed == false &&
  112. close_at_changed == false &&
  113. sla_changed == false &&
  114. calendar_changed == false &&
  115. escalation_calculation['escalation_disabled'] == escalation_disabled
  116. return false
  117. end
  118. end
  119. # reset escalation attributes
  120. self.escalation_at = nil
  121. if force == true
  122. self.first_response_escalation_at = nil
  123. self.update_escalation_at = nil
  124. self.close_escalation_at = nil
  125. end
  126. biz = Biz::Schedule.new do |config|
  127. # get business hours
  128. hours = calendar.business_hours_to_hash
  129. raise "No configured hours found in calendar #{calendar.inspect}" if hours.blank?
  130. config.hours = hours
  131. # get holidays
  132. config.holidays = calendar.public_holidays_to_array
  133. config.time_zone = calendar.timezone
  134. end
  135. # get history data
  136. history_data = nil
  137. # calculate first response escalation
  138. if force == true || first_response_at_changed == true
  139. if !history_data
  140. history_data = history_get
  141. end
  142. if sla.first_response_time
  143. self.first_response_escalation_at = destination_time(created_at, sla.first_response_time, biz, history_data)
  144. end
  145. # get response time in min
  146. if first_response_at
  147. self.first_response_in_min = period_working_minutes(created_at, first_response_at, biz, history_data)
  148. end
  149. # set time to show if sla is raised or not
  150. if sla.first_response_time && first_response_in_min
  151. self.first_response_diff_in_min = sla.first_response_time - first_response_in_min
  152. end
  153. end
  154. # calculate update time escalation
  155. if force == true || last_update_at_changed == true
  156. if !history_data
  157. history_data = history_get
  158. end
  159. if sla.update_time && last_update_at
  160. self.update_escalation_at = destination_time(last_update_at, sla.update_time, biz, history_data)
  161. end
  162. # get update time in min
  163. if last_update_at && last_update_at != created_at
  164. self.update_in_min = period_working_minutes(created_at, last_update_at, biz, history_data)
  165. end
  166. # set sla time
  167. if sla.update_time && update_in_min
  168. self.update_diff_in_min = sla.update_time - update_in_min
  169. end
  170. end
  171. # calculate close time escalation
  172. if force == true || close_at_changed == true
  173. if !history_data
  174. history_data = history_get
  175. end
  176. if sla.solution_time
  177. self.close_escalation_at = destination_time(created_at, sla.solution_time, biz, history_data)
  178. end
  179. # get close time in min
  180. if close_at
  181. self.close_in_min = period_working_minutes(created_at, close_at, biz, history_data)
  182. end
  183. # set time to show if sla is raised or not
  184. if sla.solution_time && close_in_min
  185. self.close_diff_in_min = sla.solution_time - close_in_min
  186. end
  187. end
  188. # set closest escalation time
  189. if escalation_disabled
  190. self.escalation_at = nil
  191. else
  192. if !first_response_at && first_response_escalation_at
  193. self.escalation_at = first_response_escalation_at
  194. end
  195. if update_escalation_at && ((!escalation_at && update_escalation_at) || update_escalation_at < escalation_at)
  196. self.escalation_at = update_escalation_at
  197. end
  198. if !close_at && close_escalation_at && ((!escalation_at && close_escalation_at) || close_escalation_at < escalation_at)
  199. self.escalation_at = close_escalation_at
  200. end
  201. end
  202. # remember already counted time to do on next update only the diff
  203. preferences[:escalation_calculation] = {
  204. first_response_at: first_response_at,
  205. last_update_at: last_update_at,
  206. close_at: close_at,
  207. sla_id: sla.id,
  208. sla_updated_at: sla.updated_at,
  209. calendar_id: calendar.id,
  210. calendar_updated_at: calendar.updated_at,
  211. escalation_disabled: escalation_disabled,
  212. }
  213. true
  214. end
  215. =begin
  216. return sla for ticket
  217. ticket = Ticket.find(123)
  218. result = ticket.escalation_calculation_get_sla
  219. returns
  220. result = selected_sla
  221. =end
  222. def escalation_calculation_get_sla
  223. sla_selected = nil
  224. sla_list = Cache.get('SLA::List::Active')
  225. if sla_list.nil?
  226. sla_list = Sla.all.order(:name, :created_at)
  227. Cache.write('SLA::List::Active', sla_list, { expires_in: 1.hour })
  228. end
  229. sla_list.each do |sla|
  230. if sla.condition.blank?
  231. sla_selected = sla
  232. elsif sla.condition
  233. query_condition, bind_condition, tables = Ticket.selector2sql(sla.condition)
  234. ticket = Ticket.where(query_condition, *bind_condition).joins(tables).find_by(id: id)
  235. next if !ticket
  236. sla_selected = sla
  237. break
  238. end
  239. end
  240. sla_selected
  241. end
  242. private
  243. =begin
  244. return destination_time for time range
  245. destination_time = destination_time(start_time, move_minutes, biz, history_data)
  246. returns
  247. destination_time = Time.zone.parse('2016-08-02T11:11:11Z')
  248. =end
  249. def destination_time(start_time, move_minutes, biz, history_data)
  250. local_destination_time = biz.time(move_minutes, :minutes).after(start_time)
  251. # go step by step to end of move_minutes until move_minutes is 0
  252. 200.times.each do |_count|
  253. # check if we have pending time in the range to the destination time
  254. working_minutes = period_working_minutes(start_time, local_destination_time, biz, history_data, true)
  255. move_minutes -= working_minutes
  256. # skip if no pending time is given
  257. break if move_minutes <= 0
  258. # set pending destination to start time and add pending time to destination time
  259. start_time = local_destination_time
  260. local_destination_time = biz.time(move_minutes, :minutes).after(start_time)
  261. end
  262. local_destination_time
  263. end
  264. # get period working minutes time in minutes
  265. def period_working_minutes(start_time, end_time, biz, history_list, add_current = false)
  266. working_time_in_min = 0
  267. last_state = nil
  268. last_state_change = nil
  269. ignore_escalation_states = Ticket::State.where(
  270. ignore_escalation: true,
  271. ).map(&:name)
  272. # add state changes till now
  273. if add_current && saved_change_to_attribute('state_id') && saved_change_to_attribute('state_id')[0] && saved_change_to_attribute('state_id')[1]
  274. last_history_state = nil
  275. history_list.each do |history_item|
  276. next if !history_item['attribute']
  277. next if history_item['attribute'] != 'state'
  278. next if history_item['id']
  279. last_history_state = history_item
  280. end
  281. local_updated_at = updated_at
  282. if saved_change_to_attribute('updated_at') && saved_change_to_attribute('updated_at')[1]
  283. local_updated_at = saved_change_to_attribute('updated_at')[1]
  284. end
  285. history_item = {
  286. 'attribute' => 'state',
  287. 'created_at' => local_updated_at,
  288. 'value_from' => Ticket::State.find(saved_change_to_attribute('state_id')[0]).name,
  289. 'value_to' => Ticket::State.find(saved_change_to_attribute('state_id')[1]).name,
  290. }
  291. if last_history_state
  292. last_history_state = history_item
  293. else
  294. history_list.push history_item
  295. end
  296. end
  297. history_list.each do |history|
  298. # ignore if it isn't a state change
  299. next if !history['attribute']
  300. next if history['attribute'] != 'state'
  301. created_at = history['created_at']
  302. # ignore all newer state before start_time
  303. next if created_at < start_time
  304. # ignore all older state changes after end_time
  305. next if last_state_change && last_state_change > end_time
  306. # if created_at is later then end_time, use end_time as last time
  307. if created_at > end_time
  308. created_at = end_time
  309. end
  310. # get initial state and time
  311. if !last_state
  312. last_state = history['value_from']
  313. last_state_change = start_time
  314. end
  315. # check if time need to be counted
  316. counted = true
  317. if ignore_escalation_states.include?(history['value_from'])
  318. counted = false
  319. end
  320. if counted
  321. diff = biz.within(last_state_change, created_at).in_minutes
  322. working_time_in_min += diff
  323. end
  324. # remember for next loop last state
  325. last_state = history['value_to']
  326. last_state_change = created_at
  327. end
  328. # if we have time to count after history entries has finished
  329. if last_state_change && last_state_change < end_time
  330. diff = biz.within(last_state_change, end_time).in_minutes
  331. working_time_in_min += diff
  332. end
  333. # if we have not had any state change
  334. if !last_state_change
  335. diff = biz.within(start_time, end_time).in_minutes
  336. working_time_in_min += diff
  337. end
  338. working_time_in_min
  339. end
  340. end