1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-11-28 09:24:24 +01:00

[feature] paginate statistics

This commit is contained in:
Sylvain 2016-06-21 13:16:42 +02:00
parent cb3cd8ee11
commit 4639a15e2f
7 changed files with 27 additions and 7 deletions

View File

@ -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)

View File

@ -229,7 +229,7 @@
<div id="totaux">
<ul>
<li>{{ 'entries' | translate }} {{data.length}}</li>
<li>{{ 'entries' | translate }} {{totalHits}}</li>
<li ng-show="selectedIndex.ca">{{ 'revenue_' | translate }} {{sumCA | currency}}</li>
<li>{{ 'average_age' | translate }} {{averageAge}} {{ 'years_old' | translate }}</li>
<li ng-if="!type.active.simple">{{ 'total' | translate }} {{type.active.label}} : {{sumStat}}</li>
@ -279,7 +279,7 @@
</tbody>
</table>
<div class="text-center">
<button class="btn btn-warning" ng-click="showMoreResults()" ng-hide="member.noMore"><i class="fa fa-search-plus" aria-hidden="true"></i> {{ 'display_more_results' | translate }}</button>
<button class="btn btn-warning" ng-click="showMoreResults()" ng-hide="data && data.length >= totalHits"><i class="fa fa-search-plus" aria-hidden="true"></i> {{ 'display_more_results' | translate }}</button>
</div>
</uib-tab>
</uib-tabset>

View File

@ -16,4 +16,12 @@ class API::StatisticsController < API::ApiController
end
}
end
def scroll
authorize :statistic, :scroll?
results = Elasticsearch::Client.new.scroll scroll: params[:scroll], scroll_id: params[:scrollId]
render json: results
end
end

View File

@ -1,5 +1,5 @@
class StatisticPolicy < ApplicationPolicy
['index', 'account', 'event', 'machine', 'project', 'subscription', 'training', 'user'].each do |action|
%w(index account event machine project subscription training user scroll).each do |action|
define_method "#{action}?" do
user.is_admin?
end

View File

@ -388,6 +388,7 @@ en:
revenue: "Revenue"
unknown: "Unknown"
user_id: "User ID"
display_more_results: "Display more results"
stats_graphs:

View File

@ -388,6 +388,7 @@ fr:
revenue: "Chiffre d'affaires"
unknown: "Inconnu"
user_id: "ID Utilisateur"
display_more_results: "Afficher plus de résultats"
stats_graphs:

View File

@ -104,6 +104,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}"
end
post '_search/scroll', to: "api/statistics#scroll"
match '/project_collaborator/:valid_token', to: 'api/projects#collaborator_valid', via: :get