reports_controller.rb 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. require 'tempfile'
  3. class ReportsController < ApplicationController
  4. before_action { authentication_check(permission: 'report') }
  5. # GET /api/reports/config
  6. def reporting_config
  7. if !Report.enabled?
  8. render json: {
  9. error: 'Elasticsearch need to be configured!',
  10. }
  11. return
  12. end
  13. render json: {
  14. config: Report.config,
  15. profiles: Report::Profile.list,
  16. }
  17. end
  18. # GET /api/reports/generate
  19. def generate
  20. get_params = params_all
  21. return if !get_params
  22. result = {}
  23. get_params[:metric][:backend].each { |backend|
  24. condition = get_params[:profile].condition
  25. if backend[:condition]
  26. backend[:condition].merge(condition)
  27. else
  28. backend[:condition] = condition
  29. end
  30. next if !backend[:adapter]
  31. result[backend[:name]] = backend[:adapter].aggs(
  32. range_start: get_params[:start],
  33. range_end: get_params[:stop],
  34. interval: get_params[:range],
  35. selector: backend[:condition],
  36. params: backend[:params],
  37. )
  38. }
  39. #created = aggs(start, stop, range, 'created_at', profile.condition)
  40. #closed = aggs(start, stop, range, 'close_at', profile.condition)
  41. #first_solution =
  42. #reopend = backend(start, stop, range, Report::TicketReopened, profile.condition)
  43. # add backlog
  44. #backlogs = []
  45. #position = -1
  46. #created.each {|_not_used|
  47. # position += 1
  48. # diff = created[position][1] - closed[position][1]
  49. # backlog = [position+1, diff]
  50. # backlogs.push backlog
  51. #}
  52. render json: {
  53. data: result
  54. }
  55. end
  56. # GET /api/reports/sets
  57. def sets
  58. get_params = params_all
  59. return if !get_params
  60. if !params[:downloadBackendSelected]
  61. render json: {
  62. error: 'No such downloadBackendSelected param',
  63. }, status: :unprocessable_entity
  64. return
  65. end
  66. # get data
  67. result = {}
  68. get_params[:metric][:backend].each { |backend|
  69. next if params[:downloadBackendSelected] != backend[:name]
  70. condition = get_params[:profile].condition
  71. if backend[:condition]
  72. backend[:condition].merge(condition)
  73. else
  74. backend[:condition] = condition
  75. end
  76. next if !backend[:adapter]
  77. result = backend[:adapter].items(
  78. range_start: get_params[:start],
  79. range_end: get_params[:stop],
  80. interval: get_params[:range],
  81. selector: backend[:condition],
  82. params: backend[:params],
  83. sheet: params[:sheet],
  84. )
  85. # generate sheet
  86. next if !params[:sheet]
  87. content = sheet(get_params[:profile], backend[:display], result)
  88. send_data(
  89. content,
  90. filename: "tickets-#{get_params[:profile].name}-#{backend[:display]}.xls",
  91. type: 'application/vnd.ms-excel',
  92. disposition: 'attachment'
  93. )
  94. }
  95. return if params[:sheet]
  96. render json: result
  97. end
  98. def params_all
  99. profile = nil
  100. if !params[:profiles] && !params[:profile_id]
  101. raise Exceptions::UnprocessableEntity, 'No such profiles param'
  102. end
  103. if params[:profile_id]
  104. profile = Report::Profile.find(params[:profile_id])
  105. else
  106. params[:profiles].each { |profile_id, active|
  107. next if !active
  108. profile = Report::Profile.find(profile_id)
  109. }
  110. end
  111. if !profile
  112. raise Exceptions::UnprocessableEntity, 'No such active profile'
  113. end
  114. local_config = Report.config
  115. if !local_config || !local_config[:metric] || !local_config[:metric][params[:metric].to_sym]
  116. raise Exceptions::UnprocessableEntity, "No such metric #{params[:metric]}"
  117. end
  118. metric = local_config[:metric][params[:metric].to_sym]
  119. #{"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"}}
  120. if params[:timeRange] == 'realtime'
  121. start = (Time.zone.now - 60.minutes).iso8601
  122. stop = Time.zone.now.iso8601
  123. range = 'minute'
  124. elsif params[:timeRange] == 'day'
  125. date = Date.parse("#{params[:year]}-#{params[:month]}-#{params[:day]}").to_s
  126. start = "#{date}T00:00:00Z"
  127. stop = "#{date}T23:59:59Z"
  128. range = 'hour'
  129. elsif params[:timeRange] == 'week'
  130. start = Date.commercial(params[:year], params[:week]).iso8601
  131. stop = Date.parse(start).end_of_week.iso8601
  132. range = 'week'
  133. elsif params[:timeRange] == 'month'
  134. start = Date.parse("#{params[:year]}-#{params[:month]}-01}").iso8601
  135. stop = Date.parse(start).end_of_month.iso8601
  136. range = 'day'
  137. else
  138. start = "#{params[:year]}-01-01"
  139. stop = Date.parse("#{params[:year]}-12-31").iso8601
  140. range = 'month'
  141. end
  142. {
  143. profile: profile,
  144. metric: metric,
  145. config: local_config,
  146. start: start,
  147. stop: stop,
  148. range: range,
  149. }
  150. end
  151. def sheet(profile, title, result)
  152. # Create a new Excel workbook
  153. temp_file = Tempfile.new('time_tracking.xls')
  154. workbook = WriteExcel.new(temp_file)
  155. # Add a worksheet
  156. worksheet = workbook.add_worksheet
  157. worksheet.set_column(0, 0, 10)
  158. worksheet.set_column(1, 1, 34)
  159. worksheet.set_column(2, 2, 10)
  160. worksheet.set_column(3, 3, 10)
  161. worksheet.set_column(4, 7, 20)
  162. # Add and define a format
  163. format = workbook.add_format # Add a format
  164. format.set_bold
  165. format.set_size(14)
  166. format.set_color('black')
  167. worksheet.set_row(0, 0, 6)
  168. # Write a formatted and unformatted string, row and column notation.
  169. worksheet.write(0, 0, "Tickets: #{profile.name} (#{title})", format)
  170. format_header = workbook.add_format # Add a format
  171. format_header.set_italic
  172. format_header.set_bg_color('gray')
  173. format_header.set_color('white')
  174. worksheet.write(2, 0, '#', format_header )
  175. worksheet.write(2, 1, 'Title', format_header )
  176. worksheet.write(2, 2, 'State', format_header )
  177. worksheet.write(2, 3, 'Priority', format_header )
  178. worksheet.write(2, 4, 'Customer', format_header )
  179. worksheet.write(2, 5, 'Created at', format_header )
  180. worksheet.write(2, 6, 'Updated at', format_header )
  181. worksheet.write(2, 7, 'Closed at', format_header )
  182. row = 2
  183. result[:ticket_ids].each { |ticket_id|
  184. ticket = Ticket.lookup(id: ticket_id)
  185. row += 1
  186. worksheet.write(row, 0, ticket.number )
  187. worksheet.write(row, 1, ticket.title)
  188. worksheet.write(row, 2, ticket.state.name)
  189. worksheet.write(row, 3, ticket.priority.name)
  190. worksheet.write(row, 4, ticket.customer.fullname)
  191. worksheet.write(row, 5, ticket.created_at)
  192. worksheet.write(row, 6, ticket.updated_at)
  193. worksheet.write(row, 7, ticket.close_at)
  194. }
  195. workbook.close
  196. # read file again
  197. file = File.new(temp_file, 'r')
  198. contents = file.read
  199. file.close
  200. contents
  201. end
  202. end