ticket_channel_distribution.rb 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://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. # get channels
  9. channels = [
  10. {
  11. sender: 'email',
  12. icon: 'email',
  13. },
  14. {
  15. sender: 'phone',
  16. icon: 'phone',
  17. },
  18. {
  19. sender: 'twitter',
  20. icon: 'twitter',
  21. },
  22. {
  23. sender: 'facebook',
  24. icon: 'facebook',
  25. },
  26. ]
  27. # calcualte
  28. result = {}
  29. total_in = 0
  30. total_out = 0
  31. channels.each do |channel|
  32. result[channel[:sender].to_sym] = {
  33. icon: channel[:icon]
  34. }
  35. type_ids = []
  36. Ticket::Article::Type.all.each do |type|
  37. next if type.name !~ /^#{channel[:sender]}/i
  38. type_ids.push type.id
  39. end
  40. sender = Ticket::Article::Sender.lookup( name: 'Customer' )
  41. count = Ticket.where(group_id: group_ids).joins(:articles).where(
  42. ticket_articles: { sender_id: sender, type_id: type_ids }
  43. ).where(
  44. 'ticket_articles.created_at > ?', Time.zone.now - time_range
  45. ).count
  46. result[channel[:sender].to_sym][:inbound] = count
  47. total_in += count
  48. sender = Ticket::Article::Sender.lookup( name: 'Agent' )
  49. count = Ticket.where(group_id: group_ids).joins(:articles).where(
  50. ticket_articles: { sender_id: sender, type_id: type_ids }
  51. ).where(
  52. 'ticket_articles.created_at > ?', Time.zone.now - time_range
  53. ).count
  54. result[channel[:sender].to_sym][:outbound] = count
  55. total_out += count
  56. end
  57. # append in percent
  58. channels.each do |channel|
  59. count = result[channel[:sender].to_sym][:inbound]
  60. #puts "#{channel.inspect}:in/#{result.inspect}:#{count}"
  61. in_process_precent = if count.zero?
  62. 0
  63. else
  64. (count * 1000) / ((total_in * 1000) / 100)
  65. end
  66. result[channel[:sender].to_sym][:inbound_in_percent] = in_process_precent
  67. count = result[channel[:sender].to_sym][:outbound]
  68. out_process_precent = if count.zero?
  69. 0
  70. else
  71. (count * 1000) / ((total_out * 1000) / 100)
  72. end
  73. result[channel[:sender].to_sym][:outbound_in_percent] = out_process_precent
  74. end
  75. { channels: result }
  76. end
  77. def self.average_state(result, _user_id)
  78. result
  79. end
  80. end