ticket_waiting_time.rb 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class Stats::TicketWaitingTime
  3. def self.generate(user)
  4. open_state_ids = Ticket::State.by_category(:open).pluck(:id)
  5. # get users groups
  6. group_ids = user.groups.map(&:id)
  7. own_waiting = Ticket.where(
  8. 'owner_id = ? AND group_id IN (?) AND state_id IN (?) AND updated_at > ?', user.id, group_ids, open_state_ids, Time.zone.today
  9. )
  10. all_waiting = Ticket.where(
  11. 'group_id IN (?) AND state_id IN (?) AND updated_at > ?', group_ids, open_state_ids, Time.zone.today
  12. )
  13. handling_time = calculate_average(own_waiting, Time.zone.today)
  14. if handling_time.positive?
  15. handling_time = (handling_time / 60).round
  16. end
  17. average_per_agent = calculate_average(all_waiting, Time.zone.today)
  18. if average_per_agent.positive?
  19. average_per_agent = (average_per_agent / 60).round
  20. end
  21. state = 'supergood'
  22. percent = 0
  23. state = if handling_time <= 60
  24. percent = handling_time.to_f / 60
  25. 'supergood'
  26. elsif handling_time <= 60 * 4
  27. percent = (handling_time.to_f - 60) / (60 * 3)
  28. 'good'
  29. elsif handling_time <= 60 * 8
  30. percent = (handling_time.to_f - 60 * 4) / (60 * 4)
  31. 'ok'
  32. else
  33. percent = 1.00
  34. 'bad'
  35. end
  36. {
  37. handling_time: handling_time,
  38. average_per_agent: average_per_agent,
  39. state: state,
  40. percent: percent,
  41. }
  42. end
  43. def self.average_state(result, _user_id)
  44. result
  45. end
  46. def self.calculate_average(tickets, start_time)
  47. average_time = 0
  48. count_time = 0
  49. tickets.each { |ticket|
  50. ticket.articles.joins(:type).where('ticket_articles.created_at > ? AND ticket_articles.internal = ? AND ticket_article_types.communication = ?', start_time, false, true).each { |article|
  51. if article.sender.name == 'Customer'
  52. count_time = article.created_at.to_i
  53. else
  54. average_time += article.created_at.to_i - count_time
  55. count_time = 0
  56. end
  57. }
  58. }
  59. average_time
  60. end
  61. end