1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-11-29 10:24:20 +01:00

(quality) refactored API::StatisticsController

This commit is contained in:
Sylvain 2022-10-12 14:19:59 +02:00
parent 0016f248d8
commit eb0639f7b2
3 changed files with 59 additions and 36 deletions

View File

@ -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

View File

@ -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

View File

@ -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