ticket_generic_time.rb 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. class Report::TicketGenericTime
  2. =begin
  3. result = Report::TicketGenericTime.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', # year, quarter, month, week, day, hour, minute, second
  7. selector: selector, # ticket selector to get only a collection of tickets
  8. params: { field: 'created_at', selector: selector_sub },
  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. interval_es = params[:interval]
  17. if params[:interval] == 'week'
  18. interval_es = 'day'
  19. end
  20. aggs_interval = {
  21. from: params[:range_start].iso8601,
  22. to: params[:range_end].iso8601,
  23. interval: interval_es, # year, quarter, month, week, day, hour, minute, second
  24. field: params[:params][:field],
  25. timezone: params[:timezone],
  26. }
  27. without_merged_tickets = {
  28. 'state' => {
  29. 'operator' => 'is not',
  30. 'value' => 'merged'
  31. }
  32. }
  33. selector = params[:selector].clone
  34. if params[:params].present? && params[:params][:selector].present?
  35. selector = selector.merge(params[:params][:selector])
  36. end
  37. selector.merge!(without_merged_tickets) # do not show merged tickets in reports
  38. result_es = SearchIndexBackend.selectors(['Ticket'], selector, {}, aggs_interval)
  39. if params[:interval] == 'month'
  40. stop_interval = 12
  41. elsif params[:interval] == 'week'
  42. stop_interval = 7
  43. elsif params[:interval] == 'day'
  44. stop_interval = ((params[:range_end] - params[:range_start]) / 86_400).to_i + 1
  45. elsif params[:interval] == 'hour'
  46. stop_interval = 24
  47. elsif params[:interval] == 'minute'
  48. stop_interval = 60
  49. end
  50. result = []
  51. (1..stop_interval).each do |_counter|
  52. match = false
  53. if !result_es
  54. raise "Invalid es result #{result_es.inspect}"
  55. end
  56. if !result_es['aggregations']
  57. raise "Invalid es result, no aggregations #{result_es.inspect}"
  58. end
  59. if !result_es['aggregations']['time_buckets']
  60. raise "Invalid es result, no time_buckets #{result_es.inspect}"
  61. end
  62. if !result_es['aggregations']['time_buckets']['buckets']
  63. raise "Invalid es result, no buckets #{result_es.inspect}"
  64. end
  65. result_es['aggregations']['time_buckets']['buckets'].each do |item|
  66. key_as_string = Time.zone.parse(item['key_as_string'])
  67. next if !item['doc_count']
  68. next if item['doc_count'].zero?
  69. # only compare date - in certain cases elasticsearch timezone offset will not match
  70. replace = ':\d\dZ$'
  71. if params[:interval] == 'month'
  72. replace = '\d\dT\d\d:\d\d:\d\dZ$'
  73. elsif params[:interval] == 'day' || params[:interval] == 'week'
  74. replace = '\d\d:\d\d:\d\dZ$'
  75. end
  76. next if key_as_string.iso8601.sub(/#{replace}/, '') != params[:range_start].iso8601.sub(/#{replace}/, '')
  77. next if match
  78. match = true
  79. result.push item['doc_count']
  80. if params[:interval] == 'month'
  81. params[:range_start] = params[:range_start].next_month
  82. elsif params[:interval] == 'week'
  83. params[:range_start] = params[:range_start].next_day
  84. elsif params[:interval] == 'day'
  85. params[:range_start] = params[:range_start].next_day
  86. elsif params[:interval] == 'hour'
  87. params[:range_start] = params[:range_start] + 1.hour
  88. elsif params[:interval] == 'minute'
  89. params[:range_start] = params[:range_start] + 1.minute
  90. end
  91. end
  92. next if match
  93. result.push 0
  94. if params[:interval] == 'month'
  95. params[:range_start] = params[:range_start].next_month
  96. elsif params[:interval] == 'week'
  97. params[:range_start] = params[:range_start].next_day
  98. elsif params[:interval] == 'day'
  99. params[:range_start] = params[:range_start] + 1.day
  100. elsif params[:interval] == 'hour'
  101. params[:range_start] = params[:range_start] + 1.hour
  102. elsif params[:interval] == 'minute'
  103. params[:range_start] = params[:range_start] + 1.minute
  104. end
  105. end
  106. result
  107. end
  108. =begin
  109. result = Report::TicketGenericTime.items(
  110. range_start: Time.zone.parse('2015-01-01T00:00:00Z'),
  111. range_end: Time.zone.parse('2015-12-31T23:59:59Z'),
  112. selector: selector, # ticket selector to get only a collection of tickets
  113. params: { field: 'created_at' },
  114. )
  115. returns
  116. {
  117. count: 123,
  118. ticket_ids: [4,5,1,5,0,51,5,56,7,4],
  119. assets: assets,
  120. }
  121. =end
  122. def self.items(params)
  123. aggs_interval = {
  124. from: params[:range_start].iso8601,
  125. to: params[:range_end].iso8601,
  126. field: params[:params][:field],
  127. }
  128. limit = 6000
  129. if params[:sheet].blank?
  130. limit = 100
  131. end
  132. without_merged_tickets = {
  133. 'state' => {
  134. 'operator' => 'is not',
  135. 'value' => 'merged'
  136. }
  137. }
  138. selector = params[:selector].clone
  139. if params[:params] && params[:params][:selector]
  140. selector = selector.merge(params[:params][:selector])
  141. end
  142. selector.merge!(without_merged_tickets) # do not show merged tickets in reports
  143. result = SearchIndexBackend.selectors(['Ticket'], selector, { limit: limit }, aggs_interval)
  144. return result if params[:sheet].present?
  145. assets = {}
  146. result[:ticket_ids].each do |ticket_id|
  147. suppress(ActiveRecord::RecordNotFound) do
  148. ticket_full = Ticket.find(ticket_id)
  149. assets = ticket_full.assets(assets)
  150. end
  151. end
  152. result[:assets] = assets
  153. result
  154. end
  155. end