ticket_waiting_time.rb 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. state = 'supergood'
  23. percent = 0
  24. state = if handling_time <= 60
  25. percent = handling_time.to_f / 60
  26. 'supergood'
  27. elsif handling_time <= 60 * 4
  28. percent = (handling_time.to_f - 60) / (60 * 3)
  29. 'good'
  30. elsif handling_time <= 60 * 8
  31. percent = (handling_time.to_f - 60 * 4) / (60 * 4)
  32. 'ok'
  33. else
  34. percent = 1.00
  35. 'bad'
  36. end
  37. {
  38. handling_time: handling_time,
  39. average_per_agent: average_per_agent,
  40. state: state,
  41. percent: percent,
  42. }
  43. end
  44. def self.average_state(result, _user_id)
  45. result
  46. end
  47. def self.calculate_average(ticket_ids, start_time)
  48. average_time = 0
  49. count_articles = 0
  50. last_ticket_id = nil
  51. count_time = nil
  52. 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|
  53. if last_ticket_id != article[2]
  54. last_ticket_id = article[2]
  55. count_time = 0
  56. end
  57. sender = Ticket::Article::Sender.lookup(id: article[1])
  58. if sender.name == 'Customer'
  59. count_time = article[0].to_i
  60. elsif count_time.positive?
  61. average_time += article[0].to_i - count_time
  62. count_articles += 1
  63. count_time = 0
  64. end
  65. end
  66. if count_articles.positive?
  67. average_time = average_time / count_articles
  68. end
  69. average_time
  70. end
  71. end