From 8e8bd3f9ebbb6b0c777628b4be24899501c06a2d Mon Sep 17 00:00:00 2001 From: Sylvain Date: Thu, 7 Jul 2016 15:31:22 +0200 Subject: [PATCH] full subscriptions export --- README.md | 5 ++++ .../controllers/admin/statistics.coffee.erb | 8 +++++- .../admin/statistics/export.html.erb | 2 +- app/controllers/api/statistics_controller.rb | 4 +++ app/helpers/application_helper.rb | 11 ++++++++ .../statistics/export_subscription.xlsx.axlsx | 27 ++++++++++++++----- config/application.yml.default | 1 + config/locales/fr.yml | 21 ++++++++++++++- config/secrets.yml | 4 +++ 9 files changed, 73 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 94f47b3e9..6a0a3b075 100644 --- a/README.md +++ b/README.md @@ -531,6 +531,11 @@ See https://angular-ui.github.io/bootstrap/#uibdateparser-s-format-codes for a l **BEWARE**: years format with less than 4 digits will result in problems because the system won't be able to distinct dates with the same less significant digits, eg. 50 could mean 1950 or 2050. + EXCEL_DATE_FORMAT + +Date format for dates shown in exported Excel files (eg. statistics) +See https://support.microsoft.com/en-us/kb/264372 for a list a available formats. + #### Applying changes diff --git a/app/assets/javascripts/controllers/admin/statistics.coffee.erb b/app/assets/javascripts/controllers/admin/statistics.coffee.erb index 25e57300a..51b6bddd5 100644 --- a/app/assets/javascripts/controllers/admin/statistics.coffee.erb +++ b/app/assets/javascripts/controllers/admin/statistics.coffee.erb @@ -274,7 +274,7 @@ Application.Controllers.controller "StatisticsController", ["$scope", "$state", ## - # Run the current elastic query on the server and return the result as an Excel file + # Open a modal dialog asking the user for details about exporting the statistics tables to an excel file ## $scope.exportToExcel = -> options = @@ -532,6 +532,7 @@ Application.Controllers.controller "StatisticsController", ["$scope", "$state", Application.Controllers.controller 'ExportStatisticsController', [ '$scope', '$uibModalInstance', 'dates', 'query', 'key', 'CSRF' , ($scope, $uibModalInstance, dates, query, key, CSRF) -> + ## Retrieve Anti-CSRF tokens from cookies CSRF.setMetaTags() ## Bindings for date range @@ -546,6 +547,7 @@ Application.Controllers.controller 'ExportStatisticsController', [ '$scope', '$u ## Form action on the above URL $scope.method = "post" + ## Anti-CSRF token to inject into the download form $scope.csrfToken = angular.element('meta[name="csrf-token"]')[0].content ## Binding of the export type (global / current) @@ -590,6 +592,10 @@ Application.Controllers.controller 'ExportStatisticsController', [ '$scope', '$u + ## + # Callback when exchanging the export type between 'global' and 'current view' + # Adjust the query and the requesting url according to this type. + ## $scope.setRequest = -> if $scope.export.type == 'global' $scope.actionUrl = '/stats/global/export' diff --git a/app/assets/templates/admin/statistics/export.html.erb b/app/assets/templates/admin/statistics/export.html.erb index 403d517d0..4cebbbef3 100644 --- a/app/assets/templates/admin/statistics/export.html.erb +++ b/app/assets/templates/admin/statistics/export.html.erb @@ -67,7 +67,7 @@ - + diff --git a/app/controllers/api/statistics_controller.rb b/app/controllers/api/statistics_controller.rb index 693d8613d..5efc45d60 100644 --- a/app/controllers/api/statistics_controller.rb +++ b/app/controllers/api/statistics_controller.rb @@ -25,6 +25,10 @@ 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" end } diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index c504024bc..3d26f5936 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -52,6 +52,17 @@ module ApplicationHelper if (bool) then return :true else return :false end end + def get_item(array, id, key = nil) + array.each do |i| + if key.nil? + return i if i.id == id + else + return i if i[key] == id + end + + end + end + private ## inspired by gems/actionview-4.2.5/lib/action_view/helpers/translation_helper.rb diff --git a/app/views/api/statistics/export_subscription.xlsx.axlsx b/app/views/api/statistics/export_subscription.xlsx.axlsx index 1a9ac787f..1393f27fd 100644 --- a/app/views/api/statistics/export_subscription.xlsx.axlsx +++ b/app/views/api/statistics/export_subscription.xlsx.axlsx @@ -1,13 +1,26 @@ wb = xlsx_package.workbook -price = wb.styles.add_style :format_code => '' +bold = wb.styles.add_style :b => true +date = wb.styles.add_style :format_code => Rails.application.secrets.excel_date_format -wb.add_worksheet(name: "Abonnements") do |sheet| - 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"] +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| - sheet.add_row [hit['_source']['date'], hit['_source']['userId'], hit['_source']['gender'], hit['_source']['age'], hit['_source']['planId'], hit['_source']['groupName'], hit['_source']['ca']] + 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 \ No newline at end of file diff --git a/config/application.yml.default b/config/application.yml.default index 4facf7172..d041ecc51 100644 --- a/config/application.yml.default +++ b/config/application.yml.default @@ -47,6 +47,7 @@ TIME_ZONE: 'Paris' WEEK_STARTING_DAY: 'monday' D3_DATE_FORMAT: '%d/%m/%y' UIB_DATE_FORMAT: 'dd/MM/yyyy' +EXCEL_DATE_FORMAT: 'dd/mm/yyyy' OPENLAB_APP_SECRET: OPENLAB_APP_ID: diff --git a/config/locales/fr.yml b/config/locales/fr.yml index f34204b90..8c535a11e 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -255,4 +255,23 @@ fr: course: "Stage" workshop: "Atelier" account_creation: "Création de compte" - project_publication: "Publication de projet" \ No newline at end of file + project_publication: "Publication de projet" + + export: + # export des statistiques au format excel + subscriptions: "Abonnements" + entries: "Entrées" + revenue_: "Chiffre d'affaires" + average_age: "Âge moyen" + total: "Total" + date: "Date" + user: "Utilisateur" + email: "Courriel" + phone: "Téléphone" + gender: "Genre" + age: "Âge" + type: "Type" + group: "Groupe" + revenue: "Chiffre d'affaires" + male: "Homme" + female: "Femme" \ No newline at end of file diff --git a/config/secrets.yml b/config/secrets.yml index 4425cc5fc..7703aea4e 100644 --- a/config/secrets.yml +++ b/config/secrets.yml @@ -21,6 +21,7 @@ development: week_starting_day: <%= ENV["WEEK_STARTING_DAY"] %> d3_date_format: <%= ENV["D3_DATE_FORMAT"].dump %> # .dump is needed as the value may start by a '%', see https://github.com/tenderlove/psych/issues/75 uib_date_format: <%= ENV["UIB_DATE_FORMAT"] %> + excel_date_format: <%= ENV["EXCEL_DATE_FORMAT"]%> rails_locale: <%= ENV["RAILS_LOCALE"] %> moment_locale: <%= ENV["MOMENT_LOCALE"] %> summernote_locale: <%= ENV["SUMMERNOTE_LOCALE"] %> @@ -45,6 +46,7 @@ test: week_starting_day: monday d3_date_format: '%d/%m/%y' uib_date_format: dd/MM/yyyy + excel_date_format: dd/mm/yyyy rails_locale: en moment_locale: en summernote_locale: en-US @@ -76,6 +78,7 @@ staging: week_starting_day: <%= ENV["WEEK_STARTING_DAY"] %> d3_date_format: <%= ENV["D3_DATE_FORMAT"].dump %> uib_date_format: <%= ENV["UIB_DATE_FORMAT"] %> + excel_date_format: <%= ENV["EXCEL_DATE_FORMAT"]%> rails_locale: <%= ENV["RAILS_LOCALE"] %> moment_locale: <%= ENV["MOMENT_LOCALE"] %> summernote_locale: <%= ENV["SUMMERNOTE_LOCALE"] %> @@ -109,6 +112,7 @@ production: week_starting_day: <%= ENV["WEEK_STARTING_DAY"] %> d3_date_format: <%= ENV["D3_DATE_FORMAT"].dump %> uib_date_format: <%= ENV["UIB_DATE_FORMAT"] %> + excel_date_format: <%= ENV["EXCEL_DATE_FORMAT"]%> rails_locale: <%= ENV["RAILS_LOCALE"] %> moment_locale: <%= ENV["MOMENT_LOCALE"] %> summernote_locale: <%= ENV["SUMMERNOTE_LOCALE"] %>