diff --git a/app/controllers/api/statistics_controller.rb b/app/controllers/api/statistics_controller.rb index 5356c00cd..d657df10a 100644 --- a/app/controllers/api/statistics_controller.rb +++ b/app/controllers/api/statistics_controller.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -# API Controller for resources of type Space +# API Controller for various statistical resources (gateway to elasticsearch DB) class API::StatisticsController < API::ApiController before_action :authenticate_user! @@ -11,63 +11,42 @@ class API::StatisticsController < API::ApiController %w[account event machine project subscription training user space order].each do |path| class_eval %{ - def #{path} - authorize :statistic, :#{path}? + def #{path} # def account + authorize :statistic, :#{path}? # authorize :statistic, :account + render json: Statistics::QueryService.query('#{path}', request) # render json: Statistics::QueryService.query('account', request) + end # end - # remove additional parameters - statistic_type = request.query_parameters.delete('stat-type') - custom_query = request.query_parameters.delete('custom-query') - start_date = request.query_parameters.delete('start-date') - end_date = request.query_parameters.delete('end-date') + def export_#{path} # def export_account + authorize :statistic, :export_#{path}? # authorize :statistic, :export_account? - # run main query in elasticSearch - query = MultiJson.load(request.body.read) - results = Stats::#{path.classify}.search(query, request.query_parameters.symbolize_keys).response - - # run additional custom aggregations, if any - CustomAggregationService.new.("#{path}", statistic_type, start_date, end_date, custom_query, results) - - # return result - render json: results - end - - def export_#{path} - authorize :statistic, :export_#{path}? - - export = Export.where(category:'statistics', export_type: '#{path}', query: params[:body], key: params[:type_key]).last - if export.nil? || !FileTest.exist?(export.file) - @export = Export.new(category:'statistics', - export_type: '#{path}', - user: current_user, - query: params[:body], - key: params[:type_key]) + @export = Statistics::QueryService.export('#{path}', params) # @export = Statistics::QueryService.export('account', params) + if @export.is_a?(Export) if @export.save - render json: {export_id: @export.id}, status: :ok + render json: { export_id: @export.id }, status: :ok else render json: @export.errors, status: :unprocessable_entity end else - send_file File.join(Rails.root, export.file), + send_file @export, type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', disposition: 'attachment' end end - }, __FILE__, __LINE__ - 42 + }, __FILE__, __LINE__ - 22 end def export_global authorize :statistic, :export_global? - export = Export.where(category: 'statistics', export_type: 'global', query: params[:body]).last - if export.nil? || !FileTest.exist?(export.file) - @export = Export.new(category: 'statistics', export_type: 'global', user: current_user, query: params[:body]) + @export = Statistics::QueryService.export(global, params) + if @export.is_a?(Export) if @export.save render json: { export_id: @export.id }, status: :ok else render json: @export.errors, status: :unprocessable_entity end else - send_file File.join(Rails.root, export.file), + send_file @export, type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', disposition: 'attachment' end diff --git a/app/services/export_service.rb b/app/services/export_service.rb index 1e4bd3ebe..0ffea8daa 100644 --- a/app/services/export_service.rb +++ b/app/services/export_service.rb @@ -12,6 +12,8 @@ class ExportService last_export_reservations when 'users/subscription' last_export_subscriptions + when %r{statistics/.*} + last_export_statistics(type.split('/')[1]) else raise TypeError "unknown export type: #{type}" end @@ -44,5 +46,10 @@ class ExportService .where('created_at > ?', last_update) .last end + + def last_export_statistics(type) + Export.where(category: 'statistics', export_type: type, query: params[:body], key: params[:type_key]) + .last + end end end diff --git a/app/services/statistics/query_service.rb b/app/services/statistics/query_service.rb new file mode 100644 index 000000000..9c1580486 --- /dev/null +++ b/app/services/statistics/query_service.rb @@ -0,0 +1,37 @@ +# frozen_string_literal: true + +# Query the elasticsearch database of statistics and format the result +class Statistics::QueryService + class << self + def query(statistic_index, request) + # remove additional parameters + statistic_type = request.query_parameters.delete('stat-type') + custom_query = request.query_parameters.delete('custom-query') + start_date = request.query_parameters.delete('start-date') + end_date = request.query_parameters.delete('end-date') + + # run main query in elasticSearch + query = MultiJson.load(request.body.read) + model = "Stats::#{statistic_index}".constantize + results = model.search(query, request.query_parameters.symbolize_keys).response + + # run additional custom aggregations, if any + CustomAggregationService.new.call(statistic_index, statistic_type, start_date, end_date, custom_query, results) + + results + end + + def export(statistic_index, params) + export = ExportService.last_export("statistics/#{statistic_index}") + if export.nil? || !FileTest.exist?(export.file) + Export.new(category: 'statistics', + export_type: statistic_index, + user: current_user, + query: params[:body], + key: params[:type_key]) + else + File.root.join(export.file) + end + end + end +end