ticket_in_process.rb 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class Stats::TicketInProcess
  3. def self.generate(user)
  4. # get own tickets which are "workable"
  5. open_state_ids = Ticket::State.by_category(:work_on).pluck(:id)
  6. pending_state_ids = Ticket::State.by_category(:pending_reminder).pluck(:id)
  7. own_ticket_ids = Ticket.select('id').where(
  8. 'owner_id = ? AND (state_id IN (?) OR (state_id IN (?) AND pending_time < ?))',
  9. user.id, open_state_ids, pending_state_ids, Time.zone.now
  10. ).limit(1000).pluck(:id)
  11. # get all tickets where I worked on today (owner & closed today)
  12. closed_state_ids = Ticket::State.by_category(:closed).pluck(:id)
  13. closed_ticket_ids = Ticket.select('id').where(
  14. 'owner_id = ? AND state_id IN (?) AND close_at > ?',
  15. user.id, closed_state_ids, 1.day.ago
  16. ).limit(100).pluck(:id)
  17. # get all tickets which I changed to pending action
  18. pending_action_state_ids = Ticket::State.by_category(:pending_action).pluck(:id)
  19. pending_action_ticket_ids = Ticket.select('id').where(
  20. 'owner_id = ? AND state_id IN (?) AND updated_at > ?',
  21. user.id, pending_action_state_ids, 1.day.ago
  22. ).limit(100).pluck(:id)
  23. all_ticket_ids = own_ticket_ids.concat(closed_ticket_ids).concat(pending_action_ticket_ids).uniq
  24. # get count where user worked on
  25. history_object = History::Object.lookup(name: 'Ticket')
  26. count = History.select('DISTINCT(o_id)').where(
  27. 'histories.created_at >= ? AND histories.history_object_id = ? AND histories.created_by_id = ? AND histories.o_id IN (?)', 1.day.ago, history_object.id, user.id, all_ticket_ids
  28. ).count
  29. total = all_ticket_ids.count
  30. in_process_precent = 0
  31. state = 'supergood'
  32. average_in_percent = '-'
  33. if total.nonzero?
  34. in_process_precent = (count.to_f / (total.to_f / 100)).round(1)
  35. end
  36. {
  37. used_for_average: in_process_precent,
  38. average_per_agent: average_in_percent,
  39. state: state,
  40. in_process: count,
  41. percent: in_process_precent,
  42. total: total,
  43. }
  44. end
  45. def self.average_state(result, _user_id)
  46. return result if !result.key?(:used_for_average)
  47. if result[:total] < 1
  48. result[:state] = 'supergood'
  49. return result
  50. end
  51. in_percent = (result[:used_for_average].to_f / (result[:average_per_agent].to_f / 100)).round(1)
  52. result[:state] = if in_percent >= 90
  53. 'supergood'
  54. elsif in_percent >= 65
  55. 'good'
  56. elsif in_percent >= 40
  57. 'ok'
  58. elsif in_percent >= 20
  59. 'bad'
  60. else
  61. 'superbad'
  62. end
  63. result
  64. end
  65. end