ticket_waiting_time.rb 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class Stats::TicketWaitingTime
  3. def self.generate(user)
  4. # get users groups
  5. group_ids = user.group_ids_access('full')
  6. own_waiting = []
  7. all_waiting = []
  8. Ticket.where('group_id IN (?) AND updated_at > ?', group_ids.sort, Time.zone.today).limit(20_000).pluck(:id, :owner_id).each do |ticket|
  9. all_waiting.push ticket[0]
  10. if ticket[1] == user.id
  11. own_waiting.push ticket[0]
  12. end
  13. end
  14. handling_time = calculate_average(own_waiting, Time.zone.today)
  15. if handling_time.positive?
  16. handling_time = (handling_time / 60).round
  17. end
  18. average_per_agent = calculate_average(all_waiting, Time.zone.today)
  19. if average_per_agent.positive?
  20. average_per_agent = (average_per_agent / 60).round
  21. end
  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(ticket_ids, start_time)
  47. average_time = 0
  48. count_articles = 0
  49. last_ticket_id = nil
  50. count_time = nil
  51. Ticket::Article.joins(:type).joins(:sender).where('ticket_articles.ticket_id IN (?) AND ticket_articles.created_at > ? AND ticket_articles.internal = ? AND ticket_article_types.communication = ?', ticket_ids, start_time, false, true).order(:ticket_id, :created_at).pluck(:created_at, :sender_id, :ticket_id, :id).each do |article|
  52. if last_ticket_id != article[2]
  53. last_ticket_id = article[2]
  54. count_time = 0
  55. end
  56. sender = Ticket::Article::Sender.lookup(id: article[1])
  57. if sender.name == 'Customer'
  58. count_time = article[0].to_i
  59. elsif count_time.positive?
  60. average_time += article[0].to_i - count_time
  61. count_articles += 1
  62. count_time = 0
  63. end
  64. end
  65. if count_articles.positive?
  66. average_time = average_time / count_articles
  67. end
  68. average_time
  69. end
  70. end