stats.rb 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. backends = [
  11. Stats::TicketChannelDistribution,
  12. Stats::TicketInProcess,
  13. Stats::TicketLoadMeasure,
  14. Stats::TicketEscalation,
  15. Stats::TicketReopen,
  16. Stats::TicketWaitingTime,
  17. ]
  18. # generate stats per agent
  19. users = User.with_permissions('ticket.agent')
  20. agent_count = 0
  21. user_result = {}
  22. users.each do |user|
  23. next if user.id == 1
  24. next if !user.active
  25. agent_count += 1
  26. data = {}
  27. backends.each do |backend|
  28. data[backend] = backend.generate(user)
  29. end
  30. user_result[user.id] = data
  31. end
  32. # calculate average
  33. backend_average_sum = {}
  34. user_result.each_value do |data|
  35. data.each do |backend_model, backend_result|
  36. next if !backend_result.key?(:used_for_average)
  37. if !backend_average_sum[backend_model]
  38. backend_average_sum[backend_model] = 0
  39. end
  40. backend_average_sum[backend_model] += backend_result[:used_for_average]
  41. end
  42. end
  43. # generate average param and icon state
  44. backend_average_sum.each do |backend_model_average, result|
  45. average = ( result.to_f / agent_count.to_f ).round(1)
  46. user_result.each do |user_id, data|
  47. next if !data[backend_model_average]
  48. next if !data[backend_model_average].key?(:used_for_average)
  49. data[backend_model_average][:average_per_agent] = average
  50. # generate icon state
  51. backend_model_average.to_s.constantize.average_state(data[backend_model_average], user_id)
  52. end
  53. end
  54. user_result.each do |user_id, data|
  55. data_for_user = {}
  56. data.each do |backend, result|
  57. data_for_user[backend.to_app_model] = result
  58. end
  59. state_store = StatsStore.sync(
  60. object: 'User',
  61. o_id: user_id,
  62. key: 'dashboard',
  63. data: data_for_user,
  64. )
  65. message = {
  66. event: 'resetCollection',
  67. data: {
  68. state_store.class.to_app_model => [state_store],
  69. },
  70. }
  71. Sessions.send_to(user_id, message)
  72. event = {
  73. event: 'dashboard_stats_rebuild',
  74. }
  75. Sessions.send_to(user_id, event)
  76. end
  77. true
  78. end
  79. end