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

80 lines
3.1 KiB
Ruby
Raw Normal View History

2016-03-23 18:39:41 +01:00
class API::StatisticsController < API::ApiController
before_action :authenticate_user!
def index
authorize :statistic, :index?
@statistics = StatisticIndex.all
end
%w(account event machine project subscription training user).each do |path|
class_eval %{
def #{path}
authorize :statistic, :#{path}?
# remove additional parameters
statistic_type = request.query_parameters.delete('stat-type')
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)
2016-03-23 18:39:41 +01:00
results = Stats::#{path.classify}.search(query, request.query_parameters.symbolize_keys).response
# run additional custom aggregations, if any
2016-11-21 11:45:48 +01:00
if statistic_type and start_date and end_date
2016-11-23 16:30:19 +01:00
stat_index = StatisticIndex.find_by(es_type_key: "#{path}")
2016-11-21 11:45:48 +01:00
stat_type = StatisticType.where(statistic_index_id: stat_index.id, key: statistic_type).first
client = Elasticsearch::Model.client
stat_type.statistic_custom_aggregations.each do |custom|
c_res = client.search index: custom.es_index, type:custom.es_type, body:sprintf(custom.query, {aggs_name: custom.field, start_date: start_date, end_date: end_date})
results['aggregations'][custom.field] = c_res['aggregations'][custom.field]
end
end
# return result
2016-03-23 18:39:41 +01:00
render json: results
end
def export_#{path}
2016-07-12 12:03:38 +02:00
authorize :statistic, :export_#{path}?
2016-07-07 16:26:25 +02:00
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
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), :type => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', :disposition => 'attachment'
2016-07-06 19:00:22 +02:00
end
end
2016-03-23 18:39:41 +01:00
}
end
2016-06-21 13:16:42 +02:00
def export_global
2016-07-12 12:03:38 +02:00
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]})
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), :type => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', :disposition => 'attachment'
2016-07-12 12:03:38 +02:00
end
end
2016-06-21 13:16:42 +02:00
def scroll
authorize :statistic, :scroll?
results = Elasticsearch::Model.client.scroll scroll: params[:scroll], scroll_id: params[:scrollId]
2016-06-21 13:16:42 +02:00
render json: results
end
2016-03-23 18:39:41 +01:00
end