ticket_moved.rb 4.5 KB

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