trabajo-afectivo/lib/report/ticket_generic_time.rb

184 lines
5.5 KiB
Ruby
Raw Normal View History

2015-10-29 02:33:36 +00:00
class Report::TicketGenericTime
=begin
2015-10-29 09:07:45 +00:00
2015-10-29 02:33:36 +00:00
result = Report::TicketGenericTime.aggs(
range_start: Time.zone.parse('2015-01-01T00:00:00Z'),
range_end: Time.zone.parse('2015-12-31T23:59:59Z'),
2015-10-29 02:33:36 +00:00
interval: 'month', # year, quarter, month, week, day, hour, minute, second
selector: selector, # ticket selector to get only a collection of tickets
2015-10-29 09:07:45 +00:00
params: { field: 'created_at', selector: selector_sub },
timezone: 'Europe/Berlin',
2015-10-29 02:33:36 +00:00
)
returns
[4,5,1,5,0,51,5,56,7,4]
=end
def self.aggs(params_origin)
params = params_origin.dup
2015-10-29 02:33:36 +00:00
interval_es = params[:interval]
if params[:interval] == 'week'
interval_es = 'day'
end
aggs_interval = {
from: params[:range_start].iso8601,
to: params[:range_end].iso8601,
2015-10-29 02:33:36 +00:00
interval: interval_es, # year, quarter, month, week, day, hour, minute, second
field: params[:params][:field],
timezone: params[:timezone],
2015-10-29 02:33:36 +00:00
}
without_merged_tickets = {
'state' => {
'operator' => 'is not',
'value' => 'merged'
}
}
2015-10-29 09:07:45 +00:00
selector = params[:selector].clone
if params[:params].present? && params[:params][:selector].present?
2015-10-29 09:07:45 +00:00
selector = selector.merge(params[:params][:selector])
end
selector.merge!(without_merged_tickets) # do not show merged tickets in reports
2015-10-29 09:07:45 +00:00
result_es = SearchIndexBackend.selectors('Ticket', selector, {}, aggs_interval)
2015-10-29 02:33:36 +00:00
if params[:interval] == 'month'
stop_interval = 12
elsif params[:interval] == 'week'
stop_interval = 7
elsif params[:interval] == 'day'
stop_interval = ((params[:range_end] - params[:range_start]) / 86_400).to_i + 1
2015-10-29 02:33:36 +00:00
elsif params[:interval] == 'hour'
stop_interval = 24
elsif params[:interval] == 'minute'
stop_interval = 60
end
result = []
(1..stop_interval).each do |_counter|
2015-10-29 02:33:36 +00:00
match = false
2015-10-29 09:07:45 +00:00
if !result_es
2016-03-01 14:26:46 +00:00
raise "Invalid es result #{result_es.inspect}"
2015-10-29 09:07:45 +00:00
end
if !result_es['aggregations']
2016-03-01 14:26:46 +00:00
raise "Invalid es result, no aggregations #{result_es.inspect}"
2015-10-29 09:07:45 +00:00
end
if !result_es['aggregations']['time_buckets']
2016-03-01 14:26:46 +00:00
raise "Invalid es result, no time_buckets #{result_es.inspect}"
2015-10-29 09:07:45 +00:00
end
if !result_es['aggregations']['time_buckets']['buckets']
2016-03-01 14:26:46 +00:00
raise "Invalid es result, no buckets #{result_es.inspect}"
2015-10-29 09:07:45 +00:00
end
result_es['aggregations']['time_buckets']['buckets'].each do |item|
key_as_string = Time.zone.parse(item['key_as_string'])
2015-10-29 02:33:36 +00:00
next if !item['doc_count']
next if item['doc_count'].zero?
# only compare date - in certain cases elasticsearch timezone offset will not match
replace = ':\d\dZ$'
if params[:interval] == 'month'
replace = '\d\dT\d\d:\d\d:\d\dZ$'
elsif params[:interval] == 'day' || params[:interval] == 'week'
replace = '\d\d:\d\d:\d\dZ$'
end
next if key_as_string.iso8601.sub(/#{replace}/, '') != params[:range_start].iso8601.sub(/#{replace}/, '')
2015-10-29 02:33:36 +00:00
next if match
2015-10-29 02:33:36 +00:00
match = true
result.push item['doc_count']
if params[:interval] == 'month'
params[:range_start] = params[:range_start].next_month
2015-10-29 02:33:36 +00:00
elsif params[:interval] == 'week'
params[:range_start] = params[:range_start].next_day
2015-10-29 02:33:36 +00:00
elsif params[:interval] == 'day'
params[:range_start] = params[:range_start].next_day
2015-10-29 02:33:36 +00:00
elsif params[:interval] == 'hour'
params[:range_start] = params[:range_start] + 1.hour
2015-10-29 02:33:36 +00:00
elsif params[:interval] == 'minute'
params[:range_start] = params[:range_start] + 1.minute
2015-10-29 02:33:36 +00:00
end
end
2015-10-29 02:33:36 +00:00
next if match
2015-10-29 02:33:36 +00:00
result.push 0
if params[:interval] == 'month'
params[:range_start] = params[:range_start].next_month
2015-10-29 02:33:36 +00:00
elsif params[:interval] == 'week'
params[:range_start] = params[:range_start].next_day
2015-10-29 02:33:36 +00:00
elsif params[:interval] == 'day'
params[:range_start] = params[:range_start] + 1.day
2015-10-29 02:33:36 +00:00
elsif params[:interval] == 'hour'
params[:range_start] = params[:range_start] + 1.hour
2015-10-29 02:33:36 +00:00
elsif params[:interval] == 'minute'
params[:range_start] = params[:range_start] + 1.minute
2015-10-29 02:33:36 +00:00
end
end
2015-10-29 02:33:36 +00:00
result
end
=begin
result = Report::TicketGenericTime.items(
range_start: Time.zone.parse('2015-01-01T00:00:00Z'),
range_end: Time.zone.parse('2015-12-31T23:59:59Z'),
2015-10-29 02:33:36 +00:00
selector: selector, # ticket selector to get only a collection of tickets
params: { field: 'created_at' },
)
returns
{
count: 123,
ticket_ids: [4,5,1,5,0,51,5,56,7,4],
assets: assets,
}
=end
def self.items(params)
aggs_interval = {
from: params[:range_start].iso8601,
to: params[:range_end].iso8601,
2015-10-29 02:33:36 +00:00
field: params[:params][:field],
}
limit = 6000
if params[:sheet].blank?
2015-10-29 18:20:22 +00:00
limit = 100
end
without_merged_tickets = {
'state' => {
'operator' => 'is not',
'value' => 'merged'
}
}
2015-10-29 09:07:45 +00:00
selector = params[:selector].clone
if params[:params] && params[:params][:selector]
selector = selector.merge(params[:params][:selector])
end
selector.merge!(without_merged_tickets) # do not show merged tickets in reports
2015-10-29 09:07:45 +00:00
result = SearchIndexBackend.selectors('Ticket', selector, { limit: limit }, aggs_interval)
return result if params[:sheet].present?
2015-10-29 02:33:36 +00:00
assets = {}
result[:ticket_ids].each do |ticket_id|
suppress(ActiveRecord::RecordNotFound) do
ticket_full = Ticket.find(ticket_id)
assets = ticket_full.assets(assets)
end
end
2015-10-29 02:33:36 +00:00
result[:assets] = assets
result
end
end