ticket_moved.rb 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. class Report::TicketMoved < Report::Base
  2. =begin
  3. result = Report::TicketMoved.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. params: { type: 'in' }, # in|out
  9. )
  10. returns
  11. [4,5,1,5,0,51,5,56,7,4]
  12. =end
  13. def self.aggs(params)
  14. selector = params[:selector]['ticket.group_id']
  15. if !selector
  16. return [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
  17. end
  18. interval = params[:interval]
  19. if params[:interval] == 'week'
  20. interval = 'day'
  21. end
  22. result = []
  23. if params[:interval] == 'month'
  24. start = Date.parse(params[:range_start])
  25. stop_interval = 12
  26. elsif params[:interval] == 'week'
  27. start = Date.parse(params[:range_start])
  28. stop_interval = 7
  29. elsif params[:interval] == 'day'
  30. start = Date.parse(params[:range_start])
  31. stop_interval = 31
  32. elsif params[:interval] == 'hour'
  33. start = Time.zone.parse(params[:range_start])
  34. stop_interval = 24
  35. elsif params[:interval] == 'minute'
  36. start = Time.zone.parse(params[:range_start])
  37. stop_interval = 60
  38. end
  39. (1..stop_interval).each do |_counter|
  40. if params[:interval] == 'month'
  41. stop = start.next_month
  42. elsif params[:interval] == 'week'
  43. stop = start.next_day
  44. elsif params[:interval] == 'day'
  45. stop = start.next_day
  46. elsif params[:interval] == 'hour'
  47. stop = start + 1.hour
  48. elsif params[:interval] == 'minute'
  49. stop = start + 1.minute
  50. end
  51. local_params = group_attributes(selector, params)
  52. local_selector = params[:selector].clone
  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: start,
  61. end: stop,
  62. selector: local_selector
  63. }
  64. local_params = defaults.merge(local_params)
  65. count = history_count(local_params)
  66. result.push count
  67. start = stop
  68. end
  69. result
  70. end
  71. =begin
  72. result = Report::TicketMoved.items(
  73. range_start: '2015-01-01T00:00:00Z',
  74. range_end: '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. local_selector = params[:selector].clone
  95. if params[:params][:type] == 'out'
  96. local_selector.delete('ticket.group_id')
  97. end
  98. defaults = {
  99. object: 'Ticket',
  100. type: 'updated',
  101. attribute: 'group',
  102. start: params[:range_start],
  103. end: params[:range_end],
  104. selector: local_selector
  105. }
  106. local_params = defaults.merge(local_params)
  107. result = history(local_params)
  108. assets = {}
  109. result[:ticket_ids].each do |ticket_id|
  110. ticket_full = Ticket.find(ticket_id)
  111. assets = ticket_full.assets(assets)
  112. end
  113. result[:assets] = assets
  114. result
  115. end
  116. def self.group_attributes(selector, params)
  117. if selector['operator'] == 'is'
  118. group_id = selector['value']
  119. if params[:params][:type] == 'in'
  120. return {
  121. id_not_from: group_id,
  122. id_to: group_id,
  123. }
  124. else
  125. return {
  126. id_from: group_id,
  127. id_not_to: group_id,
  128. }
  129. end
  130. else
  131. group_id = selector['value']
  132. if params[:params][:type] == 'in'
  133. return {
  134. id_from: group_id,
  135. id_not_to: group_id,
  136. }
  137. else
  138. return {
  139. id_not_from: group_id,
  140. id_to: group_id,
  141. }
  142. end
  143. end
  144. raise "Unknown selector params '#{selector.inspect}'"
  145. end
  146. end