1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-01-17 06:52:27 +01:00

subscriptions export with all rows

This commit is contained in:
Sylvain 2016-07-06 19:00:22 +02:00
parent e8b1c99d2a
commit b0f7c634ea
2 changed files with 17 additions and 3 deletions

View File

@ -18,7 +18,13 @@ class API::StatisticsController < API::ApiController
def export_#{path}
authorize :statistic, :#{path}?
query = MultiJson.load(params[:body])
@results = Stats::#{path.classify}.search(query, request.query_parameters.symbolize_keys).response
@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
render xlsx: 'export_#{path}.xlsx', filename: "#{path}.xlsx"
end
}
@ -26,6 +32,7 @@ class API::StatisticsController < API::ApiController
def export_global
# query all stats with range arguments
Elasticsearch::Model.client.search
render xls: []
end

View File

@ -1,6 +1,13 @@
wb = xlsx_package.workbook
price = wb.styles.add_style :format_code => ''
wb.add_worksheet(name: "Abonnements") do |sheet|
@results[:hits][:hits].each do |hit|
sheet.add_row [hit[:_source].date, hit[:_source].userId, hit[:_source].gender, hit[:_source].age, hit[:_source].planId, hit[:_source].groupName, hit[:_source].ca]
sheet.add_row ['Entrées', @results['hits']['total']]
sheet.add_row ["Chiffre d'affaires", @results['aggregations']['total_ca']['value']]
sheet.add_row ['Âge moyen', @results['aggregations']['average_age']['value']]
sheet.add_row ['Date', 'ID Utilisateur', 'Genre', 'Âge', 'Type', 'Groupe', "Chiffre d'affaire"]
@results['hits']['hits'].each do |hit|
sheet.add_row [hit['_source']['date'], hit['_source']['userId'], hit['_source']['gender'], hit['_source']['age'], hit['_source']['planId'], hit['_source']['groupName'], hit['_source']['ca']]
end
end