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}?
|
|
|
|
query = MultiJson.load(request.body.read)
|
|
|
|
results = Stats::#{path.classify}.search(query, request.query_parameters.symbolize_keys).response
|
|
|
|
render json: results
|
|
|
|
end
|
2016-07-04 17:15:37 +02:00
|
|
|
|
|
|
|
def export_#{path}
|
|
|
|
authorize :statistic, :#{path}?
|
2016-07-07 16:26:25 +02:00
|
|
|
|
2016-07-06 15:53:09 +02:00
|
|
|
query = MultiJson.load(params[:body])
|
2016-07-07 16:57:23 +02:00
|
|
|
type_key = params[:type_key]
|
2016-07-07 16:26:25 +02:00
|
|
|
|
2016-07-06 19:00:22 +02:00
|
|
|
@results = Elasticsearch::Model.client.search({index: 'stats', type: '#{path}', scroll: '30s', body: query})
|
|
|
|
scroll_id = @results['_scroll_id']
|
|
|
|
while @results['hits']['hits'].size != @results['hits']['total']
|
|
|
|
scroll_res = Elasticsearch::Model.client.scroll(scroll: '30s', scroll_id: scroll_id)
|
|
|
|
@results['hits']['hits'].concat(scroll_res['hits']['hits'])
|
|
|
|
scroll_id = scroll_res['_scroll_id']
|
|
|
|
end
|
2016-07-07 16:26:25 +02:00
|
|
|
|
2016-07-07 15:31:22 +02:00
|
|
|
ids = @results['hits']['hits'].map { |u| u['_source']['userId'] }
|
|
|
|
@users = User.includes(:profile).where(:id => ids)
|
2016-07-07 16:26:25 +02:00
|
|
|
|
|
|
|
@index = StatisticIndex.find_by(es_type_key: '#{path}')
|
|
|
|
@type = StatisticType.find_by(key: type_key, statistic_index_id: @index.id)
|
|
|
|
@subtypes = @type.statistic_sub_types
|
|
|
|
@fields = @index.statistic_fields
|
|
|
|
|
|
|
|
render xlsx: 'export_current.xlsx', filename: "#{path}.xlsx"
|
2016-07-04 17:15:37 +02:00
|
|
|
end
|
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
|
|
|
|
# query all stats with range arguments
|
2016-07-06 19:00:22 +02:00
|
|
|
Elasticsearch::Model.client.search
|
2016-07-05 12:21:55 +02:00
|
|
|
render xls: []
|
|
|
|
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
|