trabajo-afectivo/app/controllers/reports_controller.rb

189 lines
5.5 KiB
Ruby
Raw Permalink Normal View History

# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
2015-10-20 08:48:43 +00:00
class ReportsController < ApplicationController
prepend_before_action { authentication_check && authorize! }
2015-10-20 08:48:43 +00:00
# GET /api/reports/config
2015-10-29 15:19:31 +00:00
def reporting_config
if !Report.enabled?
render json: {
error: 'Elasticsearch need to be configured!',
}
return
end
2015-10-20 08:48:43 +00:00
render json: {
config: Report.config,
2015-10-20 08:48:43 +00:00
profiles: Report::Profile.list,
}
end
# GET /api/reports/generate
def generate
2015-10-29 02:33:36 +00:00
get_params = params_all
return if !get_params
result = {}
get_params[:metric][:backend].each do |backend|
2015-10-29 02:33:36 +00:00
condition = get_params[:profile].condition
if backend[:condition]
backend[:condition].merge(condition)
else
backend[:condition] = condition
end
next if !backend[:adapter]
2015-10-29 02:33:36 +00:00
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],
timezone: get_params[:timezone],
timezone_offset: get_params[:timezone_offset],
current_user: current_user
2015-10-29 02:33:36 +00:00
)
end
2015-10-29 02:33:36 +00:00
2015-10-20 08:48:43 +00:00
render json: {
2015-10-29 02:33:36 +00:00
data: result
2015-10-20 08:48:43 +00:00
}
end
# GET /api/reports/sets
def sets
2015-10-29 02:33:36 +00:00
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 = {}
excel = nil
filename = nil
get_params[:metric][:backend].each do |backend|
2015-10-29 02:33:36 +00:00
next if params[:downloadBackendSelected] != backend[:name]
2015-10-29 02:33:36 +00:00
condition = get_params[:profile].condition
if backend[:condition]
backend[:condition].merge(condition)
else
backend[:condition] = condition
end
next if !backend[:adapter]
2015-10-29 02:33:36 +00:00
result = backend[:adapter].items(
range_start: get_params[:start],
range_end: get_params[:stop],
interval: get_params[:range],
selector: backend[:condition],
params: backend[:params],
sheet: params[:sheet],
timezone: get_params[:timezone],
timezone_offset: get_params[:timezone_offset],
current_user: current_user
2015-10-29 02:33:36 +00:00
)
2015-10-29 15:19:31 +00:00
result = { count: 0, ticket_ids: [] } if result.nil?
2015-10-29 15:19:31 +00:00
# generate sheet
if params[:sheet]
excel = ExcelSheet::Ticket.new(
title: "#{get_params[:profile].name} (#{backend[:display]})",
ticket_ids: result[:ticket_ids],
timezone: params[:timezone],
locale: current_user.locale,
)
filename = "tickets-#{get_params[:profile].name}-#{backend[:display]}.xls"
end
break
end
if excel
2015-10-29 15:19:31 +00:00
send_data(
excel.content,
filename: filename,
type: 'application/vnd.ms-excel',
2015-10-29 15:19:31 +00:00
disposition: 'attachment'
)
return
end
2015-10-29 15:19:31 +00:00
2015-10-29 02:33:36 +00:00
render json: result
end
def params_all
profile = nil
2015-10-29 15:19:31 +00:00
if !params[:profiles] && !params[:profile_id]
raise Exceptions::UnprocessableEntity, 'No such profiles param'
2015-10-29 02:33:36 +00:00
end
2015-10-29 15:19:31 +00:00
if params[:profile_id]
profile = Report::Profile.find(params[:profile_id])
else
params[:profiles].each do |profile_id, active|
2015-10-29 15:19:31 +00:00
next if !active
2015-10-29 15:19:31 +00:00
profile = Report::Profile.find(profile_id)
end
2015-10-29 15:19:31 +00:00
end
2015-10-29 02:33:36 +00:00
if !profile
raise Exceptions::UnprocessableEntity, 'No such active profile'
2015-10-29 02:33:36 +00:00
end
2015-10-29 15:19:31 +00:00
local_config = Report.config
if !local_config || !local_config[:metric] || !local_config[:metric][params[:metric].to_sym]
raise Exceptions::UnprocessableEntity, "No such metric #{params[:metric]}"
2015-10-29 02:33:36 +00:00
end
2015-10-29 15:19:31 +00:00
metric = local_config[:metric][params[:metric].to_sym]
2015-10-29 02:33:36 +00:00
case params[:timeRange]
when 'realtime'
start_at = (Time.zone.now - 60.minutes)
stop_at = Time.zone.now
2015-10-29 02:33:36 +00:00
range = 'minute'
when 'day'
2015-10-20 09:31:08 +00:00
date = Date.parse("#{params[:year]}-#{params[:month]}-#{params[:day]}").to_s
start_at = Time.zone.parse("#{date}T00:00:00Z")
stop_at = Time.zone.parse("#{date}T23:59:59Z")
2015-10-29 02:33:36 +00:00
range = 'hour'
when 'week'
start_week_at = Date.commercial(params[:year].to_i, params[:week].to_i)
stop_week_at = start_week_at.end_of_week
start_at = Time.zone.parse("#{start_week_at.year}-#{start_week_at.month}-#{start_week_at.day}T00:00:00Z")
stop_at = Time.zone.parse("#{stop_week_at.year}-#{stop_week_at.month}-#{stop_week_at.day}T23:59:59Z")
2015-10-29 02:33:36 +00:00
range = 'week'
when 'month'
start_at = Time.zone.parse("#{params[:year]}-#{params[:month]}-01T00:00:00Z")
stop_at = Time.zone.parse("#{params[:year]}-#{params[:month]}-#{start_at.end_of_month.day}T23:59:59Z")
2015-10-29 02:33:36 +00:00
range = 'day'
2015-10-20 08:48:43 +00:00
else
start_at = Time.zone.parse("#{params[:year]}-01-01T00:00:00Z")
stop_at = Time.zone.parse("#{params[:year]}-12-31T23:59:59Z")
range = 'month'
2015-10-20 08:48:43 +00:00
end
params[:timezone] ||= Setting.get('timezone_default')
if params[:timezone].present? && params[:timeRange] != 'realtime'
offset = stop_at.in_time_zone(params[:timezone]).utc_offset
start_at -= offset
stop_at -= offset
end
2015-10-29 02:33:36 +00:00
{
profile: profile,
metric: metric,
config: local_config,
start: start_at,
stop: stop_at,
range: range,
timezone: params[:timezone],
timezone_offset: offset,
2015-10-20 08:48:43 +00:00
}
end
end