ticket_first_solution.rb 3.6 KB

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