stats.rb 2.4 KB

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