mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2025-02-19 13:54:25 +01:00
export all stats indices to excel
This commit is contained in:
parent
8e8bd3f9eb
commit
022db4e486
@ -17,7 +17,9 @@ class API::StatisticsController < API::ApiController
|
||||
|
||||
def export_#{path}
|
||||
authorize :statistic, :#{path}?
|
||||
|
||||
query = MultiJson.load(params[:body])
|
||||
|
||||
@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']
|
||||
@ -25,11 +27,17 @@ class API::StatisticsController < API::ApiController
|
||||
@results['hits']['hits'].concat(scroll_res['hits']['hits'])
|
||||
scroll_id = scroll_res['_scroll_id']
|
||||
end
|
||||
|
||||
ids = @results['hits']['hits'].map { |u| u['_source']['userId'] }
|
||||
@users = User.includes(:profile).where(:id => ids)
|
||||
|
||||
type_key = query['query']['bool']['must'][0]['term']['type'].to_s
|
||||
@subtypes = StatisticType.find_by(key: type_key, statistic_index_id: StatisticIndex.find_by(es_type_key: '#{path}').id).statistic_sub_types
|
||||
render xlsx: 'export_#{path}.xlsx', filename: "#{path}.xlsx"
|
||||
@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"
|
||||
end
|
||||
}
|
||||
end
|
||||
|
@ -59,8 +59,8 @@ module ApplicationHelper
|
||||
else
|
||||
return i if i[key] == id
|
||||
end
|
||||
|
||||
end
|
||||
nil
|
||||
end
|
||||
|
||||
|
||||
|
61
app/views/api/statistics/export_current.xlsx.axlsx
Normal file
61
app/views/api/statistics/export_current.xlsx.axlsx
Normal file
@ -0,0 +1,61 @@
|
||||
wb = xlsx_package.workbook
|
||||
|
||||
bold = wb.styles.add_style :b => true
|
||||
date = wb.styles.add_style :format_code => Rails.application.secrets.excel_date_format
|
||||
|
||||
wb.add_worksheet(name: @index.label) do |sheet|
|
||||
## heading stats for the current page
|
||||
sheet.add_row [t('export.entries'), @results['hits']['total']], :style => [bold, nil], :types => [:string, :integer]
|
||||
if @index.ca
|
||||
sheet.add_row [t('export.revenue'), @results['aggregations']['total_ca']['value']], :style => [bold, nil], :types => [:string, :float]
|
||||
end
|
||||
sheet.add_row [t('export.average_age'), @results['aggregations']['average_age']['value']], :style => [bold, nil], :types => [:string, :float]
|
||||
unless @type.simple
|
||||
sheet.add_row ["#{t('export.total')} #{@type.label}", @results['aggregations']['total_stat']['value']], :style => [bold, nil], :types => [:string, :integer]
|
||||
end
|
||||
sheet.add_row []
|
||||
|
||||
## data table
|
||||
# heading labels
|
||||
columns = [t('export.date'), t('export.user'), t('export.email'), t('export.phone'), t('export.gender'), t('export.age'), t('export.type')]
|
||||
columns.push @type.label unless @type.simple
|
||||
@fields.each do |f|
|
||||
columns.push f.label
|
||||
end
|
||||
columns.push t('export.revenue') if @index.ca
|
||||
sheet.add_row columns, :style => bold
|
||||
|
||||
# data rows
|
||||
@results['hits']['hits'].each do |hit|
|
||||
user = get_item(@users, hit['_source']['userId'])
|
||||
subtype = get_item(@subtypes, hit['_source']['subType'], 'key')
|
||||
data = [
|
||||
Date::strptime(hit['_source']['date'],'%Y-%m-%d'),
|
||||
user.profile.full_name,
|
||||
user.email,
|
||||
user.profile.phone,
|
||||
t("export.#{hit['_source']['gender']}"),
|
||||
hit['_source']['age'],
|
||||
subtype.nil? ? "" : subtype.label
|
||||
]
|
||||
styles = [date, nil, nil, nil, nil, nil, nil]
|
||||
types = [:date, :string, :string, :string, :string, :integer, :string]
|
||||
unless @type.simple
|
||||
data.push hit['_source']['stat']
|
||||
styles.push nil
|
||||
types.push :string
|
||||
end
|
||||
@fields.each do |f|
|
||||
data.push hit['_source'][f.key]
|
||||
styles.push nil
|
||||
types.push :string
|
||||
end
|
||||
if @index.ca
|
||||
data.push hit['_source']['ca']
|
||||
styles.push nil
|
||||
types.push :float
|
||||
end
|
||||
|
||||
sheet.add_row data, :style => styles, :types => types
|
||||
end
|
||||
end
|
@ -1,26 +0,0 @@
|
||||
wb = xlsx_package.workbook
|
||||
|
||||
bold = wb.styles.add_style :b => true
|
||||
date = wb.styles.add_style :format_code => Rails.application.secrets.excel_date_format
|
||||
|
||||
wb.add_worksheet(name: t('export.subscriptions')) do |sheet|
|
||||
sheet.add_row [t('export.entries'), @results['hits']['total']], :style => [bold, nil], :types => [:string, :float]
|
||||
sheet.add_row [t('export.revenue'), @results['aggregations']['total_ca']['value']], :style => [bold, nil], :types => [:string, :float]
|
||||
sheet.add_row [t('export.average_age'), @results['aggregations']['average_age']['value']], :style => [bold, nil], :types => [:string, :float]
|
||||
sheet.add_row []
|
||||
sheet.add_row [t('export.date'), t('export.user'), t('export.email'), t('export.phone'), t('export.gender'), t('export.age'), t('export.type'), t('export.group'), t('export.revenue')], :style => bold
|
||||
@results['hits']['hits'].each do |hit|
|
||||
user = get_item(@users, hit['_source']['userId'])
|
||||
sheet.add_row [
|
||||
Date::strptime(hit['_source']['date'],'%Y-%m-%d'),
|
||||
user.profile.full_name,
|
||||
user.email, user.profile.phone,
|
||||
t("export.#{hit['_source']['gender']}"),
|
||||
hit['_source']['age'],
|
||||
get_item(@subtypes, hit['_source']['subType'], 'key').label,
|
||||
hit['_source']['groupName'],
|
||||
hit['_source']['ca']
|
||||
], :style => [date, nil, nil, nil, nil, nil, nil, nil],
|
||||
:types => [:date, :string, :string, :string, :string, :integer, :string, :string, :float]
|
||||
end
|
||||
end
|
@ -251,4 +251,21 @@ en:
|
||||
bookings: "Bookings"
|
||||
hours_number: "Hours number"
|
||||
tickets_number: "Tickets number"
|
||||
revenue: "Revenue"
|
||||
revenue: "Revenue"
|
||||
|
||||
export:
|
||||
# statistics exports to the excel file format
|
||||
entries: "Entries"
|
||||
revenue: "Revenue"
|
||||
average_age: "Average Age"
|
||||
total: "Total"
|
||||
date: "Date"
|
||||
user: "User"
|
||||
email: "Email"
|
||||
phone: "Phone"
|
||||
gender: "Gender"
|
||||
age: "Age"
|
||||
type: "Type"
|
||||
revenue: "Revenue"
|
||||
male: "Man"
|
||||
female: "Woman"
|
@ -259,9 +259,8 @@ fr:
|
||||
|
||||
export:
|
||||
# export des statistiques au format excel
|
||||
subscriptions: "Abonnements"
|
||||
entries: "Entrées"
|
||||
revenue_: "Chiffre d'affaires"
|
||||
revenue: "Chiffre d'affaires"
|
||||
average_age: "Âge moyen"
|
||||
total: "Total"
|
||||
date: "Date"
|
||||
@ -271,7 +270,6 @@ fr:
|
||||
gender: "Genre"
|
||||
age: "Âge"
|
||||
type: "Type"
|
||||
group: "Groupe"
|
||||
revenue: "Chiffre d'affaires"
|
||||
male: "Homme"
|
||||
female: "Femme"
|
Loading…
x
Reference in New Issue
Block a user