ticket_reopen.rb 2.8 KB

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