diff --git a/app/assets/javascripts/controllers/admin/statistics.coffee.erb b/app/assets/javascripts/controllers/admin/statistics.coffee.erb index 60925e364..d9b5f058f 100644 --- a/app/assets/javascripts/controllers/admin/statistics.coffee.erb +++ b/app/assets/javascripts/controllers/admin/statistics.coffee.erb @@ -2,8 +2,8 @@ -Application.Controllers.controller "StatisticsController", ["$scope", "$state", "$rootScope", '$uibModal', "Statistics", "es", "Member", '_t', 'membersPromise', 'statisticsPromise' -, ($scope, $state, $rootScope, $uibModal, Statistics, es, Member, _t, membersPromise, statisticsPromise) -> +Application.Controllers.controller "StatisticsController", ["$scope", "$state", "$rootScope", '$uibModal', "Export", "es", "Member", '_t', 'membersPromise', 'statisticsPromise' +, ($scope, $state, $rootScope, $uibModal, Export, es, Member, _t, membersPromise, statisticsPromise) -> @@ -288,9 +288,24 @@ Application.Controllers.controller "StatisticsController", ["$scope", "$state", $uibModal.open options .result['finally'](null).then (info)-> - console.info(info) - , (reason)-> - console.error(reason) + # export requested + if info.type == 'current' + custom = buildCustomFilterQuery() + Export.stats $scope.selectedIndex.es_type_key, + buildElasticDataQuery($scope.type.active.key, custom, $scope.agePicker.start, $scope.agePicker.end, moment($scope.datePickerStart.selected), moment($scope.datePickerEnd.selected), $scope.sorting) + else if info.type == 'global' + Export.stats 'global', + "query": + "bool": + "must": [ + { + "range": + "date": + "gte": moment(info.dates.start).format() + "lte": moment(info.dates.end).format() + } + ] + @@ -331,12 +346,7 @@ Application.Controllers.controller "StatisticsController", ["$scope", "$state", $scope.sumStat = 0 $scope.totalHits = null $scope.searchDate = new Date() - custom = null - if $scope.customFilter.criterion and $scope.customFilter.criterion.key and $scope.customFilter.value - custom = {} - custom.key = $scope.customFilter.criterion.key - custom.value = $scope.customFilter.value - custom.exclude = $scope.customFilter.exclude + custom = buildCustomFilterQuery() queryElasticStats $scope.selectedIndex.es_type_key, $scope.type.active.key, custom, (res, err)-> if (err) console.error("[statisticsController::refreshStats] Unable to refresh due to "+err) @@ -508,6 +518,20 @@ Application.Controllers.controller "StatisticsController", ["$scope", "$state", + ## + # Build and return an object according to the custom filter set by the user, used to request elasticsearch + # @return {Object|null} + ## + buildCustomFilterQuery = -> + custom = null + if $scope.customFilter.criterion and $scope.customFilter.criterion.key and $scope.customFilter.value + custom = {} + custom.key = $scope.customFilter.criterion.key + custom.value = $scope.customFilter.value + custom.exclude = $scope.customFilter.exclude + custom + + # init the controller (call at the end !) initialize() @@ -518,10 +542,10 @@ Application.Controllers.controller "StatisticsController", ["$scope", "$state", Application.Controllers.controller 'ExportStatisticsController', [ '$scope', '$uibModalInstance', 'dates', ($scope, $uibModalInstance, dates) -> - # bindings for date range + ## Bindings for date range $scope.dates = dates - + ## Binding of the export type (global / current) $scope.export = type: 'current' @@ -543,6 +567,8 @@ Application.Controllers.controller 'ExportStatisticsController', [ '$scope', '$u options: startingDay: Fablab.weekStartingDay + + ## # Callback to open the datepicker (interval start) # @param $event {Object} jQuery event object @@ -560,8 +586,24 @@ Application.Controllers.controller 'ExportStatisticsController', [ '$scope', '$u $scope.exportEnd.opened = !$scope.exportEnd.opened - $scope.ok = (info) -> - $uibModalInstance.close( info ) + + ## + # Callback to close the modal, telling the caller to start the export with the selected parameters + ## + $scope.ok = -> + info = + type: $scope.export.type + + if info.type == 'global' + info.dates = $scope.dates + + $uibModalInstance.close(info) + + + + ## + # Callback to cancel the export and close the modal + ## $scope.cancel = -> $uibModalInstance.dismiss('cancel') ] \ No newline at end of file diff --git a/app/assets/javascripts/services/export.coffee b/app/assets/javascripts/services/export.coffee new file mode 100644 index 000000000..5f7d0483d --- /dev/null +++ b/app/assets/javascripts/services/export.coffee @@ -0,0 +1,11 @@ +'use strict' + +Application.Services.factory 'Export', ["$http", ($http)-> + stats: (scope, query) -> + $http.post('/stats/'+scope+'/export', query).then((res) -> + console.log(res) + , (err) -> + console.error(err) + ) + +] diff --git a/app/controllers/api/statistics_controller.rb b/app/controllers/api/statistics_controller.rb index 0ceb57504..bf85952dc 100644 --- a/app/controllers/api/statistics_controller.rb +++ b/app/controllers/api/statistics_controller.rb @@ -19,14 +19,16 @@ class API::StatisticsController < API::ApiController 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 + render xls: results end } end + def export_global + # query all stats with range arguments + render xls: [] + end + def scroll authorize :statistic, :scroll? diff --git a/config/routes.rb b/config/routes.rb index 89ffae482..b96d8c9ed 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -129,6 +129,7 @@ Rails.application.routes.draw do post "/stats/#{path}/_search", to: "api/statistics##{path}" post "/stats/#{path}/export", to: "api/statistics#export_#{path}" end + post '/stats/global/export', to: "api/statistics#export_global" post '_search/scroll', to: "api/statistics#scroll" match '/project_collaborator/:valid_token', to: 'api/projects#collaborator_valid', via: :get