2022-01-01 13:38:12 +00:00
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
2017-04-18 08:06:22 +00:00
class Stats :: TicketWaitingTime
def self . generate ( user )
# get users groups
2017-06-16 20:43:09 +00:00
group_ids = user . group_ids_access ( 'full' )
2017-04-18 08:06:22 +00:00
2018-05-08 12:18:15 +00:00
own_waiting = [ ]
all_waiting = [ ]
2018-05-08 12:37:36 +00:00
Ticket . where ( 'group_id IN (?) AND updated_at > ?' , group_ids . sort , Time . zone . today ) . limit ( 20_000 ) . pluck ( :id , :owner_id ) . each do | ticket |
2018-05-08 12:18:15 +00:00
all_waiting . push ticket [ 0 ]
if ticket [ 1 ] == user . id
own_waiting . push ticket [ 0 ]
end
end
2017-04-18 08:06:22 +00:00
handling_time = calculate_average ( own_waiting , Time . zone . today )
if handling_time . positive?
handling_time = ( handling_time / 60 ) . round
end
average_per_agent = calculate_average ( all_waiting , Time . zone . today )
if average_per_agent . positive?
average_per_agent = ( average_per_agent / 60 ) . round
end
percent = 0
state = if handling_time < = 60
percent = handling_time . to_f / 60
'supergood'
elsif handling_time < = 60 * 4
percent = ( handling_time . to_f - 60 ) / ( 60 * 3 )
'good'
elsif handling_time < = 60 * 8
2021-09-14 07:46:08 +00:00
percent = ( handling_time . to_f - ( 60 * 4 ) ) / ( 60 * 4 )
2017-04-18 08:06:22 +00:00
'ok'
else
percent = 1 . 00
'bad'
end
{
2018-12-19 17:31:51 +00:00
handling_time : handling_time ,
2017-04-18 08:06:22 +00:00
average_per_agent : average_per_agent ,
2018-12-19 17:31:51 +00:00
state : state ,
percent : percent ,
2017-04-18 08:06:22 +00:00
}
end
def self . average_state ( result , _user_id )
result
end
2018-05-08 12:18:15 +00:00
def self . calculate_average ( ticket_ids , start_time )
2017-04-20 10:04:20 +00:00
average_time = 0
count_articles = 0
2018-05-08 12:18:15 +00:00
last_ticket_id = nil
count_time = nil
2017-04-18 08:06:22 +00:00
2018-05-08 12:18:15 +00:00
Ticket :: Article . joins ( :type ) . joins ( :sender ) . where ( 'ticket_articles.ticket_id IN (?) AND ticket_articles.created_at > ? AND ticket_articles.internal = ? AND ticket_article_types.communication = ?' , ticket_ids , start_time , false , true ) . order ( :ticket_id , :created_at ) . pluck ( :created_at , :sender_id , :ticket_id , :id ) . each do | article |
if last_ticket_id != article [ 2 ]
last_ticket_id = article [ 2 ]
count_time = 0
end
sender = Ticket :: Article :: Sender . lookup ( id : article [ 1 ] )
if sender . name == 'Customer'
count_time = article [ 0 ] . to_i
elsif count_time . positive?
average_time += article [ 0 ] . to_i - count_time
count_articles += 1
count_time = 0
2017-10-01 12:25:52 +00:00
end
end
2017-04-18 08:06:22 +00:00
2017-04-20 10:04:20 +00:00
if count_articles . positive?
2021-07-16 14:10:31 +00:00
average_time /= count_articles
2017-04-20 10:04:20 +00:00
end
2017-04-18 08:06:22 +00:00
average_time
end
end