ticket_response_time.rb 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class Stats::TicketResponseTime
  3. def self.log(object, o_id)
  4. return if object != 'Ticket'
  5. ticket = Ticket.lookup(id: o_id)
  6. article_created_by_id = 3
  7. # check if response was sent by owner
  8. return if ticket.owner_id != 1 && ticket.owner_id != article_created_by_id
  9. # return if customer send at least
  10. return if ticket.last_contact_customer_at > ticket.last_contact_agent_at
  11. # TODO: only business hours
  12. response_time_taken = ticket.last_contact_agent_at - ticket.last_contact_customer_at
  13. (response_time_taken / 60).round
  14. end
  15. def self.generate(user)
  16. items = StatsStore.where('created_at > ? AND created_at < ?', Time.zone.now - 7.days, Time.zone.now).where(key: 'ticket:response_time')
  17. count_total = items.count
  18. total = 0
  19. count_own = 0
  20. own = 0
  21. items.each do |_item|
  22. ticket = Ticket.lookup(id: data[:ticket_id])
  23. if ticket.owner_id == user.id
  24. count_own += 1
  25. own += data[:time]
  26. end
  27. total += data[:time]
  28. end
  29. if total.nonzero?
  30. own = (own / count_own).round
  31. end
  32. {
  33. used_for_average: 0,
  34. average_per_agent: '-',
  35. own: own,
  36. total: total,
  37. }
  38. end
  39. def self.average_state(result, _user_id)
  40. return result if !result.key?(:used_for_average)
  41. if result[:total] < 1
  42. result[:state] = 'supergood'
  43. return result
  44. end
  45. in_percent = ( result[:used_for_average].to_f / (result[:total].to_f / 100) ).round(1)
  46. result[:state] = if in_percent >= 90
  47. 'supergood'
  48. elsif in_percent >= 65
  49. 'good'
  50. elsif in_percent >= 40
  51. 'ok'
  52. elsif in_percent >= 20
  53. 'bad'
  54. else
  55. 'superbad'
  56. end
  57. result
  58. end
  59. end