ticket_first_solution.rb 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. class Report::TicketFirstSolution
  2. =begin
  3. result = Report::TicketFirstSolution.aggs(
  4. range_start: Time.zone.parse('2015-01-01T00:00:00Z'),
  5. range_end: Time.zone.parse('2015-12-31T23:59:59Z'),
  6. interval: 'month', # quarter, month, week, day, hour, minute, second
  7. selector: selector, # ticket selector to get only a collection of tickets
  8. )
  9. returns
  10. [4,5,1,5,0,51,5,56,7,4]
  11. =end
  12. def self.aggs(params_origin)
  13. params = params_origin.dup
  14. interval = params[:interval]
  15. if params[:interval] == 'week'
  16. interval = 'day'
  17. end
  18. result = []
  19. if params[:interval] == 'month'
  20. stop_interval = 12
  21. elsif params[:interval] == 'week'
  22. stop_interval = 7
  23. elsif params[:interval] == 'day'
  24. stop_interval = 31
  25. elsif params[:interval] == 'hour'
  26. stop_interval = 24
  27. elsif params[:interval] == 'minute'
  28. stop_interval = 60
  29. end
  30. (1..stop_interval).each do |_counter|
  31. if params[:interval] == 'month'
  32. params[:range_end] = params[:range_start].next_month
  33. elsif params[:interval] == 'week'
  34. params[:range_end] = params[:range_start].next_day
  35. elsif params[:interval] == 'day'
  36. params[:range_end] = params[:range_start].next_day
  37. elsif params[:interval] == 'hour'
  38. params[:range_end] = params[:range_start] + 1.hour
  39. elsif params[:interval] == 'minute'
  40. params[:range_end] = params[:range_start] + 1.minute
  41. end
  42. without_merged_tickets = {
  43. 'ticket_state.name' => {
  44. 'operator' => 'is not',
  45. 'value' => 'merged'
  46. }
  47. }
  48. params[:selector].merge!(without_merged_tickets)
  49. query, bind_params, tables = Ticket.selector2sql(params[:selector])
  50. ticket_list = Ticket.select('tickets.id, tickets.close_at, tickets.created_at').where(
  51. 'tickets.close_at IS NOT NULL AND tickets.close_at >= ? AND tickets.close_at < ?',
  52. params[:range_start],
  53. params[:range_end],
  54. ).where(query, *bind_params).joins(tables)
  55. count = 0
  56. ticket_list.each do |ticket|
  57. closed_at = ticket.close_at
  58. created_at = ticket.created_at
  59. if (closed_at - (60 * 15) ) < created_at
  60. count += 1
  61. end
  62. end
  63. result.push count
  64. params[:range_start] = params[:range_end]
  65. end
  66. result
  67. end
  68. =begin
  69. result = Report::TicketFirstSolution.items(
  70. range_start: Time.zone.parse('2015-01-01T00:00:00Z'),
  71. range_end: Time.zone.parse('2015-12-31T23:59:59Z'),
  72. selector: selector, # ticket selector to get only a collection of tickets
  73. timezone: 'Europe/Berlin',
  74. )
  75. returns
  76. {
  77. count: 123,
  78. ticket_ids: [4,5,1,5,0,51,5,56,7,4],
  79. assets: assets,
  80. }
  81. =end
  82. def self.items(params)
  83. without_merged_tickets = {
  84. 'ticket_state.name' => {
  85. 'operator' => 'is not',
  86. 'value' => 'merged'
  87. }
  88. }
  89. selector = params[:selector].merge!(without_merged_tickets)
  90. query, bind_params, tables = Ticket.selector2sql(selector)
  91. ticket_list = Ticket.select('tickets.id, tickets.close_at, tickets.created_at').where(
  92. 'tickets.close_at IS NOT NULL AND tickets.close_at >= ? AND tickets.close_at < ?',
  93. params[:range_start],
  94. params[:range_end],
  95. ).where(query, *bind_params).joins(tables).order(close_at: :asc)
  96. count = 0
  97. assets = {}
  98. ticket_ids = []
  99. ticket_list.each do |ticket|
  100. closed_at = ticket.close_at
  101. created_at = ticket.created_at
  102. if (closed_at - (60 * 15) ) < created_at
  103. count += 1
  104. ticket_ids.push ticket.id
  105. end
  106. ticket_full = Ticket.find(ticket.id)
  107. assets = ticket_full.assets(assets)
  108. end
  109. {
  110. count: count,
  111. ticket_ids: ticket_ids,
  112. assets: assets,
  113. }
  114. end
  115. end