ticket_reopen.rb 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class Stats::TicketReopen
  3. def self.generate(user)
  4. # get my closed tickets
  5. total = Ticket.select('id').where(
  6. 'owner_id = ? AND close_at > ?',
  7. user.id, Time.zone.now - 7.days
  8. ).count
  9. # get count of reopens
  10. count = StatsStore.count_by_search(
  11. object: 'User',
  12. o_id: user.id,
  13. key: 'ticket:reopen',
  14. start: Time.zone.now - 7.days,
  15. end: Time.zone.now,
  16. )
  17. if count > total
  18. total = count
  19. end
  20. reopen_in_precent = 0
  21. if total.nonzero?
  22. reopen_in_precent = ( count.to_f / (total.to_f / 100) ).round(1)
  23. end
  24. {
  25. used_for_average: reopen_in_precent,
  26. percent: reopen_in_precent,
  27. average_per_agent: '-',
  28. state: 'good',
  29. count: count,
  30. total: total,
  31. }
  32. end
  33. def self.average_state(result, _user_id)
  34. return result if !result.key?(:used_for_average)
  35. if result[:total] < 1 || result[:average_per_agent] == 0.0
  36. result[:state] = 'supergood'
  37. return result
  38. end
  39. #in_percent = ( result[:used_for_average].to_f / (result[:average_per_agent].to_f / 100) ).round(1)
  40. #result[:average_per_agent_in_percent] = in_percent
  41. in_percent = ( result[:count].to_f / (result[:total].to_f / 100) ).round(1)
  42. result[:state] = if in_percent >= 90
  43. 'superbad'
  44. elsif in_percent >= 65
  45. 'bad'
  46. elsif in_percent >= 40
  47. 'ok'
  48. elsif in_percent >= 20
  49. 'good'
  50. else
  51. 'supergood'
  52. end
  53. result
  54. end
  55. def self.log(object, o_id, changes, updated_by_id)
  56. return if object != 'Ticket'
  57. ticket = Ticket.lookup(id: o_id)
  58. return if !ticket
  59. # check if close_at is already set / if not, ticket is not reopened
  60. return if !ticket.close_at
  61. # only if state id has changed
  62. return if !changes['state_id']
  63. # only if ticket is not created in closed state
  64. return if !changes['state_id'][0]
  65. # only if current owner is not 1
  66. return if ticket.owner_id == 1
  67. state_before = Ticket::State.lookup(id: changes['state_id'][0])
  68. state_type_before = Ticket::StateType.lookup(id: state_before.state_type_id)
  69. return if state_type_before.name != 'closed'
  70. state_now = Ticket::State.lookup(id: changes['state_id'][1])
  71. state_type_now = Ticket::StateType.lookup(id: state_now.state_type_id)
  72. return if state_type_now.name == 'closed'
  73. StatsStore.add(
  74. object: 'User',
  75. o_id: ticket.owner_id,
  76. key: 'ticket:reopen',
  77. data: { ticket_id: ticket.id },
  78. created_at: Time.zone.now,
  79. created_by_id: updated_by_id,
  80. updated_by_id: updated_by_id,
  81. )
  82. end
  83. end