ticket_channel_distribution.rb 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Stats::TicketChannelDistribution
  3. def self.generate(user)
  4. # which time range?
  5. time_range = 7.days
  6. # get users groups
  7. group_ids = user.group_ids_access('full')
  8. # set default channels
  9. channels = [
  10. {
  11. sender: 'email',
  12. icon: 'email',
  13. },
  14. {
  15. sender: 'phone',
  16. icon: 'phone',
  17. },
  18. ]
  19. if Setting.get('customer_ticket_create')
  20. channels.push(
  21. {
  22. sender: 'web',
  23. icon: 'web',
  24. }
  25. )
  26. end
  27. if Setting.get('chat')
  28. channels.push(
  29. {
  30. sender: 'chat',
  31. icon: 'chat',
  32. }
  33. )
  34. end
  35. if Channel.exists?(area: 'Sms::Account')
  36. channels.push(
  37. {
  38. sender: 'sms',
  39. icon: 'sms',
  40. }
  41. )
  42. end
  43. if Channel.exists?(area: 'Twitter::Account')
  44. channels.push(
  45. {
  46. sender: 'twitter',
  47. icon: 'twitter',
  48. }
  49. )
  50. end
  51. if Channel.exists?(area: 'Facebook::Account')
  52. channels.push(
  53. {
  54. sender: 'facebook',
  55. icon: 'facebook',
  56. }
  57. )
  58. end
  59. if Channel.exists?(area: 'Telegram::Account')
  60. channels.push(
  61. {
  62. sender: 'telegram',
  63. icon: 'telegram',
  64. }
  65. )
  66. end
  67. # calculate
  68. result = {}
  69. total_in = 0
  70. total_out = 0
  71. channels.each do |channel|
  72. result[channel[:sender].to_sym] = {
  73. icon: channel[:icon]
  74. }
  75. type_ids = []
  76. Ticket::Article::Type.all.each do |type|
  77. next if !type.name.match?(%r{^#{channel[:sender]}}i)
  78. type_ids.push type.id
  79. end
  80. sender = Ticket::Article::Sender.lookup(name: 'Customer')
  81. count = Ticket.where(group_id: group_ids).joins(:articles).where(
  82. ticket_articles: { sender_id: sender, type_id: type_ids }
  83. ).where(
  84. 'ticket_articles.created_at > ?', Time.zone.now - time_range
  85. ).count
  86. result[channel[:sender].to_sym][:inbound] = count
  87. total_in += count
  88. sender = Ticket::Article::Sender.lookup(name: 'Agent')
  89. count = Ticket.where(group_id: group_ids).joins(:articles).where(
  90. ticket_articles: { sender_id: sender, type_id: type_ids }
  91. ).where(
  92. 'ticket_articles.created_at > ?', Time.zone.now - time_range
  93. ).count
  94. result[channel[:sender].to_sym][:outbound] = count
  95. total_out += count
  96. end
  97. # append in percent
  98. channels.each do |channel| # rubocop:disable Style/CombinableLoops
  99. count = result[channel[:sender].to_sym][:inbound]
  100. # puts "#{channel.inspect}:in/#{result.inspect}:#{count}"
  101. in_process_precent = if count.zero?
  102. 0
  103. else
  104. (count * 1000) / ((total_in * 1000) / 100)
  105. end
  106. result[channel[:sender].to_sym][:inbound_in_percent] = in_process_precent
  107. count = result[channel[:sender].to_sym][:outbound]
  108. out_process_precent = if count.zero?
  109. 0
  110. else
  111. (count * 1000) / ((total_out * 1000) / 100)
  112. end
  113. result[channel[:sender].to_sym][:outbound_in_percent] = out_process_precent
  114. end
  115. { channels: result }
  116. end
  117. def self.average_state(result, _user_id)
  118. result
  119. end
  120. end