2015-09-06 22:42:11 +00:00
|
|
|
# Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
|
|
|
require 'stats_store'
|
|
|
|
|
|
|
|
class Stats
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
generate stats for user
|
|
|
|
|
|
|
|
Stats.generate
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = true # if generation was successfully
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.generate
|
|
|
|
|
|
|
|
backends = [
|
|
|
|
Stats::TicketChannelDistribution,
|
|
|
|
Stats::TicketInProcess,
|
|
|
|
Stats::TicketLoadMeasure,
|
|
|
|
Stats::TicketEscalation,
|
|
|
|
Stats::TicketReopen,
|
|
|
|
]
|
|
|
|
|
2015-09-08 06:55:09 +00:00
|
|
|
# generate stats per agent
|
2016-08-12 16:39:09 +00:00
|
|
|
users = User.with_permissions('ticket.agent')
|
2015-09-08 06:55:09 +00:00
|
|
|
agent_count = 0
|
|
|
|
user_result = {}
|
2016-06-30 20:04:48 +00:00
|
|
|
users.each { |user|
|
2015-09-06 23:52:54 +00:00
|
|
|
next if user.id == 1
|
2015-09-08 06:55:09 +00:00
|
|
|
next if !user.active
|
|
|
|
agent_count += 1
|
2015-09-06 22:42:11 +00:00
|
|
|
data = {}
|
2016-06-30 20:04:48 +00:00
|
|
|
backends.each { |backend|
|
2015-09-08 10:59:46 +00:00
|
|
|
data[backend] = backend.generate(user)
|
2015-09-06 22:42:11 +00:00
|
|
|
}
|
2015-09-08 06:55:09 +00:00
|
|
|
user_result[user.id] = data
|
|
|
|
}
|
|
|
|
|
|
|
|
# calculate average
|
|
|
|
backend_average_sum = {}
|
2016-06-30 20:04:48 +00:00
|
|
|
user_result.each { |_user_id, data|
|
|
|
|
data.each { |backend_model, backend_result|
|
2015-09-08 07:57:14 +00:00
|
|
|
next if !backend_result.key?(:used_for_average)
|
2015-09-08 06:55:09 +00:00
|
|
|
if !backend_average_sum[backend_model]
|
|
|
|
backend_average_sum[backend_model] = 0
|
|
|
|
end
|
|
|
|
backend_average_sum[backend_model] += backend_result[:used_for_average]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-08 10:59:46 +00:00
|
|
|
# generate average param and icon state
|
2016-06-30 20:04:48 +00:00
|
|
|
backend_average_sum.each { |backend_model_average, result|
|
2015-09-08 06:55:09 +00:00
|
|
|
average = ( result.to_f / agent_count.to_f ).round(1)
|
2016-06-30 20:04:48 +00:00
|
|
|
user_result.each { |user_id, data|
|
2015-09-08 10:59:46 +00:00
|
|
|
next if !data[backend_model_average]
|
|
|
|
next if !data[backend_model_average].key?(:used_for_average)
|
|
|
|
data[backend_model_average][:average_per_agent] = average
|
|
|
|
|
|
|
|
# generate icon state
|
|
|
|
backend_model_average.to_s.constantize.average_state(data[backend_model_average], user_id)
|
2015-09-08 06:55:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-30 20:04:48 +00:00
|
|
|
user_result.each { |user_id, data|
|
2015-09-08 10:59:46 +00:00
|
|
|
data_for_user = {}
|
2016-06-30 20:04:48 +00:00
|
|
|
data.each { |backend, result|
|
2015-09-08 10:59:46 +00:00
|
|
|
data_for_user[backend.to_app_model] = result
|
|
|
|
}
|
2015-09-08 11:42:03 +00:00
|
|
|
state_store = StatsStore.sync(
|
2015-09-06 22:42:11 +00:00
|
|
|
object: 'User',
|
2015-09-08 06:55:09 +00:00
|
|
|
o_id: user_id,
|
2015-09-06 22:42:11 +00:00
|
|
|
key: 'dashboard',
|
2015-09-08 10:59:46 +00:00
|
|
|
data: data_for_user,
|
2015-09-06 22:42:11 +00:00
|
|
|
)
|
2015-09-08 11:42:03 +00:00
|
|
|
|
|
|
|
message = {
|
|
|
|
event: 'resetCollection',
|
|
|
|
data: {
|
|
|
|
state_store.class.to_app_model => [state_store],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
Sessions.send_to(user_id, message)
|
|
|
|
event = {
|
|
|
|
event: 'dashboard_stats_rebuild',
|
|
|
|
}
|
|
|
|
Sessions.send_to(user_id, event)
|
2015-09-06 22:42:11 +00:00
|
|
|
}
|
2015-09-08 06:55:09 +00:00
|
|
|
|
2015-09-06 22:42:11 +00:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|