ticket_generic_time.rb 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. class Report::TicketGenericTime
  2. =begin
  3. result = Report::TicketGenericTime.aggs(
  4. range_start: '2015-01-01T00:00:00Z',
  5. range_end: '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. )
  10. returns
  11. [4,5,1,5,0,51,5,56,7,4]
  12. =end
  13. def self.aggs(params)
  14. interval_es = params[:interval]
  15. if params[:interval] == 'week'
  16. interval_es = 'day'
  17. end
  18. aggs_interval = {
  19. from: params[:range_start],
  20. to: params[:range_end],
  21. interval: interval_es, # year, quarter, month, week, day, hour, minute, second
  22. field: params[:params][:field],
  23. }
  24. selector = params[:selector].clone
  25. if params[:params] && params[:params][:selector]
  26. selector = selector.merge(params[:params][:selector])
  27. end
  28. result_es = SearchIndexBackend.selectors(['Ticket'], selector, nil, nil, aggs_interval)
  29. if params[:interval] == 'month'
  30. start = Date.parse(params[:range_start])
  31. stop_interval = 12
  32. elsif params[:interval] == 'week'
  33. start = Date.parse(params[:range_start])
  34. stop_interval = 7
  35. elsif params[:interval] == 'day'
  36. start = Date.parse(params[:range_start])
  37. stop_interval = 31
  38. elsif params[:interval] == 'hour'
  39. start = Time.zone.parse(params[:range_start])
  40. stop_interval = 24
  41. elsif params[:interval] == 'minute'
  42. start = Time.zone.parse(params[:range_start])
  43. stop_interval = 60
  44. end
  45. result = []
  46. (1..stop_interval).each { |_counter|
  47. match = false
  48. if !result_es
  49. raise "Invalid es result #{result_es.inspect}"
  50. end
  51. if !result_es['aggregations']
  52. raise "Invalid es result, no aggregations #{result_es.inspect}"
  53. end
  54. if !result_es['aggregations']['time_buckets']
  55. raise "Invalid es result, no time_buckets #{result_es.inspect}"
  56. end
  57. if !result_es['aggregations']['time_buckets']['buckets']
  58. raise "Invalid es result, no buckets #{result_es.inspect}"
  59. end
  60. result_es['aggregations']['time_buckets']['buckets'].each { |item|
  61. if params[:interval] == 'minute'
  62. item['key_as_string'] = item['key_as_string'].sub(/:\d\d.\d\d\dZ$/, '')
  63. start_string = start.iso8601.sub(/:\d\dZ$/, '')
  64. else
  65. start_string = start.iso8601.sub(/:\d\d:\d\d.+?$/, '')
  66. end
  67. next if !item['doc_count']
  68. next if item['key_as_string'] !~ /#{start_string}/
  69. next if match
  70. match = true
  71. result.push item['doc_count']
  72. if params[:interval] == 'month'
  73. start = start.next_month
  74. elsif params[:interval] == 'week'
  75. start = start.next_day
  76. elsif params[:interval] == 'day'
  77. start = start.next_day
  78. elsif params[:interval] == 'hour'
  79. start = start + 1.hour
  80. elsif params[:interval] == 'minute'
  81. start = start + 1.minute
  82. end
  83. }
  84. next if match
  85. result.push 0
  86. if params[:interval] == 'month'
  87. start = start.next_month
  88. elsif params[:interval] == 'week'
  89. start = start.next_day
  90. elsif params[:interval] == 'day'
  91. start = start + 1.day
  92. elsif params[:interval] == 'hour'
  93. start = start + 1.hour
  94. elsif params[:interval] == 'minute'
  95. start = start + 1.minute
  96. end
  97. }
  98. result
  99. end
  100. =begin
  101. result = Report::TicketGenericTime.items(
  102. range_start: '2015-01-01T00:00:00Z',
  103. range_end: '2015-12-31T23:59:59Z',
  104. selector: selector, # ticket selector to get only a collection of tickets
  105. params: { field: 'created_at' },
  106. )
  107. returns
  108. {
  109. count: 123,
  110. ticket_ids: [4,5,1,5,0,51,5,56,7,4],
  111. assets: assets,
  112. }
  113. =end
  114. def self.items(params)
  115. aggs_interval = {
  116. from: params[:range_start],
  117. to: params[:range_end],
  118. field: params[:params][:field],
  119. }
  120. limit = 1000
  121. if !params[:sheet]
  122. limit = 100
  123. end
  124. selector = params[:selector].clone
  125. if params[:params] && params[:params][:selector]
  126. selector = selector.merge(params[:params][:selector])
  127. end
  128. result = SearchIndexBackend.selectors(['Ticket'], selector, limit, nil, aggs_interval)
  129. assets = {}
  130. result[:ticket_ids].each { |ticket_id|
  131. ticket_full = Ticket.find(ticket_id)
  132. assets = ticket_full.assets(assets)
  133. }
  134. result[:assets] = assets
  135. result
  136. end
  137. end