2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
2015-09-06 22:42:11 +00:00
|
|
|
class Stats
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
generate stats for user
|
|
|
|
|
|
|
|
Stats.generate
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = true # if generation was successfully
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.generate
|
|
|
|
|
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 = {}
|
2017-10-01 12:25:52 +00:00
|
|
|
users.each do |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
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2015-09-08 06:55:09 +00:00
|
|
|
agent_count += 1
|
2015-09-06 22:42:11 +00:00
|
|
|
data = {}
|
2019-01-24 08:58:58 +00:00
|
|
|
|
|
|
|
backends = Setting.where(area: 'Dashboard::Stats')
|
|
|
|
if backends.blank?
|
|
|
|
raise "No settings with area 'Dashboard::Stats' defined"
|
|
|
|
end
|
|
|
|
|
|
|
|
backends.each do |stats_item|
|
|
|
|
# additional permission check
|
|
|
|
next if stats_item.preferences[:permission] && !user.permissions?(stats_item.preferences[:permission])
|
|
|
|
|
|
|
|
backend = stats_item.state_current[:value]
|
|
|
|
if !backend
|
2020-09-30 09:07:01 +00:00
|
|
|
raise "Dashboard::Stats backend #{stats_item.name} is not defined"
|
2019-01-24 08:58:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
backend = backend.constantize
|
|
|
|
|
2015-09-08 10:59:46 +00:00
|
|
|
data[backend] = backend.generate(user)
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2020-10-06 13:13:50 +00:00
|
|
|
user_result[user] = data
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2015-09-08 06:55:09 +00:00
|
|
|
|
|
|
|
# calculate average
|
|
|
|
backend_average_sum = {}
|
2017-11-23 08:09:44 +00:00
|
|
|
user_result.each_value do |data|
|
2017-10-01 12:25:52 +00:00
|
|
|
data.each do |backend_model, backend_result|
|
2015-09-08 07:57:14 +00:00
|
|
|
next if !backend_result.key?(:used_for_average)
|
2018-10-09 06:17:41 +00:00
|
|
|
|
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]
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
|
|
|
end
|
2015-09-08 06:55:09 +00:00
|
|
|
|
2015-09-08 10:59:46 +00:00
|
|
|
# generate average param and icon state
|
2017-10-01 12:25:52 +00:00
|
|
|
backend_average_sum.each do |backend_model_average, result|
|
2021-07-16 13:38:01 +00:00
|
|
|
average = (result.to_f / agent_count).round(1)
|
2020-10-06 13:13:50 +00:00
|
|
|
user_result.each do |user, 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)
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2015-09-08 10:59:46 +00:00
|
|
|
data[backend_model_average][:average_per_agent] = average
|
|
|
|
|
|
|
|
# generate icon state
|
2020-10-06 13:13:50 +00:00
|
|
|
backend_model_average.to_s.constantize.average_state(data[backend_model_average], user.id)
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
|
|
|
end
|
2015-09-08 06:55:09 +00:00
|
|
|
|
2020-10-06 13:13:50 +00:00
|
|
|
user_result.each do |user, data|
|
2015-09-08 10:59:46 +00:00
|
|
|
data_for_user = {}
|
2017-10-01 12:25:52 +00:00
|
|
|
data.each do |backend, result|
|
2015-09-08 10:59:46 +00:00
|
|
|
data_for_user[backend.to_app_model] = result
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2015-09-08 11:42:03 +00:00
|
|
|
state_store = StatsStore.sync(
|
2020-10-06 13:13:50 +00:00
|
|
|
stats_storable: user,
|
|
|
|
key: 'dashboard',
|
|
|
|
data: data_for_user,
|
2015-09-06 22:42:11 +00:00
|
|
|
)
|
2015-09-08 11:42:03 +00:00
|
|
|
|
|
|
|
message = {
|
|
|
|
event: 'resetCollection',
|
2018-12-19 17:31:51 +00:00
|
|
|
data: {
|
2015-09-08 11:42:03 +00:00
|
|
|
state_store.class.to_app_model => [state_store],
|
|
|
|
},
|
|
|
|
}
|
2020-10-06 13:13:50 +00:00
|
|
|
Sessions.send_to(user.id, message)
|
2015-09-08 11:42:03 +00:00
|
|
|
event = {
|
|
|
|
event: 'dashboard_stats_rebuild',
|
|
|
|
}
|
2020-10-06 13:13:50 +00:00
|
|
|
Sessions.send_to(user.id, event)
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2015-09-08 06:55:09 +00:00
|
|
|
|
2015-09-06 22:42:11 +00:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|