ticket_waiting_time_spec.rb 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Stats::TicketWaitingTime do
  4. describe '.generate' do
  5. let(:user) { create(:agent, groups: [group]) }
  6. let(:group) { create(:group) }
  7. context 'when given an agent with no tickets' do
  8. it 'returns a hash with 1-day average ticket wait time for user (in minutes)' do
  9. expect(described_class.generate(user)).to include(handling_time: 0)
  10. end
  11. it 'returns a hash with 1-day average ticket wait time across user’s groups (in minutes)' do
  12. expect(described_class.generate(user)).to include(average_per_agent: 0)
  13. end
  14. it 'returns a hash with verbal grade for average ticket wait time' do
  15. expect(described_class.generate(user)).to include(state: 'supergood')
  16. end
  17. it 'returns a hash with decimal score (0–1) of user’s risk of falling to a lower grade' do
  18. expect(described_class.generate(user)).to include(percent: 0.0)
  19. end
  20. context 'and who belongs to a group with other tickets' do
  21. let(:ticket) { create(:ticket, group: group) }
  22. before do
  23. create(:ticket_article, sender_name: 'Customer', ticket: ticket, created_at: 1.hour.from_now)
  24. create(:ticket_article, sender_name: 'Agent', ticket: ticket, created_at: 2.hours.from_now)
  25. end
  26. it 'returns a hash with 1-day average ticket wait time across user’s groups (in minutes)' do
  27. expect(described_class.generate(user)).to include(average_per_agent: 60)
  28. end
  29. end
  30. end
  31. context 'when given an agent with recent (since start-of-day) customer exchanges' do
  32. let(:ticket) { create(:ticket, group: group, owner_id: user.id) }
  33. before do
  34. create(:ticket_article, sender_name: 'Customer', ticket: ticket, created_at: 1.hour.from_now)
  35. create(:ticket_article, sender_name: 'Agent', ticket: ticket, created_at: 2.hours.from_now)
  36. end
  37. it 'returns a hash with 1-day average ticket wait time for user (in minutes)' do
  38. expect(described_class.generate(user)).to include(handling_time: 60)
  39. end
  40. it 'returns a hash with 1-day average ticket wait time across user’s groups (in minutes)' do
  41. expect(described_class.generate(user)).to include(average_per_agent: 60)
  42. end
  43. it 'returns a hash with verbal grade for average ticket wait time' do
  44. expect(described_class.generate(user)).to include(state: 'supergood')
  45. end
  46. it 'returns a hash with decimal score (0–1) of user’s risk of falling to a lower grade' do
  47. expect(described_class.generate(user)).to include(percent: 1.0)
  48. end
  49. context 'and who belongs to a group with other tickets' do
  50. let(:other_ticket) { create(:ticket, group: group) }
  51. before do
  52. create(:ticket_article, sender_name: 'Customer', ticket: other_ticket, created_at: 1.hour.from_now)
  53. create(:ticket_article, sender_name: 'Agent', ticket: other_ticket, created_at: 3.hours.from_now)
  54. end
  55. it 'returns a hash with 1-day average ticket wait time across user’s groups (in minutes)' do
  56. expect(described_class.generate(user)).to include(average_per_agent: 90)
  57. end
  58. end
  59. end
  60. end
  61. describe '.calculate_average' do
  62. let(:ticket) { create(:ticket) }
  63. let(:start_time) { Time.current.beginning_of_day }
  64. context 'with empty tickets (no articles)' do
  65. it 'returns 0' do
  66. expect(described_class.calculate_average(ticket.id, start_time)).to eq(0)
  67. end
  68. end
  69. context 'with old articles (last message predates given start time)' do
  70. before do
  71. create(:ticket_article, sender_name: 'Customer', ticket: ticket, created_at: 1.day.ago)
  72. create(:ticket_article, sender_name: 'Agent', ticket: ticket, created_at: 1.day.ago)
  73. end
  74. it 'returns 0' do
  75. expect(described_class.calculate_average(ticket.id, start_time)).to eq(0)
  76. end
  77. end
  78. context 'with a single exchange' do
  79. before do
  80. create(:ticket_article, sender_name: 'Customer', ticket: ticket, created_at: start_time + 1.minute)
  81. create(:ticket_article, sender_name: 'Agent', ticket: ticket, created_at: start_time + 2.minutes)
  82. end
  83. it 'returns elapsed time' do
  84. expect(described_class.calculate_average(ticket.id, start_time)).to eq(1.minute)
  85. end
  86. end
  87. context 'with internal notes' do
  88. before do
  89. create(:ticket_article, sender_name: 'Customer', ticket: ticket, created_at: start_time + 1.minute)
  90. create(:ticket_article, sender_name: 'Agent', ticket: ticket, created_at: start_time + 2.minutes, internal: true)
  91. create(:ticket_article, sender_name: 'Agent', ticket: ticket, created_at: start_time + 3.minutes)
  92. end
  93. it 'ignores them (and measures time to actual response)' do
  94. expect(described_class.calculate_average(ticket.id, start_time)).to eq(2.minutes)
  95. end
  96. end
  97. context 'with multiple exchanges' do
  98. before do
  99. create(:ticket_article, sender_name: 'Customer', ticket: ticket, created_at: start_time + 1.minute)
  100. create(:ticket_article, sender_name: 'Agent', ticket: ticket, created_at: start_time + 2.minutes)
  101. create(:ticket_article, sender_name: 'Customer', ticket: ticket, created_at: start_time + 10.minutes)
  102. create(:ticket_article, sender_name: 'Agent', ticket: ticket, created_at: start_time + 15.minutes)
  103. end
  104. it 'returns average of elapsed times' do
  105. expect(described_class.calculate_average(ticket.id, start_time)).to eq(3.minutes)
  106. end
  107. end
  108. context 'with all above cases combined' do
  109. before do
  110. # empty ticket
  111. create(:ticket)
  112. # old messages
  113. create(:ticket_article, sender_name: 'Customer', ticket: ticket, created_at: 1.day.ago)
  114. create(:ticket_article, sender_name: 'Agent', ticket: ticket, created_at: 1.day.ago)
  115. # first exchange, with internal notes
  116. create(:ticket_article, sender_name: 'Customer', ticket: ticket, created_at: start_time + 1.minute)
  117. create(:ticket_article, sender_name: 'Agent', ticket: ticket, created_at: start_time + 90.seconds, internal: true)
  118. create(:ticket_article, sender_name: 'Agent', ticket: ticket, created_at: start_time + 2.minutes)
  119. # second exchange
  120. create(:ticket_article, sender_name: 'Customer', ticket: ticket, created_at: start_time + 10.minutes)
  121. create(:ticket_article, sender_name: 'Agent', ticket: ticket, created_at: start_time + 15.minutes)
  122. end
  123. it 'ignores all edge cases and returns only specified average response time' do
  124. expect(described_class.calculate_average(ticket.id, start_time)).to eq(3.minutes)
  125. end
  126. end
  127. end
  128. end