trabajo-afectivo/lib/stats/ticket_channel_distribution.rb

95 lines
2.6 KiB
Ruby
Raw Normal View History

2016-10-19 03:11:36 +00:00
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
2015-09-06 22:42:11 +00:00
class Stats::TicketChannelDistribution
def self.generate(user)
# which time range?
time_range = 7.days
# get users groups
group_ids = user.group_ids_access('full')
2015-09-06 22:42:11 +00:00
# get channels
channels = [
{
sender: 'email',
icon: 'email',
2015-09-06 22:42:11 +00:00
},
{
sender: 'phone',
icon: 'phone',
2015-09-06 22:42:11 +00:00
},
{
sender: 'twitter',
icon: 'twitter',
2015-09-06 22:42:11 +00:00
},
{
sender: 'facebook',
icon: 'facebook',
2015-09-06 22:42:11 +00:00
},
]
# calculate
2015-09-06 22:42:11 +00:00
result = {}
total_in = 0
total_out = 0
channels.each do |channel|
2015-09-06 22:42:11 +00:00
result[channel[:sender].to_sym] = {
icon: channel[:icon]
}
type_ids = []
Ticket::Article::Type.all.each do |type|
2015-09-06 22:42:11 +00:00
next if type.name !~ /^#{channel[:sender]}/i
2015-09-06 22:42:11 +00:00
type_ids.push type.id
end
2015-09-06 22:42:11 +00:00
sender = Ticket::Article::Sender.lookup( name: 'Customer' )
count = Ticket.where(group_id: group_ids).joins(:articles).where(
ticket_articles: { sender_id: sender, type_id: type_ids }
).where(
'ticket_articles.created_at > ?', Time.zone.now - time_range
).count
result[channel[:sender].to_sym][:inbound] = count
total_in += count
sender = Ticket::Article::Sender.lookup( name: 'Agent' )
count = Ticket.where(group_id: group_ids).joins(:articles).where(
ticket_articles: { sender_id: sender, type_id: type_ids }
).where(
'ticket_articles.created_at > ?', Time.zone.now - time_range
).count
result[channel[:sender].to_sym][:outbound] = count
total_out += count
end
2015-09-06 22:42:11 +00:00
# append in percent
channels.each do |channel|
2015-09-06 22:42:11 +00:00
count = result[channel[:sender].to_sym][:inbound]
#puts "#{channel.inspect}:in/#{result.inspect}:#{count}"
in_process_precent = if count.zero?
0
else
(count * 1000) / ((total_in * 1000) / 100)
end
2015-09-06 22:42:11 +00:00
result[channel[:sender].to_sym][:inbound_in_percent] = in_process_precent
count = result[channel[:sender].to_sym][:outbound]
out_process_precent = if count.zero?
0
else
(count * 1000) / ((total_out * 1000) / 100)
end
2015-09-06 22:42:11 +00:00
result[channel[:sender].to_sym][:outbound_in_percent] = out_process_precent
end
2015-09-06 22:42:11 +00:00
{ channels: result }
end
def self.average_state(result, _user_id)
result
end
2015-09-06 22:42:11 +00:00
end