From 4639a15e2f2f7c8afb836d649c54ebbe5006726d Mon Sep 17 00:00:00 2001 From: Sylvain Date: Tue, 21 Jun 2016 13:16:42 +0200 Subject: [PATCH] [feature] paginate statistics --- .../controllers/admin/statistics.coffee | 17 +++++++++++++---- .../templates/admin/statistics/index.html.erb | 4 ++-- app/controllers/api/statistics_controller.rb | 8 ++++++++ app/policies/statistic_policy.rb | 2 +- config/locales/app.admin.en.yml | 1 + config/locales/app.admin.fr.yml | 1 + config/routes.rb | 1 + 7 files changed, 27 insertions(+), 7 deletions(-) diff --git a/app/assets/javascripts/controllers/admin/statistics.coffee b/app/assets/javascripts/controllers/admin/statistics.coffee index 22ecc5d77..a60896555 100644 --- a/app/assets/javascripts/controllers/admin/statistics.coffee +++ b/app/assets/javascripts/controllers/admin/statistics.coffee @@ -8,7 +8,7 @@ Application.Controllers.controller "StatisticsController", ["$scope", "$state", ### PRIVATE STATIC CONSTANTS ### ## search window size - RESULTS_PER_PAGE = 10 + RESULTS_PER_PAGE = 20 ## keep search context for (delay in minutes) ... ES_SCROLL_TIME = 1 @@ -250,19 +250,28 @@ Application.Controllers.controller "StatisticsController", ["$scope", "$state", return "ID "+id + + ## + # Run a scroll query to elasticsearch to append the next packet of results to those displayed. + # If the ES search context has expired when the user ask for more results, we re-run the whole query. + ## $scope.showMoreResults = -> + # if all results were retrieved, do nothing + if $scope.data.length >= $scope.totalHits + return + if moment($scope.searchDate).add(ES_SCROLL_TIME, 'minutes').isBefore(moment()) # elastic search context has expired, so we run again the whole query refreshStats() else - es.search - "size": RESULTS_PER_PAGE + es.scroll "scroll": ES_SCROLL_TIME+'m' - "body": $scope.scrollId + "body": {scrollId: $scope.scrollId} , (error, response) -> if (error) console.error "Error: something unexpected occurred during elasticSearch scroll query: "+error else + $scope.scrollId = response._scroll_id $scope.data = $scope.data.concat(response.hits.hits) diff --git a/app/assets/templates/admin/statistics/index.html.erb b/app/assets/templates/admin/statistics/index.html.erb index 552740864..ec1b79036 100644 --- a/app/assets/templates/admin/statistics/index.html.erb +++ b/app/assets/templates/admin/statistics/index.html.erb @@ -229,7 +229,7 @@