2016-10-19 03:11:36 +00:00
|
|
|
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
|
2015-10-20 08:48:43 +00:00
|
|
|
class ReportsController < ApplicationController
|
2017-02-15 12:29:25 +00:00
|
|
|
prepend_before_action { authentication_check(permission: 'report') }
|
2015-10-20 08:48:43 +00:00
|
|
|
|
|
|
|
# GET /api/reports/config
|
2015-10-29 15:19:31 +00:00
|
|
|
def reporting_config
|
2016-11-09 14:05:18 +00:00
|
|
|
if !Report.enabled?
|
|
|
|
render json: {
|
|
|
|
error: 'Elasticsearch need to be configured!',
|
|
|
|
}
|
|
|
|
return
|
|
|
|
end
|
2015-10-20 08:48:43 +00:00
|
|
|
render json: {
|
2018-12-19 17:31:51 +00:00
|
|
|
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 = {}
|
2017-10-01 12:25:52 +00:00
|
|
|
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]
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2015-10-29 02:33:36 +00:00
|
|
|
result[backend[:name]] = backend[:adapter].aggs(
|
2019-03-26 00:17:17 +00:00
|
|
|
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],
|
2015-10-29 02:33:36 +00:00
|
|
|
)
|
2017-10-01 12:25:52 +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 = {}
|
2019-06-24 07:28:34 +00:00
|
|
|
content = nil
|
|
|
|
filename = nil
|
2017-10-01 12:25:52 +00:00
|
|
|
get_params[:metric][:backend].each do |backend|
|
2015-10-29 02:33:36 +00:00
|
|
|
next if params[:downloadBackendSelected] != backend[:name]
|
2018-10-09 06:17:41 +00:00
|
|
|
|
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]
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2015-10-29 02:33:36 +00:00
|
|
|
result = backend[:adapter].items(
|
2019-03-26 00:17:17 +00:00
|
|
|
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],
|
2015-10-29 02:33:36 +00:00
|
|
|
)
|
2015-10-29 15:19:31 +00:00
|
|
|
|
2018-04-27 13:35:20 +00:00
|
|
|
result = { count: 0, ticket_ids: [] } if result.nil?
|
|
|
|
|
2015-10-29 15:19:31 +00:00
|
|
|
# generate sheet
|
2019-06-24 07:28:34 +00:00
|
|
|
if params[:sheet]
|
|
|
|
content = sheet(get_params[:profile], backend[:display], result)
|
|
|
|
filename = "tickets-#{get_params[:profile].name}-#{backend[:display]}.xls"
|
|
|
|
end
|
|
|
|
break
|
|
|
|
end
|
|
|
|
if content
|
2015-10-29 15:19:31 +00:00
|
|
|
send_data(
|
|
|
|
content,
|
2019-06-24 07:28:34 +00:00
|
|
|
filename: filename,
|
2018-12-19 17:31:51 +00:00
|
|
|
type: 'application/vnd.ms-excel',
|
2015-10-29 15:19:31 +00:00
|
|
|
disposition: 'attachment'
|
|
|
|
)
|
2019-06-24 07:28:34 +00:00
|
|
|
return
|
2017-10-01 12:25:52 +00:00
|
|
|
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]
|
2016-06-30 08:24:03 +00:00
|
|
|
raise Exceptions::UnprocessableEntity, 'No such profiles param'
|
2015-10-29 02:33:36 +00:00
|
|
|
end
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2015-10-29 15:19:31 +00:00
|
|
|
if params[:profile_id]
|
|
|
|
profile = Report::Profile.find(params[:profile_id])
|
|
|
|
else
|
2017-10-01 12:25:52 +00:00
|
|
|
params[:profiles].each do |profile_id, active|
|
2015-10-29 15:19:31 +00:00
|
|
|
next if !active
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2015-10-29 15:19:31 +00:00
|
|
|
profile = Report::Profile.find(profile_id)
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2015-10-29 15:19:31 +00:00
|
|
|
end
|
2015-10-29 02:33:36 +00:00
|
|
|
if !profile
|
2016-06-30 08:24:03 +00:00
|
|
|
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]
|
2016-06-30 08:24:03 +00:00
|
|
|
raise Exceptions::UnprocessableEntity, "No such metric #{params[:metric]}"
|
2015-10-29 02:33:36 +00:00
|
|
|
end
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2015-10-29 15:19:31 +00:00
|
|
|
metric = local_config[:metric][params[:metric].to_sym]
|
2015-10-29 02:33:36 +00:00
|
|
|
|
2015-10-20 08:48:43 +00:00
|
|
|
if params[:timeRange] == 'realtime'
|
2019-03-26 00:17:17 +00:00
|
|
|
start_at = (Time.zone.now - 60.minutes)
|
|
|
|
stop_at = Time.zone.now
|
2015-10-29 02:33:36 +00:00
|
|
|
range = 'minute'
|
2015-10-20 08:48:43 +00:00
|
|
|
elsif params[:timeRange] == 'day'
|
2015-10-20 09:31:08 +00:00
|
|
|
date = Date.parse("#{params[:year]}-#{params[:month]}-#{params[:day]}").to_s
|
2019-03-26 00:17:17 +00:00
|
|
|
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'
|
2015-10-20 08:48:43 +00:00
|
|
|
elsif params[:timeRange] == 'week'
|
2019-03-26 00:17:17 +00:00
|
|
|
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'
|
2015-10-20 08:48:43 +00:00
|
|
|
elsif params[:timeRange] == 'month'
|
2019-03-26 00:17:17 +00:00
|
|
|
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
|
2019-03-26 00:17:17 +00:00
|
|
|
start_at = Time.zone.parse("#{params[:year]}-01-01T00:00:00Z")
|
|
|
|
stop_at = Time.zone.parse("#{params[:year]}-12-31T23:59:59Z")
|
2016-11-09 14:05:18 +00:00
|
|
|
range = 'month'
|
2015-10-20 08:48:43 +00:00
|
|
|
end
|
2019-03-26 00:17:17 +00:00
|
|
|
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
|
|
|
{
|
2019-03-26 00:17:17 +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
|
|
|
|
|
2015-10-29 15:19:31 +00:00
|
|
|
def sheet(profile, title, result)
|
|
|
|
|
2019-06-24 07:28:34 +00:00
|
|
|
params[:timezone] ||= Setting.get('timezone_default')
|
|
|
|
|
2015-10-29 15:19:31 +00:00
|
|
|
# Create a new Excel workbook
|
|
|
|
temp_file = Tempfile.new('time_tracking.xls')
|
|
|
|
workbook = WriteExcel.new(temp_file)
|
|
|
|
|
|
|
|
# Add a worksheet
|
|
|
|
worksheet = workbook.add_worksheet
|
2019-06-24 07:28:34 +00:00
|
|
|
worksheet.set_row(0, 18)
|
2015-10-29 15:19:31 +00:00
|
|
|
worksheet.set_column(0, 0, 10)
|
|
|
|
worksheet.set_column(1, 1, 34)
|
|
|
|
worksheet.set_column(2, 2, 10)
|
|
|
|
worksheet.set_column(3, 3, 10)
|
2017-04-17 23:14:54 +00:00
|
|
|
worksheet.set_column(4, 8, 20)
|
2019-06-24 07:28:34 +00:00
|
|
|
worksheet.set_column(11, 0, 20)
|
|
|
|
worksheet.set_column(12, 0, 20)
|
|
|
|
worksheet.set_column(13, 0, 20)
|
2015-10-29 15:19:31 +00:00
|
|
|
|
2017-04-18 08:42:33 +00:00
|
|
|
# Add and define a format
|
2017-06-07 08:34:38 +00:00
|
|
|
format = workbook.add_format
|
2015-10-29 15:19:31 +00:00
|
|
|
format.set_bold
|
|
|
|
format.set_size(14)
|
|
|
|
format.set_color('black')
|
|
|
|
|
|
|
|
# Write a formatted and unformatted string, row and column notation.
|
2018-01-22 14:37:42 +00:00
|
|
|
worksheet.write_string(0, 0, "Tickets: #{profile.name} (#{title})", format)
|
2015-10-29 15:19:31 +00:00
|
|
|
|
2017-06-07 08:34:38 +00:00
|
|
|
format_header = workbook.add_format
|
2015-10-29 15:19:31 +00:00
|
|
|
format_header.set_italic
|
|
|
|
format_header.set_bg_color('gray')
|
|
|
|
format_header.set_color('white')
|
2017-06-07 08:34:38 +00:00
|
|
|
|
2019-06-24 07:28:34 +00:00
|
|
|
format_time = workbook.add_format(num_format: 'yyyy-mm-dd hh:mm:ss')
|
|
|
|
format_date = workbook.add_format(num_format: 'yyyy-mm-dd')
|
|
|
|
|
|
|
|
format_footer = workbook.add_format
|
|
|
|
format_footer.set_italic
|
|
|
|
format_footer.set_color('gray')
|
|
|
|
format_footer.set_size(8)
|
|
|
|
|
2018-01-22 14:37:42 +00:00
|
|
|
worksheet.write_string(2, 0, '#', format_header)
|
|
|
|
worksheet.write_string(2, 1, 'Title', format_header)
|
|
|
|
worksheet.write_string(2, 2, 'State', format_header)
|
|
|
|
worksheet.write_string(2, 3, 'Priority', format_header)
|
|
|
|
worksheet.write_string(2, 4, 'Group', format_header)
|
|
|
|
worksheet.write_string(2, 5, 'Owner', format_header)
|
|
|
|
worksheet.write_string(2, 6, 'Customer', format_header)
|
|
|
|
worksheet.write_string(2, 7, 'Organization', format_header)
|
|
|
|
worksheet.write_string(2, 8, 'Create Channel', format_header)
|
|
|
|
worksheet.write_string(2, 9, 'Sender', format_header)
|
|
|
|
worksheet.write_string(2, 10, 'Tags', format_header)
|
|
|
|
worksheet.write_string(2, 11, 'Created at', format_header)
|
|
|
|
worksheet.write_string(2, 12, 'Updated at', format_header)
|
|
|
|
worksheet.write_string(2, 13, 'Closed at', format_header)
|
2019-06-24 07:28:34 +00:00
|
|
|
|
2018-04-27 13:35:20 +00:00
|
|
|
# ObjectManager attributes
|
|
|
|
header_column = 14
|
|
|
|
# needs to be skipped
|
2018-12-19 17:31:51 +00:00
|
|
|
objects = ObjectManager::Attribute.where(editable: true,
|
|
|
|
active: true,
|
|
|
|
to_create: false,
|
2018-04-27 13:35:20 +00:00
|
|
|
object_lookup_id: ObjectLookup.lookup(name: 'Ticket').id)
|
|
|
|
.pluck(:name, :display, :data_type, :data_option)
|
|
|
|
.map { |name, display, data_type, data_option| { name: name, display: display, data_type: data_type, data_option: data_option } }
|
|
|
|
objects.each do |object|
|
2019-06-24 07:28:34 +00:00
|
|
|
worksheet.set_column(header_column, 0, 16)
|
2018-04-27 13:35:20 +00:00
|
|
|
worksheet.write_string(2, header_column, object[:display].capitalize, format_header)
|
|
|
|
header_column += 1
|
|
|
|
end
|
2015-10-29 15:19:31 +00:00
|
|
|
|
|
|
|
row = 2
|
2017-06-07 08:34:38 +00:00
|
|
|
result[:ticket_ids].each do |ticket_id|
|
2019-06-27 18:26:28 +00:00
|
|
|
|
|
|
|
ticket = Ticket.lookup(id: ticket_id)
|
|
|
|
row += 1
|
|
|
|
worksheet.write_string(row, 0, ticket.number)
|
|
|
|
worksheet.write_string(row, 1, ticket.title)
|
|
|
|
worksheet.write_string(row, 2, ticket.state.name)
|
|
|
|
worksheet.write_string(row, 3, ticket.priority.name)
|
|
|
|
worksheet.write_string(row, 4, ticket.group.name)
|
|
|
|
worksheet.write_string(row, 5, ticket.owner.fullname)
|
|
|
|
worksheet.write_string(row, 6, ticket.customer.fullname)
|
|
|
|
worksheet.write_string(row, 7, ticket.try(:organization).try(:name))
|
|
|
|
worksheet.write_string(row, 8, ticket.create_article_type.name)
|
|
|
|
worksheet.write_string(row, 9, ticket.create_article_sender.name)
|
|
|
|
worksheet.write_string(row, 10, ticket.tag_list.join(','))
|
|
|
|
|
|
|
|
worksheet.write_date_time(row, 11, time_in_localtime_for_excel(ticket.created_at, params[:timezone]), format_time)
|
|
|
|
worksheet.write_date_time(row, 12, time_in_localtime_for_excel(ticket.updated_at, params[:timezone]), format_time)
|
|
|
|
worksheet.write_date_time(row, 13, time_in_localtime_for_excel(ticket.close_at, params[:timezone]), format_time) if ticket.close_at.present?
|
|
|
|
|
|
|
|
# Object Manager attributes
|
|
|
|
column = 14
|
|
|
|
# We already queried ObjectManager::Attributes, so we just use objects
|
|
|
|
objects.each do |object|
|
|
|
|
key = object[:name]
|
|
|
|
case object[:data_type]
|
|
|
|
when 'boolean', 'select'
|
|
|
|
value = ticket.send(key.to_sym)
|
|
|
|
if object[:data_option] && object[:data_option]['options'] && object[:data_option]['options'][ticket.send(key.to_sym)]
|
|
|
|
value = object[:data_option]['options'][ticket.send(key.to_sym)]
|
2018-04-27 13:35:20 +00:00
|
|
|
end
|
2019-06-27 18:26:28 +00:00
|
|
|
worksheet.write_string(row, column, value)
|
|
|
|
when 'datetime'
|
|
|
|
worksheet.write_date_time(row, column, time_in_localtime_for_excel(ticket.send(key.to_sym), params[:timezone]), format_time) if ticket.send(key.to_sym).present?
|
|
|
|
when 'date'
|
|
|
|
worksheet.write_date_time(row, column, ticket.send(key.to_sym).to_s, format_date) if ticket.send(key.to_sym).present?
|
|
|
|
when 'integer'
|
|
|
|
worksheet.write_number(row, column, ticket.send(key.to_sym))
|
|
|
|
else
|
|
|
|
# for text, integer and tree select
|
|
|
|
worksheet.write_string(row, column, ticket.send(key.to_sym).to_s)
|
2018-04-27 13:35:20 +00:00
|
|
|
end
|
2019-06-27 18:26:28 +00:00
|
|
|
column += 1
|
2017-12-12 02:54:50 +00:00
|
|
|
end
|
2019-06-27 18:26:28 +00:00
|
|
|
rescue => e
|
|
|
|
Rails.logger.error "SKIP: #{e.message}"
|
|
|
|
|
2017-06-07 08:34:38 +00:00
|
|
|
end
|
2015-10-29 15:19:31 +00:00
|
|
|
|
2019-06-24 07:28:34 +00:00
|
|
|
row += 2
|
|
|
|
worksheet.write_string(row, 0, "#{Translation.translate(current_user.locale, 'Timezone')}: #{params[:timezone]}", format_footer)
|
|
|
|
|
2015-10-29 15:19:31 +00:00
|
|
|
workbook.close
|
|
|
|
|
|
|
|
# read file again
|
|
|
|
file = File.new(temp_file, 'r')
|
|
|
|
contents = file.read
|
|
|
|
file.close
|
|
|
|
contents
|
|
|
|
end
|
|
|
|
|
2019-06-24 07:28:34 +00:00
|
|
|
def time_in_localtime_for_excel(time, timezone)
|
|
|
|
return if time.blank?
|
|
|
|
|
|
|
|
if timezone.present?
|
|
|
|
offset = time.in_time_zone(timezone).utc_offset
|
|
|
|
time -= offset
|
|
|
|
end
|
|
|
|
time.utc.iso8601.to_s.sub(/Z$/, '')
|
|
|
|
end
|
2015-10-20 08:48:43 +00:00
|
|
|
end
|