From 38c00391fc8ae2ea6bbd35ddb44276f441b54739 Mon Sep 17 00:00:00 2001 From: Sylvain Date: Mon, 4 Jul 2016 17:15:37 +0200 Subject: [PATCH] basic UI and API for exporting stats to excel --- ...tatistics.coffee => statistics.coffee.erb} | 81 ++++++++++++++++++- .../admin/statistics/export.html.erb | 68 ++++++++++++++++ .../templates/admin/statistics/index.html.erb | 1 + app/controllers/api/statistics_controller.rb | 10 +++ config/locales/app.admin.en.yml | 4 + config/locales/app.admin.fr.yml | 4 + config/routes.rb | 1 + 7 files changed, 166 insertions(+), 3 deletions(-) rename app/assets/javascripts/controllers/admin/{statistics.coffee => statistics.coffee.erb} (87%) create mode 100644 app/assets/templates/admin/statistics/export.html.erb diff --git a/app/assets/javascripts/controllers/admin/statistics.coffee b/app/assets/javascripts/controllers/admin/statistics.coffee.erb similarity index 87% rename from app/assets/javascripts/controllers/admin/statistics.coffee rename to app/assets/javascripts/controllers/admin/statistics.coffee.erb index 454b9961f..60925e364 100644 --- a/app/assets/javascripts/controllers/admin/statistics.coffee +++ b/app/assets/javascripts/controllers/admin/statistics.coffee.erb @@ -1,7 +1,9 @@ 'use strict' -Application.Controllers.controller "StatisticsController", ["$scope", "$state", "$rootScope", "Statistics", "es", "Member", '_t', 'membersPromise', 'statisticsPromise' -, ($scope, $state, $rootScope, Statistics, es, Member, _t, membersPromise, statisticsPromise) -> + + +Application.Controllers.controller "StatisticsController", ["$scope", "$state", "$rootScope", '$uibModal', "Statistics", "es", "Member", '_t', 'membersPromise', 'statisticsPromise' +, ($scope, $state, $rootScope, $uibModal, Statistics, es, Member, _t, membersPromise, statisticsPromise) -> @@ -271,6 +273,27 @@ Application.Controllers.controller "StatisticsController", ["$scope", "$state", + ## + # Run the current elastic query on the server and return the result as an Excel file + ## + $scope.exportToExcel = -> + options = + templateUrl: '<%= asset_path "admin/statistics/export.html" %>' + size: 'sm' + controller: 'ExportStatisticsController' + resolve: + dates: -> + start: $scope.datePickerStart.selected + end: $scope.datePickerEnd.selected + + $uibModal.open options + .result['finally'](null).then (info)-> + console.info(info) + , (reason)-> + console.error(reason) + + + ### PRIVATE SCOPE ### ## @@ -429,7 +452,7 @@ Application.Controllers.controller "StatisticsController", ["$scope", "$state", "field": "age" "total_stat": "sum": - "field": "sta" + "field": "stat" } q @@ -490,3 +513,55 @@ Application.Controllers.controller "StatisticsController", ["$scope", "$state", initialize() ] + + + +Application.Controllers.controller 'ExportStatisticsController', [ '$scope', '$uibModalInstance', 'dates', ($scope, $uibModalInstance, dates) -> + + # bindings for date range + $scope.dates = dates + + + $scope.export = + type: 'current' + + ## datePicker parameters for interval beginning + $scope.exportStart = + format: Fablab.uibDateFormat + opened: false # default: datePicker is not shown + minDate: null + maxDate: moment().subtract(1, 'day').toDate() + options: + startingDay: Fablab.weekStartingDay + + ## datePicker parameters for interval ending + $scope.exportEnd = + format: Fablab.uibDateFormat + opened: false # default: datePicker is not shown + minDate: null + maxDate: moment().subtract(1, 'day').toDate() + options: + startingDay: Fablab.weekStartingDay + + ## + # Callback to open the datepicker (interval start) + # @param $event {Object} jQuery event object + ## + $scope.toggleStartDatePicker = ($event) -> + $scope.exportStart.opened = !$scope.exportStart.opened + + + + ## + # Callback to open the datepicker (interval end) + # @param $event {Object} jQuery event object + ## + $scope.toggleEndDatePicker = ($event) -> + $scope.exportEnd.opened = !$scope.exportEnd.opened + + + $scope.ok = (info) -> + $uibModalInstance.close( info ) + $scope.cancel = -> + $uibModalInstance.dismiss('cancel') +] \ No newline at end of file diff --git a/app/assets/templates/admin/statistics/export.html.erb b/app/assets/templates/admin/statistics/export.html.erb new file mode 100644 index 000000000..85aa18683 --- /dev/null +++ b/app/assets/templates/admin/statistics/export.html.erb @@ -0,0 +1,68 @@ + + + diff --git a/app/assets/templates/admin/statistics/index.html.erb b/app/assets/templates/admin/statistics/index.html.erb index 9ffe47fba..39a104fb7 100644 --- a/app/assets/templates/admin/statistics/index.html.erb +++ b/app/assets/templates/admin/statistics/index.html.erb @@ -12,6 +12,7 @@
+ {{ 'evolution' | translate }}
diff --git a/app/controllers/api/statistics_controller.rb b/app/controllers/api/statistics_controller.rb index 7dca1b347..0ceb57504 100644 --- a/app/controllers/api/statistics_controller.rb +++ b/app/controllers/api/statistics_controller.rb @@ -14,6 +14,16 @@ class API::StatisticsController < API::ApiController results = Stats::#{path.classify}.search(query, request.query_parameters.symbolize_keys).response render json: results end + + def export_#{path} + authorize :statistic, :#{path}? + query = MultiJson.load(request.body.read) + results = Stats::#{path.classify}.search(query, request.query_parameters.symbolize_keys).response + respond_to do |format| + format.html + format.xls + end + end } end diff --git a/config/locales/app.admin.en.yml b/config/locales/app.admin.en.yml index 5c668f460..1093f28dc 100644 --- a/config/locales/app.admin.en.yml +++ b/config/locales/app.admin.en.yml @@ -389,6 +389,10 @@ en: unknown: "Unknown" user_id: "User ID" display_more_results: "Display more results" + export_statistics_to_excel: "Export statistics to Excel" + export_all_statistics: "Export all statistics" + export_the_current_search_results: "Export the current search results" + export: "Export" stats_graphs: diff --git a/config/locales/app.admin.fr.yml b/config/locales/app.admin.fr.yml index ce66544db..1372d6b27 100644 --- a/config/locales/app.admin.fr.yml +++ b/config/locales/app.admin.fr.yml @@ -389,6 +389,10 @@ fr: unknown: "Inconnu" user_id: "ID Utilisateur" display_more_results: "Afficher plus de résultats" + export_statistics_to_excel: "Exporter les statistiques vers Excel" + export_all_statistics: "Exporter toutes les statistiques" + export_the_current_search_results: "Exporter les résultats de la recherche courante" + export: "Exporter" stats_graphs: diff --git a/config/routes.rb b/config/routes.rb index 1226d6dbb..89ffae482 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -127,6 +127,7 @@ Rails.application.routes.draw do %w(account event machine project subscription training user).each do |path| post "/stats/#{path}/_search", to: "api/statistics##{path}" + post "/stats/#{path}/export", to: "api/statistics#export_#{path}" end post '_search/scroll', to: "api/statistics#scroll"