1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-01 12:24:28 +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 # 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 class API::StatisticsController < API::ApiController
before_action :authenticate_user! 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| %w[account event machine project subscription training user space order].each do |path|
class_eval %{ class_eval %{
def #{path} def #{path} # def account
authorize :statistic, :#{path}? 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 def export_#{path} # def export_account
statistic_type = request.query_parameters.delete('stat-type') authorize :statistic, :export_#{path}? # authorize :statistic, :export_account?
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 @export = Statistics::QueryService.export('#{path}', params) # @export = Statistics::QueryService.export('account', params)
query = MultiJson.load(request.body.read) if @export.is_a?(Export)
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])
if @export.save if @export.save
render json: {export_id: @export.id}, status: :ok render json: { export_id: @export.id }, status: :ok
else else
render json: @export.errors, status: :unprocessable_entity render json: @export.errors, status: :unprocessable_entity
end end
else else
send_file File.join(Rails.root, export.file), send_file @export,
type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
disposition: 'attachment' disposition: 'attachment'
end end
end end
}, __FILE__, __LINE__ - 42 }, __FILE__, __LINE__ - 22
end end
def export_global def export_global
authorize :statistic, :export_global? authorize :statistic, :export_global?
export = Export.where(category: 'statistics', export_type: 'global', query: params[:body]).last @export = Statistics::QueryService.export(global, params)
if export.nil? || !FileTest.exist?(export.file) if @export.is_a?(Export)
@export = Export.new(category: 'statistics', export_type: 'global', user: current_user, query: params[:body])
if @export.save if @export.save
render json: { export_id: @export.id }, status: :ok render json: { export_id: @export.id }, status: :ok
else else
render json: @export.errors, status: :unprocessable_entity render json: @export.errors, status: :unprocessable_entity
end end
else else
send_file File.join(Rails.root, export.file), send_file @export,
type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
disposition: 'attachment' disposition: 'attachment'
end end

View File

@ -12,6 +12,8 @@ class ExportService
last_export_reservations last_export_reservations
when 'users/subscription' when 'users/subscription'
last_export_subscriptions last_export_subscriptions
when %r{statistics/.*}
last_export_statistics(type.split('/')[1])
else else
raise TypeError "unknown export type: #{type}" raise TypeError "unknown export type: #{type}"
end end
@ -44,5 +46,10 @@ class ExportService
.where('created_at > ?', last_update) .where('created_at > ?', last_update)
.last .last
end end
def last_export_statistics(type)
Export.where(category: 'statistics', export_type: type, query: params[:body], key: params[:type_key])
.last
end
end 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