reports_controller.rb 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
  2. class ReportsController < ApplicationController
  3. before_action :authentication_check
  4. # GET /api/reports/config
  5. def config
  6. return if deny_if_not_role('Report')
  7. render json: {
  8. config: Report.config,
  9. profiles: Report::Profile.list,
  10. }
  11. end
  12. # GET /api/reports/generate
  13. def generate
  14. return if deny_if_not_role('Report')
  15. get_params = params_all
  16. return if !get_params
  17. result = {}
  18. get_params[:metric][:backend].each {|backend|
  19. condition = get_params[:profile].condition
  20. if backend[:condition]
  21. backend[:condition].merge(condition)
  22. else
  23. backend[:condition] = condition
  24. end
  25. next if !backend[:adapter]
  26. result[backend[:name]] = backend[:adapter].aggs(
  27. range_start: get_params[:start],
  28. range_end: get_params[:stop],
  29. interval: get_params[:range],
  30. selector: backend[:condition],
  31. params: backend[:params],
  32. )
  33. }
  34. #created = aggs(start, stop, range, 'created_at', profile.condition)
  35. #closed = aggs(start, stop, range, 'close_time', profile.condition)
  36. #first_solution =
  37. #reopend = backend(start, stop, range, Report::TicketReopened, profile.condition)
  38. # add backlog
  39. #backlogs = []
  40. #position = -1
  41. #created.each {|_not_used|
  42. # position += 1
  43. # diff = created[position][1] - closed[position][1]
  44. # backlog = [position+1, diff]
  45. # backlogs.push backlog
  46. #}
  47. render json: {
  48. data: result
  49. }
  50. end
  51. # GET /api/reports/sets
  52. def sets
  53. return if deny_if_not_role('Report')
  54. get_params = params_all
  55. return if !get_params
  56. if !params[:downloadBackendSelected]
  57. render json: {
  58. error: 'No such downloadBackendSelected param',
  59. }, status: :unprocessable_entity
  60. return
  61. end
  62. # get data
  63. result = {}
  64. get_params[:metric][:backend].each {|backend|
  65. next if params[:downloadBackendSelected] != backend[:name]
  66. condition = get_params[:profile].condition
  67. if backend[:condition]
  68. backend[:condition].merge(condition)
  69. else
  70. backend[:condition] = condition
  71. end
  72. next if !backend[:adapter]
  73. result = backend[:adapter].items(
  74. range_start: get_params[:start],
  75. range_end: get_params[:stop],
  76. interval: get_params[:range],
  77. selector: backend[:condition],
  78. params: backend[:params],
  79. )
  80. }
  81. render json: result
  82. end
  83. def params_all
  84. profile = nil
  85. if !params[:profiles]
  86. render json: {
  87. error: 'No such profiles param',
  88. }, status: :unprocessable_entity
  89. return
  90. end
  91. params[:profiles].each {|profile_id, active|
  92. next if !active
  93. profile = Report::Profile.find(profile_id)
  94. }
  95. if !profile
  96. render json: {
  97. error: 'No such active profile',
  98. }, status: :unprocessable_entity
  99. return
  100. end
  101. config = Report.config
  102. if !config || !config[:metric] || !config[:metric][params[:metric].to_sym]
  103. render json: {
  104. error: "No such metric #{params[:metric]}"
  105. }, status: :unprocessable_entity
  106. return
  107. end
  108. metric = config[:metric][params[:metric].to_sym]
  109. #{"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"}}
  110. if params[:timeRange] == 'realtime'
  111. start = (Time.zone.now - 60.minutes).iso8601
  112. stop = Time.zone.now.iso8601
  113. range = 'minute'
  114. elsif params[:timeRange] == 'day'
  115. date = Date.parse("#{params[:year]}-#{params[:month]}-#{params[:day]}").to_s
  116. start = "#{date}T00:00:00Z"
  117. stop = "#{date}T23:59:59Z"
  118. range = 'hour'
  119. elsif params[:timeRange] == 'week'
  120. start = Date.commercial(params[:year], params[:week]).iso8601
  121. stop = Date.parse(start).end_of_week
  122. range = 'week'
  123. elsif params[:timeRange] == 'month'
  124. start = Date.parse("#{params[:year]}-#{params[:month]}-01}").iso8601
  125. stop = Date.parse(start).end_of_month
  126. range = 'day'
  127. else
  128. start = "#{params[:year]}-01-01"
  129. stop = "#{params[:year]}-12-31"
  130. range = 'month'
  131. end
  132. {
  133. profile: profile,
  134. metric: metric,
  135. config: config,
  136. start: start,
  137. stop: stop,
  138. range: range,
  139. }
  140. end
  141. end