ticket_first_solution.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. class Report::TicketFirstSolution
  2. =begin
  3. result = Report::TicketFirstSolution.aggs(
  4. range_start: '2015-01-01T00:00:00Z',
  5. range_end: '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)
  13. interval = params[:interval]
  14. if params[:interval] == 'week'
  15. interval = 'day'
  16. end
  17. result = []
  18. if params[:interval] == 'month'
  19. start = Date.parse(params[:range_start])
  20. stop_interval = 12
  21. elsif params[:interval] == 'week'
  22. start = Date.parse(params[:range_start])
  23. stop_interval = 7
  24. elsif params[:interval] == 'day'
  25. start = Date.parse(params[:range_start])
  26. stop_interval = 31
  27. elsif params[:interval] == 'hour'
  28. start = Time.zone.parse(params[:range_start])
  29. stop_interval = 24
  30. elsif params[:interval] == 'minute'
  31. start = Time.zone.parse(params[:range_start])
  32. stop_interval = 60
  33. end
  34. (1..stop_interval).each { |_counter|
  35. if params[:interval] == 'month'
  36. stop = start.next_month
  37. elsif params[:interval] == 'week'
  38. stop = start.next_day
  39. elsif params[:interval] == 'day'
  40. stop = start.next_day
  41. elsif params[:interval] == 'hour'
  42. stop = start + 1.hour
  43. elsif params[:interval] == 'minute'
  44. stop = start + 1.minute
  45. end
  46. query, bind_params, tables = Ticket.selector2sql(params[:selector])
  47. ticket_list = Ticket.select('tickets.id, tickets.close_at, tickets.created_at').where(
  48. 'tickets.close_at IS NOT NULL AND tickets.close_at >= ? AND tickets.close_at < ?',
  49. start,
  50. stop,
  51. ).where(query, *bind_params).joins(tables)
  52. count = 0
  53. ticket_list.each { |ticket|
  54. closed_at = ticket.close_at
  55. created_at = ticket.created_at
  56. if (closed_at - (60 * 15) ) < created_at
  57. count += 1
  58. end
  59. }
  60. result.push count
  61. start = stop
  62. }
  63. result
  64. end
  65. =begin
  66. result = Report::TicketFirstSolution.items(
  67. range_start: '2015-01-01T00:00:00Z',
  68. range_end: '2015-12-31T23:59:59Z',
  69. selector: selector, # ticket selector to get only a collection of tickets
  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. query, bind_params, tables = Ticket.selector2sql(params[:selector])
  80. ticket_list = Ticket.select('tickets.id, tickets.close_at, tickets.created_at').where(
  81. 'tickets.close_at IS NOT NULL AND tickets.close_at >= ? AND tickets.close_at < ?',
  82. params[:range_start],
  83. params[:range_end],
  84. ).where(query, *bind_params).joins(tables).order(close_at: :asc)
  85. count = 0
  86. assets = {}
  87. ticket_ids = []
  88. ticket_list.each { |ticket|
  89. closed_at = ticket.close_at
  90. created_at = ticket.created_at
  91. if (closed_at - (60 * 15) ) < created_at
  92. count += 1
  93. ticket_ids.push ticket.id
  94. end
  95. ticket_full = Ticket.find(ticket.id)
  96. assets = ticket_full.assets(assets)
  97. }
  98. {
  99. count: count,
  100. ticket_ids: ticket_ids,
  101. assets: assets,
  102. }
  103. end
  104. end