ticket_waiting_time.rb 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 = Ticket.where(
  7. 'owner_id = ? AND group_id IN (?) AND updated_at > ?', user.id, group_ids, Time.zone.today
  8. )
  9. all_waiting = Ticket.where(
  10. 'group_id IN (?) AND updated_at > ?', group_ids, Time.zone.today
  11. )
  12. handling_time = calculate_average(own_waiting, Time.zone.today)
  13. if handling_time.positive?
  14. handling_time = (handling_time / 60).round
  15. end
  16. average_per_agent = calculate_average(all_waiting, Time.zone.today)
  17. if average_per_agent.positive?
  18. average_per_agent = (average_per_agent / 60).round
  19. end
  20. state = 'supergood'
  21. percent = 0
  22. state = if handling_time <= 60
  23. percent = handling_time.to_f / 60
  24. 'supergood'
  25. elsif handling_time <= 60 * 4
  26. percent = (handling_time.to_f - 60) / (60 * 3)
  27. 'good'
  28. elsif handling_time <= 60 * 8
  29. percent = (handling_time.to_f - 60 * 4) / (60 * 4)
  30. 'ok'
  31. else
  32. percent = 1.00
  33. 'bad'
  34. end
  35. {
  36. handling_time: handling_time,
  37. average_per_agent: average_per_agent,
  38. state: state,
  39. percent: percent,
  40. }
  41. end
  42. def self.average_state(result, _user_id)
  43. result
  44. end
  45. def self.calculate_average(tickets, start_time)
  46. average_time = 0
  47. count_articles = 0
  48. tickets.each do |ticket|
  49. count_time = 0
  50. ticket.articles.joins(:type).where('ticket_articles.created_at > ? AND ticket_articles.internal = ? AND ticket_article_types.communication = ?', start_time, false, true).each do |article|
  51. if article.sender.name == 'Customer'
  52. count_time = article.created_at.to_i
  53. elsif count_time.positive?
  54. average_time += article.created_at.to_i - count_time
  55. count_articles += 1
  56. count_time = 0
  57. end
  58. end
  59. end
  60. if count_articles.positive?
  61. average_time = average_time / count_articles
  62. end
  63. average_time
  64. end
  65. end