From 3919e6fc1f4940feb04e12d1949e0b21e62ece85 Mon Sep 17 00:00:00 2001 From: Chris Reid Date: Fri, 5 May 2017 11:55:48 -0600 Subject: [PATCH] fixing sorting --- public/index.html | 110 ++++++++++++++++++++++++++++------------------ 1 file changed, 67 insertions(+), 43 deletions(-) diff --git a/public/index.html b/public/index.html index 55d67ad..ca110ab 100644 --- a/public/index.html +++ b/public/index.html @@ -32,7 +32,7 @@
- +
@@ -47,7 +47,7 @@
  • - @@ -118,7 +118,7 @@
    -
    +
    @@ -140,30 +140,48 @@ templateUrl: '/myPipeline.html' }; }); + app.filter('pipeOrdering', function ($filter) { + return function (items, fieldList, pipeSortBy, scope) { + var itemz = $filter('orderBy')(items, fieldList); + if (pipeSortBy === 'aggregateStatus') { + return $filter('orderByPipeStatus')(itemz, scope); + } + return itemz; + }; + }); + app.filter('orderByPipeStatus', function ($filter) { - function pipeStatusNumber(pipe) { + var status = { + 'succeeded': 1, + 'failed': 2, + 'errored': 3, + 'aborted': 8, + 'no-builds': 9 + }; + function pipeStatusNumber(pipe, pipeStatus) { if (pipe.paused) { return 100; } - var status = { - 'succeeded': 1, - 'failed': 2, - 'errored': 3, - 'aborted': 8, - 'no-builds': 9 - }; - return status[pipe._aggregateStatus]; + return status[pipeStatus]; } return function (items, scope) { var filtered = []; angular.forEach(items, function(item) { + item._aggregateStatusWeight = pipeStatusNumber(item, scope.getPipeAggregateStatus(item, $filter)); filtered.push(item); - item._aggregateStatus = scope.getPipeAggregateStatus(item, $filter); }); - filtered.sort(function (a, b) { - return (pipeStatusNumber(a) > pipeStatusNumber(b) ? 1 : -1); + filtered.sort(function (a, b) { + var weightA = a._aggregateStatusWeight; + var weightB = b._aggregateStatusWeight; + if (weightA > weightB) { + return 1; + } else if (weightA < weightB) { + return -1; + } else { + return 0; + } }); return filtered; }; @@ -256,38 +274,45 @@ }); } - $scope.getPipeAggregateStatus = function(pipe, $filter) { + $scope.getPipeAggregateStatus = function(pipe, $filter, force) { var status = null; - var pending = null; - if (pipe.jobs) { - var filter = $scope.getJobFilter(); - var result = $filter('pipeFilters').apply(this, [pipe.jobs, filter, 'job', $scope]); - result.forEach(function(job) { - if (job.finished_build) { - if (status === null) { - status = job.finished_build.status; - } else if(status === 'succeeded' && job.finished_build.status === 'failed') { - status = job.finished_build.status; - } else if(job.finished_build.status === 'errored') { - status = job.finished_build.status; - } else if(job.finished_build.status === 'aborted') { - status = job.finished_build.status; + + // check "cache" + var cacheTimeout = pipe._aggregateStatusTime < (new Date().getTime() - 250); + if (force || !pipe.aggregateStatus || cacheTimeout) { + var pending = null; + if (pipe.jobs) { + var filter = $scope.getJobFilter(); + var result = $filter('pipeFilters').apply(this, [pipe.jobs, filter, 'job', $scope]); + result.forEach(function(job) { + if (job.finished_build) { + if (status === null) { + status = job.finished_build.status; + } else if(status === 'succeeded' && job.finished_build.status === 'failed') { + status = job.finished_build.status; + } else if(job.finished_build.status === 'errored') { + status = job.finished_build.status; + } else if(job.finished_build.status === 'aborted') { + status = job.finished_build.status; + } } - } - if (job.next_build !== null) { - if (pending === null) { - pending = job.next_build.status; + if (job.next_build !== null) { + if (pending === null) { + pending = job.next_build.status; + } } - } - }); - } else { - //console.log(pipe.name, 'has no jobs'); - } + }); + } - if (status === null) { - status = 'no-builds'; + if (status === null) { + status = 'no-builds'; + } + + pipe._aggregateStatusTime = new Date().getTime(); + } else { + status = pipe.aggregateStatus; } return status; @@ -337,7 +362,7 @@ var defaultPipeSortBy = 'name'; var pipeSortBy = $cookies.getObject('pipeSortBy'); if (!pipeSortBy) { - pipeSortBy = defaultPipeSortBy; + pipeSortBy = angular.copy(defaultPipeSortBy); } $scope.pipeSortBy = pipeSortBy; $scope.$watch('pipeSortBy', function(newValue, oldValue){ @@ -385,7 +410,6 @@ $scope.$watch('filter', function(newValue, oldValue){ // save the value to a cookie - console.log(newValue.aggregateStatus); $cookies.putObject('filter', newValue); }, true); $scope.getPipeFilter = function() {