stats.rb 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. ]
  18. # generate stats per agent
  19. users = User.with_permissions('ticket.agent')
  20. agent_count = 0
  21. user_result = {}
  22. users.each { |user|
  23. next if user.id == 1
  24. next if !user.active
  25. agent_count += 1
  26. data = {}
  27. backends.each { |backend|
  28. data[backend] = backend.generate(user)
  29. }
  30. user_result[user.id] = data
  31. }
  32. # calculate average
  33. backend_average_sum = {}
  34. user_result.each { |_user_id, data|
  35. data.each { |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. }
  42. }
  43. # generate average param and icon state
  44. backend_average_sum.each { |backend_model_average, result|
  45. average = ( result.to_f / agent_count.to_f ).round(1)
  46. user_result.each { |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. }
  53. }
  54. user_result.each { |user_id, data|
  55. data_for_user = {}
  56. data.each { |backend, result|
  57. data_for_user[backend.to_app_model] = result
  58. }
  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. }
  77. true
  78. end
  79. end