stats.rb 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class Stats
  3. =begin
  4. generate stats for user
  5. Stats.generate
  6. returns
  7. result = true # if generation was successfully
  8. =end
  9. def self.generate
  10. # generate stats per agent
  11. users = User.with_permissions('ticket.agent')
  12. agent_count = 0
  13. user_result = {}
  14. users.each do |user|
  15. next if user.id == 1
  16. next if !user.active
  17. agent_count += 1
  18. data = {}
  19. backends = Setting.where(area: 'Dashboard::Stats')
  20. if backends.blank?
  21. raise "No settings with area 'Dashboard::Stats' defined"
  22. end
  23. backends.each do |stats_item|
  24. # additional permission check
  25. next if stats_item.preferences[:permission] && !user.permissions?(stats_item.preferences[:permission])
  26. backend = stats_item.state_current[:value]
  27. if !backend
  28. raise 'Dashboard::Stats backend ' + stats_item.name + ' is not defined'
  29. end
  30. require_dependency backend.to_filename
  31. backend = backend.constantize
  32. data[backend] = backend.generate(user)
  33. end
  34. user_result[user.id] = data
  35. end
  36. # calculate average
  37. backend_average_sum = {}
  38. user_result.each_value do |data|
  39. data.each do |backend_model, backend_result|
  40. next if !backend_result.key?(:used_for_average)
  41. if !backend_average_sum[backend_model]
  42. backend_average_sum[backend_model] = 0
  43. end
  44. backend_average_sum[backend_model] += backend_result[:used_for_average]
  45. end
  46. end
  47. # generate average param and icon state
  48. backend_average_sum.each do |backend_model_average, result|
  49. average = ( result.to_f / agent_count.to_f ).round(1)
  50. user_result.each do |user_id, data|
  51. next if !data[backend_model_average]
  52. next if !data[backend_model_average].key?(:used_for_average)
  53. data[backend_model_average][:average_per_agent] = average
  54. # generate icon state
  55. backend_model_average.to_s.constantize.average_state(data[backend_model_average], user_id)
  56. end
  57. end
  58. user_result.each do |user_id, data|
  59. data_for_user = {}
  60. data.each do |backend, result|
  61. data_for_user[backend.to_app_model] = result
  62. end
  63. state_store = StatsStore.sync(
  64. object: 'User',
  65. o_id: user_id,
  66. key: 'dashboard',
  67. data: data_for_user,
  68. )
  69. message = {
  70. event: 'resetCollection',
  71. data: {
  72. state_store.class.to_app_model => [state_store],
  73. },
  74. }
  75. Sessions.send_to(user_id, message)
  76. event = {
  77. event: 'dashboard_stats_rebuild',
  78. }
  79. Sessions.send_to(user_id, event)
  80. end
  81. true
  82. end
  83. end