2019-01-16 16:28:25 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-10-12 14:19:59 +02:00
|
|
|
# API Controller for various statistical resources (gateway to elasticsearch DB)
|
2023-02-24 17:26:55 +01:00
|
|
|
class API::StatisticsController < API::APIController
|
2016-03-23 18:39:41 +01:00
|
|
|
before_action :authenticate_user!
|
|
|
|
|
|
|
|
def index
|
|
|
|
authorize :statistic, :index?
|
|
|
|
@statistics = StatisticIndex.all
|
|
|
|
end
|
|
|
|
|
2022-10-11 17:23:45 +02:00
|
|
|
%w[account event machine project subscription training user space order].each do |path|
|
2016-03-23 18:39:41 +01:00
|
|
|
class_eval %{
|
2022-10-12 14:19:59 +02:00
|
|
|
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
|
2016-09-06 16:32:41 +02:00
|
|
|
|
2022-10-12 14:19:59 +02:00
|
|
|
def export_#{path} # def export_account
|
|
|
|
authorize :statistic, :export_#{path}? # authorize :statistic, :export_account?
|
2016-09-06 16:32:41 +02:00
|
|
|
|
2022-11-16 16:55:56 +01:00
|
|
|
@export = Statistics::QueryService.export('#{path}', params, # @export = Statistics::QueryService.export('account', params,
|
|
|
|
current_user)
|
2022-10-12 14:19:59 +02:00
|
|
|
if @export.is_a?(Export)
|
2016-07-27 11:28:54 +02:00
|
|
|
if @export.save
|
2022-10-12 14:19:59 +02: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
|
2022-10-12 14:19:59 +02:00
|
|
|
send_file @export,
|
2019-01-16 16:28:25 +01:00
|
|
|
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
|
2022-11-16 16:55:56 +01:00
|
|
|
}, __FILE__, __LINE__ - 23
|
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?
|
|
|
|
|
2022-11-16 16:55:56 +01:00
|
|
|
@export = Statistics::QueryService.export('global', params, current_user)
|
2022-10-12 14:19:59 +02:00
|
|
|
if @export.is_a?(Export)
|
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
|
2022-10-12 14:19:59 +02:00
|
|
|
send_file @export,
|
2019-01-16 16:28:25 +01:00
|
|
|
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
|