ticket_moved.rb 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. class Report::TicketMoved < Report::Base
  2. =begin
  3. result = Report::TicketMoved.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. params: { type: 'in' }, # in|out
  9. timezone: 'Europe/Berlin',
  10. )
  11. returns
  12. [4,5,1,5,0,51,5,56,7,4]
  13. =end
  14. def self.aggs(params_origin)
  15. params = params_origin.dup
  16. selector = params[:selector]['ticket.group_id']
  17. if !selector
  18. return [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
  19. end
  20. interval = params[:interval]
  21. if params[:interval] == 'week'
  22. interval = 'day'
  23. end
  24. result = []
  25. if params[:interval] == 'month'
  26. stop_interval = 12
  27. elsif params[:interval] == 'week'
  28. stop_interval = 7
  29. elsif params[:interval] == 'day'
  30. stop_interval = 31
  31. elsif params[:interval] == 'hour'
  32. stop_interval = 24
  33. elsif params[:interval] == 'minute'
  34. stop_interval = 60
  35. end
  36. (1..stop_interval).each do |_counter|
  37. if params[:interval] == 'month'
  38. params[:range_end] = params[:range_start].next_month
  39. elsif params[:interval] == 'week'
  40. params[:range_end] = params[:range_start].next_day
  41. elsif params[:interval] == 'day'
  42. params[:range_end] = params[:range_start].next_day
  43. elsif params[:interval] == 'hour'
  44. params[:range_end] = params[:range_start] + 1.hour
  45. elsif params[:interval] == 'minute'
  46. params[:range_end] = params[:range_start] + 1.minute
  47. end
  48. local_params = group_attributes(selector, params)
  49. local_selector = params[:selector].clone
  50. without_merged_tickets = {
  51. 'ticket_state.name' => {
  52. 'operator' => 'is not',
  53. 'value' => 'merged'
  54. }
  55. }
  56. local_selector.merge!(without_merged_tickets) # do not show merged tickets in reports
  57. if params[:params][:type] == 'out'
  58. local_selector.delete('ticket.group_id')
  59. end
  60. defaults = {
  61. object: 'Ticket',
  62. type: 'updated',
  63. attribute: 'group',
  64. start: params[:range_start],
  65. end: params[:range_end],
  66. selector: local_selector
  67. }
  68. local_params = defaults.merge(local_params)
  69. count = history_count(local_params)
  70. result.push count
  71. params[:range_start] = params[:range_end]
  72. end
  73. result
  74. end
  75. =begin
  76. result = Report::TicketMoved.items(
  77. range_start: Time.zone.parse('2015-01-01T00:00:00Z'),
  78. range_end: Time.zone.parse('2015-12-31T23:59:59Z'),
  79. selector: selector, # ticket selector to get only a collection of tickets
  80. params: { type: 'in' }, # in|out
  81. )
  82. returns
  83. {
  84. count: 123,
  85. ticket_ids: [4,5,1,5,0,51,5,56,7,4],
  86. assets: assets,
  87. }
  88. =end
  89. def self.items(params)
  90. selector = params[:selector]['ticket.group_id']
  91. if !selector
  92. return {
  93. count: 0,
  94. ticket_ids: [],
  95. }
  96. end
  97. local_params = group_attributes(selector, params)
  98. local_selector = params[:selector].clone
  99. without_merged_tickets = {
  100. 'ticket_state.name' => {
  101. 'operator' => 'is not',
  102. 'value' => 'merged'
  103. }
  104. }
  105. local_selector = params[:selector].merge!(without_merged_tickets) # do not show merged tickets in reports
  106. if params[:params][:type] == 'out'
  107. local_selector.delete('ticket.group_id')
  108. end
  109. defaults = {
  110. object: 'Ticket',
  111. type: 'updated',
  112. attribute: 'group',
  113. start: params[:range_start],
  114. end: params[:range_end],
  115. selector: local_selector
  116. }
  117. local_params = defaults.merge(local_params)
  118. result = history(local_params)
  119. return result if params[:sheet].present?
  120. assets = {}
  121. result[:ticket_ids].each do |ticket_id|
  122. ticket_full = Ticket.find(ticket_id)
  123. assets = ticket_full.assets(assets)
  124. end
  125. result[:assets] = assets
  126. result
  127. end
  128. def self.group_attributes(selector, params)
  129. group_id = selector['value']
  130. if selector['operator'] == 'is'
  131. if params[:params][:type] == 'in'
  132. return {
  133. id_not_from: group_id,
  134. id_to: group_id,
  135. }
  136. else
  137. return {
  138. id_from: group_id,
  139. id_not_to: group_id,
  140. }
  141. end
  142. elsif selector['operator'] == 'is not'
  143. if params[:params][:type] == 'in'
  144. return {
  145. id_from: group_id,
  146. id_not_to: group_id,
  147. }
  148. else
  149. return {
  150. id_not_from: group_id,
  151. id_to: group_id,
  152. }
  153. end
  154. end
  155. raise "Unknown selector params '#{selector.inspect}'"
  156. end
  157. end