2022-01-01 13:38:12 +00:00
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
2015-09-06 22:42:11 +00:00
class Stats :: TicketInProcess
def self . generate ( user )
2015-09-09 06:47:58 +00:00
# get own tickets which are "workable"
2017-03-27 14:06:18 +00:00
open_state_ids = Ticket :: State . by_category ( :work_on ) . pluck ( :id )
pending_state_ids = Ticket :: State . by_category ( :pending_reminder ) . pluck ( :id )
2015-09-06 23:52:54 +00:00
own_ticket_ids = Ticket . select ( 'id' ) . where (
'owner_id = ? AND (state_id IN (?) OR (state_id IN (?) AND pending_time < ?))' ,
user . id , open_state_ids , pending_state_ids , Time . zone . now
2017-03-27 14:06:18 +00:00
) . limit ( 1000 ) . pluck ( :id )
2015-09-06 22:42:11 +00:00
2015-09-08 07:45:56 +00:00
# get all tickets where I worked on today (owner & closed today)
2017-03-27 14:06:18 +00:00
closed_state_ids = Ticket :: State . by_category ( :closed ) . pluck ( :id )
2015-09-08 07:45:56 +00:00
closed_ticket_ids = Ticket . select ( 'id' ) . where (
2016-09-14 07:15:30 +00:00
'owner_id = ? AND state_id IN (?) AND close_at > ?' ,
2022-01-03 09:47:32 +00:00
user . id , closed_state_ids , 1 . day . ago
2017-03-27 14:06:18 +00:00
) . limit ( 100 ) . pluck ( :id )
2015-09-08 07:45:56 +00:00
# get all tickets which I changed to pending action
2017-03-27 14:06:18 +00:00
pending_action_state_ids = Ticket :: State . by_category ( :pending_action ) . pluck ( :id )
2015-09-08 07:45:56 +00:00
pending_action_ticket_ids = Ticket . select ( 'id' ) . where (
'owner_id = ? AND state_id IN (?) AND updated_at > ?' ,
2022-01-03 09:47:32 +00:00
user . id , pending_action_state_ids , 1 . day . ago
2017-03-27 14:06:18 +00:00
) . limit ( 100 ) . pluck ( :id )
2015-09-08 07:45:56 +00:00
all_ticket_ids = own_ticket_ids . concat ( closed_ticket_ids ) . concat ( pending_action_ticket_ids ) . uniq
2015-09-06 23:52:54 +00:00
# get count where user worked on
2015-09-09 06:47:58 +00:00
history_object = History :: Object . lookup ( name : 'Ticket' )
2015-09-06 22:42:11 +00:00
count = History . select ( 'DISTINCT(o_id)' ) . where (
2022-01-03 09:47:32 +00:00
'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
2015-09-06 22:42:11 +00:00
) . count
2015-09-08 07:45:56 +00:00
total = all_ticket_ids . count
2015-09-06 22:42:11 +00:00
in_process_precent = 0
state = 'supergood'
average_in_percent = '-'
2016-07-26 22:02:28 +00:00
if total . nonzero?
2021-07-16 13:38:01 +00:00
in_process_precent = ( count . to_f / ( total . to_f / 100 ) ) . round ( 1 )
2015-09-06 22:42:11 +00:00
end
{
2018-12-19 17:31:51 +00:00
used_for_average : in_process_precent ,
2015-09-08 06:55:09 +00:00
average_per_agent : average_in_percent ,
2018-12-19 17:31:51 +00:00
state : state ,
in_process : count ,
percent : in_process_precent ,
total : total ,
2015-09-06 22:42:11 +00:00
}
end
2015-09-08 10:59:46 +00:00
def self . average_state ( result , _user_id )
return result if ! result . key? ( :used_for_average )
if result [ :total ] < 1
result [ :state ] = 'supergood'
return result
end
2021-07-16 13:38:01 +00:00
in_percent = ( result [ :used_for_average ] . to_f / ( result [ :average_per_agent ] . to_f / 100 ) ) . round ( 1 )
2016-01-15 17:22:57 +00:00
result [ :state ] = if in_percent > = 90
'supergood'
elsif in_percent > = 65
'good'
elsif in_percent > = 40
'ok'
elsif in_percent > = 20
'bad'
else
'superbad'
end
2015-09-08 10:59:46 +00:00
result
end
2015-09-06 22:42:11 +00:00
end