|
@@ -16,39 +16,44 @@ class ReportsController < ApplicationController
|
|
|
def generate
|
|
|
return if deny_if_not_role('Report')
|
|
|
|
|
|
- #{"metric"=>"count", "year"=>2015, "month"=>10, "week"=>43, "day"=>20, "timeSlot"=>"year", "report"=>{"metric"=>"count", "year"=>2015, "month"=>10, "week"=>43, "day"=>20, "timeSlot"=>"year"}}
|
|
|
- if params[:timeRange] == 'realtime'
|
|
|
- start = (Time.zone.now - 60.minutes).iso8601
|
|
|
- stop = Time.zone.now.iso8601
|
|
|
- created = aggs(start, stop, 'minute', 'created_at')
|
|
|
- closed = aggs(start, stop, 'minute', 'close_time')
|
|
|
- elsif params[:timeRange] == 'day'
|
|
|
- date = Date.parse("#{params[:year]}-#{params[:month]}-#{params[:day]}").to_s
|
|
|
- start = "#{date}T00:00:00Z"
|
|
|
- stop = "#{date}T23:59:59Z"
|
|
|
- created = aggs(start, stop, 'hour', 'created_at')
|
|
|
- closed = aggs(start, stop, 'hour', 'close_time')
|
|
|
- elsif params[:timeRange] == 'week'
|
|
|
- start = Date.commercial(params[:year], params[:week]).iso8601
|
|
|
- stop = Date.parse(start).end_of_week
|
|
|
- created = aggs(start, stop, 'week', 'created_at')
|
|
|
- closed = aggs(start, stop, 'week', 'close_time')
|
|
|
- elsif params[:timeRange] == 'month'
|
|
|
- start = Date.parse("#{params[:year]}-#{params[:month]}-01}").iso8601
|
|
|
- stop = Date.parse(start).end_of_month
|
|
|
- created = aggs(start, stop, 'day', 'created_at')
|
|
|
- closed = aggs(start, stop, 'day', 'close_time')
|
|
|
- else
|
|
|
- start = "#{params[:year]}-01-01"
|
|
|
- stop = "#{params[:year]}-12-31"
|
|
|
- created = aggs(start, stop, 'month', 'created_at')
|
|
|
- closed = aggs(start, stop, 'month', 'close_time')
|
|
|
- end
|
|
|
+ get_params = params_all
|
|
|
+ return if !get_params
|
|
|
+
|
|
|
+ result = {}
|
|
|
+ get_params[:metric][:backend].each {|backend|
|
|
|
+ condition = get_params[:profile].condition
|
|
|
+ if backend[:condition]
|
|
|
+ backend[:condition].merge(condition)
|
|
|
+ else
|
|
|
+ backend[:condition] = condition
|
|
|
+ end
|
|
|
+ next if !backend[:adapter]
|
|
|
+ result[backend[:name]] = backend[:adapter].aggs(
|
|
|
+ range_start: get_params[:start],
|
|
|
+ range_end: get_params[:stop],
|
|
|
+ interval: get_params[:range],
|
|
|
+ selector: backend[:condition],
|
|
|
+ params: backend[:params],
|
|
|
+ )
|
|
|
+ }
|
|
|
+
|
|
|
+ #created = aggs(start, stop, range, 'created_at', profile.condition)
|
|
|
+ #closed = aggs(start, stop, range, 'close_time', profile.condition)
|
|
|
+ #first_solution =
|
|
|
+ #reopend = backend(start, stop, range, Report::TicketReopened, profile.condition)
|
|
|
+
|
|
|
+ # add backlog
|
|
|
+ #backlogs = []
|
|
|
+ #position = -1
|
|
|
+ #created.each {|_not_used|
|
|
|
+ # position += 1
|
|
|
+ # diff = created[position][1] - closed[position][1]
|
|
|
+ # backlog = [position+1, diff]
|
|
|
+ # backlogs.push backlog
|
|
|
+ #}
|
|
|
+
|
|
|
render json: {
|
|
|
- data: {
|
|
|
- created: created,
|
|
|
- closed: closed,
|
|
|
- }
|
|
|
+ data: result
|
|
|
}
|
|
|
end
|
|
|
|
|
@@ -56,109 +61,97 @@ class ReportsController < ApplicationController
|
|
|
def sets
|
|
|
return if deny_if_not_role('Report')
|
|
|
|
|
|
+ get_params = params_all
|
|
|
+ return if !get_params
|
|
|
+
|
|
|
+ if !params[:downloadBackendSelected]
|
|
|
+ render json: {
|
|
|
+ error: 'No such downloadBackendSelected param',
|
|
|
+ }, status: :unprocessable_entity
|
|
|
+ return
|
|
|
+ end
|
|
|
+
|
|
|
+ # get data
|
|
|
+ result = {}
|
|
|
+ get_params[:metric][:backend].each {|backend|
|
|
|
+ next if params[:downloadBackendSelected] != backend[:name]
|
|
|
+ condition = get_params[:profile].condition
|
|
|
+ if backend[:condition]
|
|
|
+ backend[:condition].merge(condition)
|
|
|
+ else
|
|
|
+ backend[:condition] = condition
|
|
|
+ end
|
|
|
+ next if !backend[:adapter]
|
|
|
+ result = backend[:adapter].items(
|
|
|
+ range_start: get_params[:start],
|
|
|
+ range_end: get_params[:stop],
|
|
|
+ interval: get_params[:range],
|
|
|
+ selector: backend[:condition],
|
|
|
+ params: backend[:params],
|
|
|
+ )
|
|
|
+ }
|
|
|
+ render json: result
|
|
|
+ end
|
|
|
+
|
|
|
+ def params_all
|
|
|
+ profile = nil
|
|
|
+ if !params[:profiles]
|
|
|
+ render json: {
|
|
|
+ error: 'No such profiles param',
|
|
|
+ }, status: :unprocessable_entity
|
|
|
+ return
|
|
|
+ end
|
|
|
+ params[:profiles].each {|profile_id, active|
|
|
|
+ next if !active
|
|
|
+ profile = Report::Profile.find(profile_id)
|
|
|
+ }
|
|
|
+ if !profile
|
|
|
+ render json: {
|
|
|
+ error: 'No such active profile',
|
|
|
+ }, status: :unprocessable_entity
|
|
|
+ return
|
|
|
+ end
|
|
|
+
|
|
|
+ config = Report.config
|
|
|
+ if !config || !config[:metric] || !config[:metric][params[:metric].to_sym]
|
|
|
+ render json: {
|
|
|
+ error: "No such metric #{params[:metric]}"
|
|
|
+ }, status: :unprocessable_entity
|
|
|
+ return
|
|
|
+ end
|
|
|
+ metric = config[:metric][params[:metric].to_sym]
|
|
|
+
|
|
|
#{"metric"=>"count", "year"=>2015, "month"=>10, "week"=>43, "day"=>20, "timeSlot"=>"year", "report"=>{"metric"=>"count", "year"=>2015, "month"=>10, "week"=>43, "day"=>20, "timeSlot"=>"year"}}
|
|
|
if params[:timeRange] == 'realtime'
|
|
|
start = (Time.zone.now - 60.minutes).iso8601
|
|
|
stop = Time.zone.now.iso8601
|
|
|
+ range = 'minute'
|
|
|
elsif params[:timeRange] == 'day'
|
|
|
date = Date.parse("#{params[:year]}-#{params[:month]}-#{params[:day]}").to_s
|
|
|
start = "#{date}T00:00:00Z"
|
|
|
stop = "#{date}T23:59:59Z"
|
|
|
+ range = 'hour'
|
|
|
elsif params[:timeRange] == 'week'
|
|
|
start = Date.commercial(params[:year], params[:week]).iso8601
|
|
|
stop = Date.parse(start).end_of_week
|
|
|
+ range = 'week'
|
|
|
elsif params[:timeRange] == 'month'
|
|
|
start = Date.parse("#{params[:year]}-#{params[:month]}-01}").iso8601
|
|
|
stop = Date.parse(start).end_of_month
|
|
|
+ range = 'day'
|
|
|
else
|
|
|
start = "#{params[:year]}-01-01"
|
|
|
stop = "#{params[:year]}-12-31"
|
|
|
+ range = 'month'
|
|
|
end
|
|
|
-
|
|
|
- # get data
|
|
|
- ticket_ids = []
|
|
|
- assets = {}
|
|
|
- Ticket.select('id').all.each {|ticket_part|
|
|
|
- ticket = Ticket.lookup(id: ticket_part.id)
|
|
|
- assets = ticket.assets(assets)
|
|
|
- ticket_ids.push ticket_part.id
|
|
|
- }
|
|
|
- count = Ticket.count
|
|
|
- render json: {
|
|
|
- ticket_ids: ticket_ids,
|
|
|
- assets: assets,
|
|
|
- count: count,
|
|
|
+ {
|
|
|
+ profile: profile,
|
|
|
+ metric: metric,
|
|
|
+ config: config,
|
|
|
+ start: start,
|
|
|
+ stop: stop,
|
|
|
+ range: range,
|
|
|
}
|
|
|
end
|
|
|
|
|
|
- def aggs(range_start, range_end, interval, field)
|
|
|
- interval_es = interval
|
|
|
- if interval == 'week'
|
|
|
- interval_es = 'day'
|
|
|
- end
|
|
|
- result = SearchIndexBackend.aggs(
|
|
|
- {
|
|
|
- },
|
|
|
- [range_start, range_end, field, interval_es],
|
|
|
- ['Ticket'],
|
|
|
- )
|
|
|
- data = []
|
|
|
- if interval == 'month'
|
|
|
- start = Date.parse(range_start)
|
|
|
- stop_interval = 12
|
|
|
- elsif interval == 'week'
|
|
|
- start = Date.parse(range_start)
|
|
|
- stop_interval = 7
|
|
|
- elsif interval == 'day'
|
|
|
- start = Date.parse(range_start)
|
|
|
- stop_interval = 31
|
|
|
- elsif interval == 'hour'
|
|
|
- start = Time.zone.parse(range_start)
|
|
|
- stop_interval = 24
|
|
|
- elsif interval == 'minute'
|
|
|
- start = Time.zone.parse(range_start)
|
|
|
- stop_interval = 60
|
|
|
- end
|
|
|
- (1..stop_interval).each {|counter|
|
|
|
- match = false
|
|
|
- result['aggregations']['time_buckets']['buckets'].each {|item|
|
|
|
- if interval == 'minute'
|
|
|
- item['key_as_string'] = item['key_as_string'].sub(/:\d\d.\d\d\dZ$/, '')
|
|
|
- start_string = start.iso8601.sub(/:\d\dZ$/, '')
|
|
|
- else
|
|
|
- start_string = start.iso8601.sub(/:\d\d:\d\d.+?$/, '')
|
|
|
- end
|
|
|
- next if !item['doc_count']
|
|
|
- next if item['key_as_string'] !~ /#{start_string}/
|
|
|
- next if match
|
|
|
- match = true
|
|
|
- data.push [counter, item['doc_count']]
|
|
|
- if interval == 'month'
|
|
|
- start = start.next_month
|
|
|
- elsif interval == 'week'
|
|
|
- start = start.next_day
|
|
|
- elsif interval == 'day'
|
|
|
- start = start.next_day
|
|
|
- elsif interval == 'hour'
|
|
|
- start = start + 1.hour
|
|
|
- elsif interval == 'minute'
|
|
|
- start = start + 1.minute
|
|
|
- end
|
|
|
- }
|
|
|
- next if match
|
|
|
- data.push [counter, 0]
|
|
|
- if interval == 'month'
|
|
|
- start = start.next_month
|
|
|
- elsif interval == 'week'
|
|
|
- start = start.next_day
|
|
|
- elsif interval == 'day'
|
|
|
- start = start + 1.day
|
|
|
- elsif interval == 'hour'
|
|
|
- start = start + 1.hour
|
|
|
- elsif interval == 'minute'
|
|
|
- start = start + 1.minute
|
|
|
- end
|
|
|
- }
|
|
|
- data
|
|
|
- end
|
|
|
end
|