ticket_response_time.rb 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. total = 0
  18. count_own = 0
  19. own = 0
  20. items.each do |_item|
  21. ticket = Ticket.lookup(id: data[:ticket_id])
  22. if ticket.owner_id == user.id
  23. count_own += 1
  24. own += data[:time]
  25. end
  26. total += data[:time]
  27. end
  28. if total.nonzero?
  29. own = (own / count_own).round
  30. end
  31. {
  32. used_for_average: 0,
  33. average_per_agent: '-',
  34. own: own,
  35. total: total,
  36. }
  37. end
  38. def self.average_state(result, _user_id)
  39. return result if !result.key?(:used_for_average)
  40. if result[:total] < 1
  41. result[:state] = 'supergood'
  42. return result
  43. end
  44. in_percent = ( result[:used_for_average].to_f / (result[:total].to_f / 100) ).round(1)
  45. result[:state] = if in_percent >= 90
  46. 'supergood'
  47. elsif in_percent >= 65
  48. 'good'
  49. elsif in_percent >= 40
  50. 'ok'
  51. elsif in_percent >= 20
  52. 'bad'
  53. else
  54. 'superbad'
  55. end
  56. result
  57. end
  58. end