2019-01-16 16:28:25 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# API Controller for resources of type Space
|
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
|
|
|
|
|
2019-01-16 16:28:25 +01:00
|
|
|
%w[account event machine project subscription training user space].each do |path|
|
2016-03-23 18:39:41 +01:00
|
|
|
class_eval %{
|
|
|
|
def #{path}
|
|
|
|
authorize :statistic, :#{path}?
|
2016-09-06 16:32:41 +02:00
|
|
|
|
|
|
|
# remove additional parameters
|
2016-09-06 14:21:52 +02:00
|
|
|
statistic_type = request.query_parameters.delete('stat-type')
|
2017-01-03 17:07:23 +01:00
|
|
|
custom_query = request.query_parameters.delete('custom-query')
|
2016-09-06 16:32:41 +02:00
|
|
|
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
|
2016-09-06 14:21:52 +02:00
|
|
|
|
2016-09-06 16:32:41 +02:00
|
|
|
# run additional custom aggregations, if any
|
2017-01-03 17:07:23 +01:00
|
|
|
CustomAggregationService.new.("#{path}", statistic_type, start_date, end_date, custom_query, results)
|
2016-09-06 14:21:52 +02:00
|
|
|
|
2016-09-06 16:32:41 +02:00
|
|
|
# return result
|
2016-03-23 18:39:41 +01:00
|
|
|
render json: results
|
|
|
|
end
|
2019-01-16 16:28:25 +01:00
|
|
|
}, __FILE__, __LINE__ - 20
|
|
|
|
end
|
2016-07-04 17:15:37 +02:00
|
|
|
|
2019-01-16 16:28:25 +01:00
|
|
|
%w[account event machine project subscription training user space].each do |path|
|
|
|
|
class_eval %{
|
2016-07-04 17:15:37 +02:00
|
|
|
def export_#{path}
|
2016-07-12 12:03:38 +02:00
|
|
|
authorize :statistic, :export_#{path}?
|
2016-07-07 16:26:25 +02:00
|
|
|
|
2019-01-16 16:28:25 +01:00
|
|
|
export = Export.where(category:'statistics', export_type: '#{path}', query: params[:body], key: params[:type_key]).last
|
2016-07-27 11:28:54 +02:00
|
|
|
if export.nil? || !FileTest.exist?(export.file)
|
2019-01-16 16:28:25 +01:00
|
|
|
@export = Export.new(category:'statistics',
|
|
|
|
export_type: '#{path}',
|
|
|
|
user: current_user,
|
|
|
|
query: params[:body],
|
|
|
|
key: params[:type_key])
|
2016-07-27 11:28:54 +02:00
|
|
|
if @export.save
|
|
|
|
render json: {export_id: @export.id}, status: :ok
|
|
|
|
else
|
|
|
|
render json: @export.errors, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
else
|
2019-01-16 16:28:25 +01:00
|
|
|
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
|
2016-07-04 17:15:37 +02:00
|
|
|
end
|
2019-01-16 16:28:25 +01:00
|
|
|
}, __FILE__, __LINE__ - 22
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|
2016-06-21 13:16:42 +02:00
|
|
|
|
2016-07-05 12:21:55 +02:00
|
|
|
def export_global
|
2016-07-12 12:03:38 +02:00
|
|
|
authorize :statistic, :export_global?
|
|
|
|
|
2019-01-16 16:28:25 +01:00
|
|
|
export = Export.where(category: 'statistics', export_type: 'global', query: params[:body]).last
|
2016-07-27 11:28:54 +02:00
|
|
|
if export.nil? || !FileTest.exist?(export.file)
|
2019-01-16 16:28:25 +01:00
|
|
|
@export = Export.new(category: 'statistics', export_type: 'global', user: current_user, query: params[:body])
|
2016-07-27 11:28:54 +02:00
|
|
|
if @export.save
|
2019-01-16 16:28:25 +01:00
|
|
|
render json: { export_id: @export.id }, status: :ok
|
2016-07-27 11:28:54 +02:00
|
|
|
else
|
|
|
|
render json: @export.errors, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
else
|
2019-01-16 16:28:25 +01:00
|
|
|
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
|
2016-07-05 12:21:55 +02:00
|
|
|
end
|
|
|
|
|
2016-06-21 13:16:42 +02:00
|
|
|
def scroll
|
|
|
|
authorize :statistic, :scroll?
|
|
|
|
|
2016-06-23 11:11:54 +02:00
|
|
|
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
|