ticket_waiting_time.rb 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://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. { handling_time:, average_per_agent:, state:, percent: }
  37. end
  38. def self.average_state(result, _user_id)
  39. result
  40. end
  41. def self.calculate_average(ticket_ids, start_time)
  42. average_time = 0
  43. count_articles = 0
  44. last_ticket_id = nil
  45. count_time = nil
  46. 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).reorder(:ticket_id, :created_at).pluck(:created_at, :sender_id, :ticket_id, :id).each do |article|
  47. if last_ticket_id != article[2]
  48. last_ticket_id = article[2]
  49. count_time = 0
  50. end
  51. sender = Ticket::Article::Sender.lookup(id: article[1])
  52. if sender.name == 'Customer'
  53. count_time = article[0].to_i
  54. elsif count_time.positive?
  55. average_time += article[0].to_i - count_time
  56. count_articles += 1
  57. count_time = 0
  58. end
  59. end
  60. if count_articles.positive?
  61. average_time /= count_articles
  62. end
  63. average_time
  64. end
  65. end